【发布时间】:2013-11-23 12:53:11
【问题描述】:
我的问题与R: Custom Legend for Multiple Layer ggplot 和Format legend for multiple layers ggplot2 密切相关,即:我想为多层绘图创建自定义图例。但是,有一个细微的区别:
在最初的问题中,期望的效果是从两种不同的分组方法中分离出来:fill 和color,这就是为什么可以使用两个不同的scale_XXX 函数。就我而言,我创建了一个情节
包含点(一层)和线(第二层)。两层都以颜色区分:
x <- seq(0, 10, .1)
y <- sin(x)
lbl <- ifelse(y > 0, 'positive', 'non-positive')
data.one <- data.frame(x=x, y=y, lbl=lbl)
data.two <- data.frame(x=c(0, 10, 0, 10), y=c(-0.5, -0.5, 0.5, 0.5), classification=c('low', 'low', 'high', 'high'))
plt <- ggplot(data.one) + geom_point(aes(x, y, color=lbl)) + scale_color_discrete(name='one', guide='legend')
plt <- plt + geom_line(data=data.two, aes(x, y, color=classification)) + scale_color_discrete(name='two', guide='legend')
print(plt)
结果如下:
我想要的是将点和线的图例分开,使图例看起来像这样:
我找不到针对我的情况采用引用问题的方法的方法。有什么想法吗?
【问题讨论】:
-
你可以看看this post and comments therein,例如“ggplot2 的设计不允许多个图例用于相同的美学”。因此,解决方案很可能是 hack-ish。