我一直在为一个图例问题而苦恼,当使用
绘图代码是...
这段文字的意思是:
这导致了以下图表...
我真正想要的是...
geom_point
和geom_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](https://istack.dev59.com/bIYOd.webp)
- 将图例合并为一个(标题=“故事部分”)
- 让图例键的颜色、线型和符号正确,因为它们现在都混乱了。
- 知道是否有一种简单的方法来锚定图例位置为右上角,而不必每次更改图表时调整
legend.position
参数。
scale_
部分进行排序的问题吗?
scale
的调用中使用name="故事部分"
。至于图例位置,为什么不使用默认值呢?根据你当前的绘图,没有办法将图例放在图内而不重叠数据。 - Heroka