简体   繁体   English

ggplot2中同一图例中的不同图例键

[英]Different legend-keys inside same legend in ggplot2

Let's say I don't need a 'proper' variable mapping but still would like to have legend keys to help the chart understanding.假设我不需要“适当的”变量映射,但仍希望使用图例键来帮助理解图表。 My actual data are similar to the following df我的实际数据类似于以下df

df <- data.frame(id = 1:10, line = rnorm(10), points = rnorm(10))

library(ggplot2)

ggplot(df) +
  geom_line(aes(id, line, colour = "line")) +
  geom_point(aes(id, points, colour = "points"))

在此处输入图片说明

Basically, I would like the legend key relative to points to be.. just a point, without the line in the middle.基本上,我希望相对于points的图例键是......只是一个点,中间没有线。 I got close to that with this:我接近了这个:

library(reshape2)

df <- melt(df, id.vars="id")

ggplot() +
  geom_point(aes(id, value, shape = variable), df[df$variable=="points",]) +
  geom_line(aes(id, value, colour = variable), df[df$variable=="line",])

but it defines two separate legends.但它定义了两个单独的图例。 Fixing the second code (and having to reshape my data) would be fine too, but I'd prefer a way (if any) to manually change any legend key (and keep using the first approch).修复第二个代码(并且必须重塑我的数据)也可以,但我更喜欢一种方法(如果有的话)来手动更改任何图例键(并继续使用第一个方法)。 Thanks!谢谢!

EDIT :编辑 :

thanks @alexwhan you refreshed my memory about variable mapping.谢谢@alexwhan,你刷新了我对变量映射的记忆。 However, the easiest way I've got so far is still the following (very bad hack!):但是,到目前为止,我最简单的方法仍然是以下(非常糟糕的黑客!):

df <- data.frame(id = 1:10, line = rnorm(10), points = rnorm(10))

ggplot(df) +
  geom_line(aes(id, line, colour = "line")) +
  geom_point(aes(id, points, shape = "points")) +
  theme(legend.title=element_blank())

which is just hiding the title of the two different legends.这只是隐藏了两个不同传说的标题。

在此处输入图片说明

Other ideas more than welcome!!!其他想法更受欢迎!!!

You can use override.aes= inside guides() function to change default appearance of legend.您可以在guides()函数中使用override.aes=来更改图例的默认外观。 In this case your guide is color= and then you should set shape=c(NA,16) to remove shape for line and then linetype=c(1,0) to remove line from point.在这种情况下,您的指南是color= ,然后您应该设置shape=c(NA,16)以删除线的形状,然后linetype=c(1,0)从点删除线。

ggplot(df) +
  geom_line(aes(id, line, colour = "line")) +
  geom_point(aes(id, points, colour = "points"))+
  guides(color=guide_legend(override.aes=list(shape=c(NA,16),linetype=c(1,0))))

在此处输入图片说明

I am not aware of any way to do this easily, but you can do a hack version like this (using your melted dataframe):我不知道有什么方法可以轻松做到这一点,但你可以做一个这样的黑客版本(使用你融化的数据框):

p <- ggplot(df.m, aes(id, value)) +
  geom_line(aes(colour = variable, linetype = variable)) + scale_linetype_manual(values = c(1,0)) +
  geom_point(aes(colour = variable, alpha = variable)) + scale_alpha_manual(values = c(0,1))

在此处输入图片说明

The key is that you need to get the mapping right to have it displayed correctly in the legend.关键是您需要获得正确的映射才能使其在图例中正确显示。 In this case, getting it 'right', means fooling it to look the way you want it to.在这种情况下,让它“正确”,意味着愚弄它看起来像你想要的样子。 It's probably worth pointing out this only works because you can set linetype to blank (0) and then use the alpha scale for the points.可能值得指出这仅适用,因为您可以将linetype设置为空白 (0),然后对点使用alpha比例。 You can't use alpha for both, because it will only take one scale.您不能对两者都使用alpha ,因为它只需要一个比例。

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

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