Create Legend in ggplot Plot in R (2 Examples) | How to Add Legends to Graphic | ggplot2 Package

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

y = rnorm(100),
group = factor(round(runif(100, 1, 5))))
head(data) # Inspect data

library("ggplot2") # Load ggplot2 library

ggplot(data, aes(x, y)) + # ggplot without legend
geom_point(col = data$group)

ggplot(data, aes(x, y, col = group)) + # ggplot with legend
geom_point()

ggplot(data, aes(x, y)) + # Specifying legend in geom
geom_point(aes(col = group))
Рекомендации по теме
Комментарии
Автор

What if the legend should be picked from a spss data? Specifically, from the labels of a vector?
Because running the code is not working for me

javierhernando
Автор

can you create a video on how to change the labels of the legend?

ZomnY
Автор

What if you want to merge for example the numbers 3 4 and 5 into 1 lable called "Others"? How do you do that? thanks!

idsl
Автор

how to give legend if data.frame is not divided in groups

meenakshigautam
join shbcf.ru