简体   繁体   English

如何修复 ggplot2 中这种奇怪的图例行为?

[英]How can I fix this strange behavior of legend in ggplot2?

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.上一个问题的解决方案工作正常( 如何在 ggplot2 中使用一个点条目和一个线条目创建图例? ),除非您更改图例的标签。

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 ). (唯一的区别是 word data更改为spreads )。 Note that I can change data to abc and it works fine.请注意,我可以将data更改为abc并且它工作正常。 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 ).其中一个起作用而另一个不起作用的原因是因为当values是未命名的向量时,它们“将按顺序(通常按字母顺序)与比例限制匹配”( 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))

I don't know why that does not work.我不知道为什么这不起作用。 But if you just want Spread in label, the following solution works但是,如果您只想在标签中传播,则以下解决方案有效

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM