在ggplot2中合并图例

3
我一直在为一个图例问题而苦恼,当使用geom_pointgeom_smooth时,我的绘图上出现了两个不同的图例。尽管我尝试了很多方法,但我仍然无法想出如何将它们合并成更易于使用的形式。
绘图代码是...
require(ggplot2)
require(scales)
require(cowplot)

palette <- scales::hue_pal()(3)
ggplot(df.Overview, aes(x=datePublished, y=sentiment, colour = type, group=type, linetype = type)) +
                geom_point(aes(shape=factor(type)), size=3.5, position=position_jitter(width=0.3), alpha = 0.5) +
                geom_smooth(fullrange = TRUE, alpha = .25, show_guide = TRUE) +
                scale_x_date(breaks = "1 week", labels=date_format("%b-%d"), limits = c(overviewStartDate,overviewEndDate)) + # limit plot to overview dates
                scale_y_continuous(limits =c(-1,1), oob=squish) +  # set upper and lower bounds of Y axis
                theme_bw() +
                background_grid(major = "xy", minor = "none") +
                labs(x = "", y = "Sentiment Index") +
                scale_colour_manual( values = palette,
                                     name="GSE", 
                                     breaks=c("sentiment_TitleDescMean", "sentiment_body"), 
                                     labels = c("x\u0304 (Title & Desc)", "Body") ) +
                scale_shape_manual( values = c('sentiment_TitleDescMean' = 17, 'sentiment_body'= 15),
                                    name="Story Part ",  
                                    breaks=c("sentiment_TitleDescMean", "sentiment_body"),
                                    labels = c("x\u0304 (Title & Desc)", "Body") ) +
                scale_linetype_discrete(name="GSE", 
                                        breaks=c("sentiment_TitleDescMean",  "sentiment_body"), 
                                        labels = c("x\u0304 (Title & Desc)", "Body") ) +
                theme(legend.key=element_rect(fill='white'), legend.position=c(.05,.75), legend.background = element_rect(fill="white", size=0.5, linetype="solid", colour ="grey30"))    # set legend position  

这段文字的意思是:
这导致了以下图表... enter image description here 我真正想要的是...
  1. 将图例合并为一个(标题=“故事部分”)
  2. 让图例键的颜色、线型和符号正确,因为它们现在都混乱了。
  3. 知道是否有一种简单的方法来锚定图例位置为右上角,而不必每次更改图表时调整legend.position参数。
请问有谁能帮忙解决我怀疑是对scale_部分进行排序的问题吗?

3
你需要给图例相同的名称,因此在所有对scale的调用中使用name="故事部分"。至于图例位置,为什么不使用默认值呢?根据你当前的绘图,没有办法将图例放在图内而不重叠数据。 - Heroka
那很简单,而且运行良好,谢谢Heroka。传奇的位置在这里很重要 - 当“真实”情节处于全尺寸时,它实际上适合右上角,只是;o) - BarneyC
2
这个问题已经被问过并得到了回答,可以在以下链接中找到:http://stackoverflow.com/search?q=[r]+[ggplot2]+combine+legend - Jaap
主题(图例位置 = c(1,1),图例对齐 = c(1,1)) - Roland
1个回答

2

Heroka的评论解答了如何将图例清晰地组合在一起的主要问题。非常简单...只需更改对scale的调用,使得每个调用中的name="xxx"都相同。

就这样...

scale_colour_manual( values = palette,
                                     name="Story Part", 
                                     breaks=c("sentiment_TitleDescMean", "sentiment_body"), 
                                     labels = c("x\u0304 (Title & Desc)", "Body") ) +
                scale_shape_manual( values = c('sentiment_TitleDescMean' = 17, 'sentiment_body'= 15),
                                    name="Story Part",  
                                    breaks=c("sentiment_TitleDescMean", "sentiment_body"),
                                    labels = c("x\u0304 (Title & Desc)", "Body") ) +
                scale_linetype_discrete(name="Story Part", 
                                        breaks=c("sentiment_TitleDescMean",  "sentiment_body"), 
                                        labels = c("x\u0304 (Title & Desc)", "Body") )

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接