英文:
Adding legends to 3 plots in ggplot2
问题
For a sample dataframe:
date = c("2021-2022", "2020-2021", "2019-2020", "2018-2019", "2017-2018", "2016-2017", "2015-2016", "2014-2015", "2013-2014", "2012-2013", "2011-2012")
cats = c(489701, 502743, 510776, 515904, 477746, 495208, 508466, 475334, 483368, 462693, 429065)
dogs = c(124046, 129445, 132384, 140041, 68535, 64516, 63950, 60076, 59650, 59441, 56194)
pigs = c(91651, 85101, 78457, 63734, 45592, 43392, 40970, 38530, 37182, 36256, 34464)
df1 <- data.frame(date, cats, dogs, pigs)
To create a legend for the dogs and pigs lines and remove the red outline, you can modify your ggplot code as follows:
p1 <- ggplot(data=df1, aes(x=date)) +
geom_bar(aes(y=cats, fill="cats"), stat="identity") +
geom_line(aes(y=dogs, color="dogs"), size = 0.75, group = 1) +
geom_point(aes(y=dogs, color="dogs"), size = 1.5) +
geom_line(aes(y=pigs, color="pigs", linetype="pigs"), size = 0.75, group = 1) +
geom_point(aes(y=pigs, color="pigs"), size = 1.5) +
theme(axis.text.x = element_text(angle = 60, vjust = 0.5)) +
scale_y_continuous(labels = comma,
limits=c(0,550000),
name = "number") +
labs(x = "Year") +
scale_fill_manual(name = "Animals", values = c(cats = "blue")) +
scale_color_manual(name = "Animals", values = c(dogs = "red", pigs = "blue")) +
scale_linetype_manual(name = "Animals", values = c(pigs = "dashed"))
p1
This code assigns different aesthetics (color, fill, linetype) to each animal, allowing you to create a legend for the lines while specifying the colors and linetype for each animal.
英文:
For a sample dataframe:
date = c("2021-2022", "2020-2021", "2019-2020", "2018-2019", "2017-2018", "2016-2017", "2015-2016", "2014-2015", "2013-2014", "2012-2013", "2011-2012")
cats = c(489701, 502743, 510776, 515904, 477746, 495208, 508466, 475334, 483368, 462693, 429065)
dogs = c(124046, 129445,132384, 140041, 68535, 64516, 63950, 60076, 59650, 59441, 56194)
pigs = c(91651, 85101, 78457, 63734, 45592, 43392, 40970, 38530, 37182, 36256, 34464)
df1 <- data.frame(date, cats, dogs, pigs)
I am trying to bar graph the cats data, and line graph the dogs and pig data.
So far I have:
p1<-ggplot(data=df1, aes(x=date, y=cats, colour = "cats")) +
geom_bar(stat="identity") +
theme(axis.text.x = element_text(angle = 60, vjust = 0.5)) +
scale_y_continuous(labels = comma,
limits=c(0,550000),
name = "number") +
labs(x = "Year") +
geom_line(data = df1, aes(x=date, y=dogs), color = 'red', size = 0.75, group = 1) +
geom_point(data = df1, aes(x=date, y=dogs), color = 'red', size = 1.5) +
geom_line(data = df1, aes(x=date, y=pigs), color = 'blue', size = 0.75, , group = 1, linetype = "dashed") +
geom_point(data = df1, aes(x=date, y=pigs), color = 'blue', size = 1.5)
p1
I am struggling to get a legend for the dogs and pigs line. I understand this is related to not having an appropriate aes characteristic - but how do I do this if I am already using color to set the line colour? (FYI - I don't want the red outline but can't seem to find what's causing that).
I understand that part of my trouble is an untidy dataset, but I also had trouble converting it to a long dataset:
df2 <- pivot_longer(date, names_to = "animals", values_to = "count")
Error in UseMethod("pivot_longer") :
no applicable method for 'pivot_longer' applied to an object of class "character"
答案1
得分: 2
问题是要获得图例,您必须映射到美学属性,即不要将线条颜色设置为参数,而是在aes()
内部映射到color
aes,并通过scale_color_manual
设置所需的颜色:
library(ggplot2)
library(scales)
ggplot(data = df1, aes(x = date)) +
geom_col(aes(y = cats, fill = "cats")) +
theme(axis.text.x = element_text(angle = 60, vjust = 0.5)) +
scale_y_continuous(
labels = comma,
limits = c(0, 550000),
name = "number"
) +
labs(x = "Year") +
geom_line(aes(y = dogs, color = "dogs"),
size = 0.75, group = 1
) +
geom_point(aes(y = dogs, color = "dogs"), size = 1.5) +
geom_line(aes(y = pigs, color = "pigs"),
size = 0.75, group = 1,
linetype = "dashed"
) +
geom_point(aes(y = pigs, color = "pigs"), size = 1.5) +
scale_color_manual(values = c(pigs = "blue", dogs = "red"))
英文:
The issue is that to get a legend you have to map on aesthetics, i.e. instead of setting the line colors as arguments map on the color
aes inside aes()
and set your desired colors via scale_color_manual
:
library(ggplot2)
library(scales)
ggplot(data = df1, aes(x = date)) +
geom_col(aes(y = cats, fill = "cats")) +
theme(axis.text.x = element_text(angle = 60, vjust = 0.5)) +
scale_y_continuous(
labels = comma,
limits = c(0, 550000),
name = "number"
) +
labs(x = "Year") +
geom_line(aes(y = dogs, color = "dogs"),
size = 0.75, group = 1
) +
geom_point(aes(y = dogs, color = "dogs"), size = 1.5) +
geom_line(aes(y = pigs, color = "pigs"),
size = 0.75, group = 1,
linetype = "dashed"
) +
geom_point(aes(y = pigs, color = "pigs"), size = 1.5) +
scale_color_manual(values = c(pigs = "blue", dogs = "red"))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论