我怎样才能在ggplot2中修复这种奇怪的传奇行为? [英] How can I fix this strange behavior of legend in ggplot2?

查看:177
本文介绍了我怎样才能在ggplot2中修复这种奇怪的传奇行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以前的问题的解决方案工作正常(除了当您更改图例的标签时,如何在ggplot2中使用一个点条目和一个条目?)。

这就是我的意思。一些数据:

  library(ggplot2)
名称< - 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)

p>

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

但是这并不是(图表是空的):

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

(仅差别字数据更改为点差)。请注意,我可以将 data 更改为 abc ,它可以正常工作。任何线索?感谢!

解决方案

原因之一是这些工作,另一个不是因为当 values 是一个unnammed向量,它们将按顺序(通常按字母顺序)与规模限制匹配( http://docs.ggplot2.org/0.9.3.1/scale_manual.html )。

所以这个工作:

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

将您的值恢复为原始顺序,但作为命名向量也可以看起来更安全/更清晰):

  ggplot(ex_data,aes(x = xvals,group = names))+ 
geom_point(aes(y = pvals,shape ='fit',linetype ='fit'))+ $ b geom_line(aes(y = yvals,shape ='spreadsheet',linetype ='spreads'))+
$ b scale_shape_manual('',values = c(spreads= 19,fitting= NA))+
scale_linetype_manual('',values = c(spreads= 0,fitting= 1) )


The solution to this previous question works fine (How can I make a legend in ggplot2 with one point entry and one line entry?) except when you change the label for the legend.

Here's what I mean. Some data:

library(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)

This works fine:

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))

But this doesn't (chart is empty):

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

(only difference is word data changed to spreads). Note that I can change data to abc and it works fine. Any clues? Thanks!

解决方案

The reason one of those works and the other doesn't is because when values is an unnammed vector, they "will be matched in order (usually alphabetical) with the limits of the scale" (http://docs.ggplot2.org/0.9.3.1/scale_manual.html). You changed which order is alphabetical.

So this works:

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

Putting your values back in the original order but as named vectors also works (and seems safer / clearer):

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

这篇关于我怎样才能在ggplot2中修复这种奇怪的传奇行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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