未指定aes()颜色和形状时,ggplot中的图例 [英] Legend in ggplot when aes() color and shape are not specified

查看:53
本文介绍了未指定aes()颜色和形状时,ggplot中的图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是ggplot的相对较新来的人,并且使用下面的数据和代码制作了下图……

I am a relative newcomer to ggplot, and have made the figure below with the data and code included here…

数据在这里

Data <- structure(list(IndID = structure(1:17, .Label = c("AA", "BB", 
"CC", "DD", "EE", "FF", "GG", "HH", "II", "JJ", "KK", "LL", "MM", 
"NN", "OO", "PP", "QQ"), class = "factor"), Avg = c(7.95, 10.483, 
5.951, 7.359, 10.465, 10.745, 14.402, 81.417, 67.087, 4.254, 
34.393, 47.324, 60.713, 75.446, 64.527, 28.779, 54.764), AvgSE = c(1.685, 
2.949, 1.097, 2.607, 4.256, 3.539, 1.702, 3.314, 0.714, 0.302, 
1.154, 1.827, 0.573, 1.292, 1.955, 1.341, 1.949), OBS = c(7.667, 
10, 8, 7.5, 14, 10.333, 12, 91, 53, 7, 29, 36.5, 43, 61, 61, 
24, 38)), .Names = c("IndID", "Avg", "AvgSE", "OBS"), class = "data.frame", row.names = c(NA, 
-17L))

看起来像这样

> head(Data)
  IndID    Avg AvgSE    OBS
1    AA  7.950 1.685  7.667
2    BB 10.483 2.949 10.000
3    CC  5.951 1.097  8.000
4    DD  7.359 2.607  7.500
5    EE 10.465 4.256 14.000
6    FF 10.745 3.539 10.333

我的情节代码在这里

ggplot(Data, aes(x=IndID, y=Avg))+
    geom_point()+
    geom_errorbar(aes(ymin=Avg-AvgSE, ymax=Avg+AvgSE))+
    geom_point(aes(y=OBS),color="red", pch = 8) +
    theme(axis.text.x=element_text(angle=30, hjust=1))

我绘制了两组不同的点.我没有要在 aes()参数中指定为 color shape 的因素.我见过的大多数SO帖子都使用这些参数,默认情况下会出现图例.据我所知(在看过很多文章并使用了R Graphics Cookbook之后),像在基本R函数中那样构建图例不太容易.

I have plotted two different sets of points. I do not have a factor to specify in the aes() argument as a color or shape. Most SO posts I have seen use these arguments after which a legend appears by default. As far as I can tell (after seeing many posts and using the R Graphics Cookbook), building a legend like in the base R functions is less straight forward.

是按此处建议更改数据结构的最佳选择http://

Is the best option to change the data structure as suggested here http://stackoverflow.com/questions/17713919/r-ggplot-2-geom-points-how-to-add-a-legend using melt()?

或者还有另一种创建图例的方法吗?

Or is there another way to create a legend?

在上面的图中,我只想为每个点集添加一个图例.一个用于黑色(Avg)点,另一个用于OBS点.

In my figure aboveI simply want a legend for each set of points. One for the black (Avg) pts and another for the OBS points.

任何建议将不胜感激!

推荐答案

我认为重塑数据确实更好,但这是一种方法.您需要在 aes()中映射颜色以创建图例.您可以将其映射到文本字符串:

I think you really are better off reshaping your data, but here's one way of doing it. You need to map colour in aes() to make a legend. You can map it to a text string:

p <- ggplot(Data, aes(x=IndID, y=Avg))+
  geom_point(aes(color = "Avg"))+
  geom_errorbar(aes(ymin=Avg-AvgSE, ymax=Avg+AvgSE))+
  geom_point(aes(y=OBS, color = "OBS"), pch = 8, show_guide = T) +
  theme(axis.text.x=element_text(angle=30, hjust=1))

要使用所需的颜色来获得颜色,请使用 scale_colour_manual(),然后可以使用 guides()覆盖图例的形状:

To get the colours the way you want, use scale_colour_manual(), and then you can override the shape of the legend using guides():

p + scale_colour_manual(values = c("black", "red")) + 
  guides(colour = guide_legend(override.aes = list(shape = c(16, 8))))

这篇关于未指定aes()颜色和形状时,ggplot中的图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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