Change Legend Labels of ggplot2 Plot in R (2 Examples) | Modify Item Text | scale_color_manual()

preview_player
Показать описание
R code of this video:

y = 1,
data # Print example data

library("ggplot2") # Load ggplot2

ggp <- ggplot(data, aes(x, y, col = group)) + # Create default ggplot2 plot
geom_point()
ggp # Draw ggplot2 plot

ggp + # Modify labels and colors
scale_color_manual(labels = c("Group 1", "Group 2", "Group 3"),
values = c("red", "blue", "green"))

data_new <- data # Duplicate data
levels(data_new$group) <- list("Group 1" = "a", # Change factor levels
"Group 2" = "b",
"Group 3" = "c")

ggp_new <- ggplot(data_new, aes(x, y, col = group)) + # Recreate ggplot2 plot
geom_point()
ggp_new # Draw updated ggplot2 plot

Follow me on Social Media:
Рекомендации по теме
Комментарии
Автор

You literally explained in seconds what took me hours, much appreciated!

robinbauwens
Автор

You are awesome! I have been looking for ways of formatting legends for so long. Thank you very much!!!

timding
Автор

May I just say your videos are superb. They have helped me countless times during my degree! The simple explanations and easy to follow videos are awesome. Thank You!!!

Olivertheking
Автор

I really appreciate all of your content. It has been a tremendous help. THANK YOU.

denisedrachenberg
Автор

This was really helpful and easy to follow, thank you!!

nicholaskerry
Автор

What caught my eye is the fact that data.frame "knows" that the value 1 is repeated six times. Seems like it is enough just to set one variable - in this case x - and the rest will follow. I did not know that and would probably have use rep() needlessly.

haraldurkarlsson
join shbcf.ru