Adding legends to 3 plots in ggplot2

huangapple go评论67阅读模式
英文:

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(&quot;2021-2022&quot;, &quot;2020-2021&quot;, &quot;2019-2020&quot;, &quot;2018-2019&quot;, &quot;2017-2018&quot;, &quot;2016-2017&quot;, &quot;2015-2016&quot;, &quot;2014-2015&quot;, &quot;2013-2014&quot;, &quot;2012-2013&quot;, &quot;2011-2012&quot;)
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 &lt;- 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&lt;-ggplot(data=df1, aes(x=date, y=cats, colour = &quot;cats&quot;)) +
  geom_bar(stat=&quot;identity&quot;) +
  theme(axis.text.x = element_text(angle = 60, vjust = 0.5)) +
  scale_y_continuous(labels = comma, 
                     limits=c(0,550000), 
                     name = &quot;number&quot;) +
  labs(x = &quot;Year&quot;) +
  geom_line(data = df1, aes(x=date, y=dogs), color = &#39;red&#39;, size = 0.75, group = 1) +
  geom_point(data = df1, aes(x=date, y=dogs), color = &#39;red&#39;, size = 1.5) +
  geom_line(data = df1, aes(x=date, y=pigs), color = &#39;blue&#39;, size = 0.75, , group = 1, linetype = &quot;dashed&quot;) +
  geom_point(data = df1, aes(x=date, y=pigs), color = &#39;blue&#39;, 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 &lt;- pivot_longer(date, names_to = &quot;animals&quot;, values_to = &quot;count&quot;)
Error in UseMethod(&quot;pivot_longer&quot;) : 
  no applicable method for &#39;pivot_longer&#39; applied to an object of class &quot;character&quot;

答案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"))

Adding legends to 3 plots in ggplot2

英文:

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 = &quot;cats&quot;)) +
  theme(axis.text.x = element_text(angle = 60, vjust = 0.5)) +
  scale_y_continuous(
    labels = comma,
    limits = c(0, 550000),
    name = &quot;number&quot;
  ) +
  labs(x = &quot;Year&quot;) +
  geom_line(aes(y = dogs, color = &quot;dogs&quot;),
    size = 0.75, group = 1
  ) +
  geom_point(aes(y = dogs, color = &quot;dogs&quot;), size = 1.5) +
  geom_line(aes(y = pigs, color = &quot;pigs&quot;),
    size = 0.75, group = 1,
    linetype = &quot;dashed&quot;
  ) +
  geom_point(aes(y = pigs, color = &quot;pigs&quot;), size = 1.5) +
  scale_color_manual(values = c(pigs = &quot;blue&quot;, dogs = &quot;red&quot;))

Adding legends to 3 plots in ggplot2

huangapple
  • 本文由 发表于 2023年5月8日 02:23:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76195600.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定