强制ggplot2为每个geom应用单独的图例 [英] Force ggplot2 to apply a separate legend for each geom

查看:182
本文介绍了强制ggplot2为每个geom应用单独的图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的图表中,使用geom_point映射的三角形都在同一图例中.本质上,我希望每个geom_都有自己的单独图例.我将如何去做?

In the chart below, the triangles mapped using geom_point are both in the same legend. Essentially, I would like each geom_ to have their own separate legend instead. How would I go about doing so?

这是我的可重复性代码:

Here's my code for reproducibility:

mydf <- data.frame(year = c(rep(2000, 3), rep(2002, 3), rep(2004, 3), rep(2006, 3), rep(2008, 3), rep(2010, 3), rep(2012, 3), rep(2014, 3), rep(2016, 3)),
                 answer = rep(c("A great deal", "Hardly any", "Only some"), 9),
                 result = c(0.3015940, 0.1399303, 0.5584757, 0.2269548, 0.1792754, 0.5937698, 0.2955301, 0.1309859, 0.5734840, 0.3008197, 0.1344499,
                            0.5647303, 0.1919454, 0.2026290, 0.6054256, 0.1059793, 0.4190533, 0.4749674, 0.1190636, 0.3631279, 0.5178085, 0.1518314,
                            0.3181203, 0.5300483, 0.1424715, 0.3094615, 0.5480669))
mydf$year <- factor(mydf$year)
mydf$answer <- factor(mydf$answer)
triangle_up <- data.frame(year = c(2004, 2008, 2010),
                        direction = c("A great deal", "Hardly any", "Hardly any"),
                        result = c(0.2955301, 0.2026290, 0.4190533))
triangle_up$year <- factor(triangle_up$year)
triangle_up$direction <- factor(triangle_up$direction)
triangle_down <- data.frame(year = c(2002, 2008, 2010, 2010, 2012),
                            direction = c(rep("A great deal", 3), "Only some", "Hardly any"),
                            result = c(0.2269548, 0.1919454, 0.1059793, 0.4749674, 0.3631279))
triangle_down$year <- factor(triangle_down$year)
triangle_down$direction <- factor(triangle_down$direction)

ggplot(mydf, aes(x = year, y = result)) + geom_line(aes(colour = answer, group = answer)) +
geom_point(data = triangle_up, aes(x = year, y = result, group = direction, fill = direction), shape = 24, size = 3) +
geom_point(data = triangle_down, aes(x = year, y = result, group = direction, fill = direction), shape = 25, size = 3)

推荐答案

此处的问题是使用多个数据框存储值.最好在mydf数据框中添加一列以存储direction变量.令人困惑的是,您已经在mydf中使用answer作为变量名,但是direction在其他数据帧中存储了相同的值.

The issue here is the use of multiple data frames to store values. It would be better to add a column to the mydf data frame to store the direction variable. Confusingly, you have used answer as the variable name in mydf, but direction to store the same values in the other data frames.

因此,这是新的mydf,在direction列中的值为"up","down"或NA:

So here is the new mydf with values "up", "down" or NA in the direction column:

mydf <- structure(structure(list(year = c(2000, 2000, 2000, 2002, 2002, 2002, 2004, 
2004, 2004, 2006, 2006, 2006, 2008, 2008, 2008, 2010, 2010, 2010, 
2012, 2012, 2012, 2014, 2014, 2014, 2016, 2016, 2016), answer = structure(c(1L, 
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 
3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), .Label = c("A great deal", 
"Hardly any", "Only some"), class = "factor"), result = c(0.301594, 
0.1399303, 0.5584757, 0.2269548, 0.1792754, 0.5937698, 0.2955301, 
0.1309859, 0.573484, 0.3008197, 0.1344499, 0.5647303, 0.1919454, 
0.202629, 0.6054256, 0.1059793, 0.4190533, 0.4749674, 0.1190636, 
0.3631279, 0.5178085, 0.1518314, 0.3181203, 0.5300483, 0.1424715, 
0.3094615, 0.5480669), direction = c(NA, NA, NA, "down", NA, 
NA, "up", NA, NA, NA, NA, NA, "down", "up", NA, "down", "up", 
"down", NA, "down", NA, NA, NA, NA, NA, NA, NA)), .Names = c("year", 
"answer", "result", "direction"), row.names = c(NA, -27L), class = 
"data.frame"))

现在,您可以为directionanswer分别绘制图例.使用scale_shape_manual手动指定形状,使用breaks忽略NA值.对于线条颜色,我们使用scale_color_manual并覆盖图例映射,以便仅显示线条,而不显示形状.

Now you can plot with separate legends for direction and answer. Shapes are specified manually using scale_shape_manual, using breaks to omit the NA values. For line colour, we use scale_color_manual and override the legend mapping so as only lines, not shapes, are shown.

ggplot(mydf, aes(year, result)) + 
  geom_line(aes(group = answer, color = answer)) + 
  geom_point(aes(shape = direction, fill = answer), size = 3) + 
  scale_shape_manual(values = c(25, 24), breaks = c("down", "up")) + 
  scale_color_manual(values = c("red", "green", "blue"),
                     guide = guide_legend(override.aes = list(shape = rep(NA, 3)))) +
  theme_light()

这篇关于强制ggplot2为每个geom应用单独的图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆