如何在ggplot2中使用一个点条目和一个条目创建图例? [英] How can I make a legend in ggplot2 with one point entry and one line entry?

查看:148
本文介绍了如何在ggplot2中使用一个点条目和一个条目创建图例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在ggplot2中绘制一个由一组数据点组成的图形,这些数据点绘制为点,并且通过拟合模型预测的线条被覆盖。图的总体思路如下所示:

  names <-c(1,1,1,2,2) ,2,3,3,3)
xvals <-c(1:9)
yvals <-c(1,2,3,10,11,12,15,16,17 )
pvals < - c(1.1,2.1,3.1,11,12,13,14,15,16)
ex_data < - data.frame(names,xvals,yvals,pvals)
ex_data $ names< - factor(ex_data $ names)

graph< - ggplot(data = ex_data,aes(x = xvals,y = yvals,color = names))
print(graph + geom_point()+ geom_line(aes(x = xvals,y = pvals)))



正如你所看到的,线条和点都被一个分类变量(在这种情况下为'名字')着色。我希望图例包含2个条目:一个标记为'Data'的点和一个标注为'Fitted'的线条(表示这些点是实际数据,而这些线条是适合的)。但是,我似乎无法得到这个工作。的(真棒)引导这里非常适合格式化,但不处理实际条目,而我尝试过技术这里无效,即


  print(graph + scale_colour_manual(,values = c(green,blue,red))
+ scale_shape_manual(,values = c (19,NA,NA))
+ scale_linetype_manual(,values = c(0,1,1)))

主要的麻烦在于,在我的实际数据中,名称有200多个不同的类别,而我只想要我在图例中提到的两个条目。这样做与我的实际数据只是产生一个没有意义的图例,因为图例试图成为颜色的关键(我的方式太多了)。



我很感激任何帮助!

解决方案

我认为这与您想要的接近:



pre $ g $ p $ ggplot(ex_data,aes(x = xvals,group = names))+
geom_point(aes(y = yvals,shape = 'data',linetype ='data'))+
geom_line(aes(y = pvals,shape ='fit',linetype ='fit'))+
scale_shape_manual('',values = c (0,1))



这个想法是,你为线和点指定了两个美学( linetype 形状),甚至尽管如此,对于具有 linetype 审美观点而言,这是毫无意义的。然后,您将这些无意义美学手动映射到空值(在这种情况下 NA 0 ),手动缩放。

I am making a graph in ggplot2 consisting of a set of datapoints plotted as points, with the lines predicted by a fitted model overlaid. The general idea of the graph looks something like this:

names <- c(1,1,1,2,2,2,3,3,3)
xvals <- c(1:9)
yvals <- c(1,2,3,10,11,12,15,16,17)
pvals <- c(1.1,2.1,3.1,11,12,13,14,15,16)
ex_data <- data.frame(names,xvals,yvals,pvals)
ex_data$names <- factor(ex_data$names)

graph <- ggplot(data=ex_data, aes(x=xvals, y=yvals, color=names))
print(graph + geom_point() + geom_line(aes(x=xvals, y=pvals)))

As you can see, both the lines and the points are colored by a categorical variable ('names' in this case). I would like the legend to contain 2 entries: a dot labeled 'Data', and a line labeled 'Fitted' (to denote that the dots are real data and the lines are fits). However, I cannot seem to get this to work. The (awesome) guide here is great for formatting, but doesn't deal with the actual entries, while I have tried the technique here to no avail, i.e.

print(graph + scale_colour_manual("", values=c("green", "blue", "red")) 
+ scale_shape_manual("", values=c(19,NA,NA)) 
+ scale_linetype_manual("",values=c(0,1,1))) 

The main trouble is that, in my actual data, there are >200 different categories for 'names,' while I only want the 2 entries I mentioned above in the legend. Doing this with my actual data just produces a meaningless legend that runs off the page, because the legend is trying to be a key for the colors (of which I have way too many).

I'd appreciate any help!

解决方案

I think this is close to what you want:

ggplot(ex_data, aes(x=xvals, group=names)) + 
  geom_point(aes(y=yvals, shape='data', linetype='data')) + 
  geom_line(aes(y=pvals, shape='fitted', linetype='fitted')) + 
  scale_shape_manual('', values=c(19, NA)) + 
  scale_linetype_manual('', values=c(0, 1))

The idea is that you specify two aesthetics (linetype and shape) for both lines and points, even though it makes no sense, say, for a point to have a linetype aesthetic. Then you manually map these "nonsense" aesthetics to "null" values (NA and 0 in this case), using a manual scale.

这篇关于如何在ggplot2中使用一个点条目和一个条目创建图例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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