scale_color_manual 在 ggplot 中仅显示一种颜色
我使用 RStudio 中的 ggplot 创建了包含一系列数据的折线图。我的图例仅显示一种颜色,始终是 geom_line 使用 show.legend=TRUE 的最后一种颜色。在这种情况下,图例中的颜色都是绿色。我缺少什么?代码如下:
ggplot()+
geom_point(data = Q1Shipments, mapping = aes(x=Year, y=Shipments)) + geom_line(data = Q1Shipments, mapping = aes(x=Year, y=Shipments), color="blue", show.legend = TRUE) +
geom_point(data = Q2Shipments, mapping = aes(x=Year, y=Shipments)) + geom_line(data = Q2Shipments, mapping = aes(x=Year, y=Shipments), color="purple", show.legend = TRUE) +
geom_point(data = Q3Shipments, mapping = aes(x=Year, y=Shipments)) + geom_line(data = Q3Shipments, mapping = aes(x=Year, y=Shipments), color="orange", show.legend = TRUE) +
geom_point(data = Q4Shipments, mapping = aes(x=Year, y=Shipments)) + geom_line(data = Q4Shipments, mapping = aes(x=Year, y=Shipments), color="green", show.legend = TRUE) +
ylim(3500, 5000) + theme(axis.text.x = element_text(angle = 45, size = 15)) +
ggtitle("Shipments Per Quarter By Year") + theme(axis.title = element_text(size = 20)) +
theme(plot.title = element_text(size = 30)) + theme(axis.text.y = element_text(angle = 45, size = 15)) +
scale_color_manual(name="Quarters", breaks=c("Q1Shipments", "Q2Shipments", "Q3Shipments", "Q4Shipments"), values=c("Q1Shipments"="blue", "Q2Shipments"="purple", "Q3Shipments"="orange", "Q4Shipments"="green"))
I created a line graph with a series of data using ggplot in RStudio. My legend is only showing one color, always the last one where the geom_line used show.legend=TRUE. In this case the colors in the legend are all green. What am I missing? Here is the code:
ggplot()+
geom_point(data = Q1Shipments, mapping = aes(x=Year, y=Shipments)) + geom_line(data = Q1Shipments, mapping = aes(x=Year, y=Shipments), color="blue", show.legend = TRUE) +
geom_point(data = Q2Shipments, mapping = aes(x=Year, y=Shipments)) + geom_line(data = Q2Shipments, mapping = aes(x=Year, y=Shipments), color="purple", show.legend = TRUE) +
geom_point(data = Q3Shipments, mapping = aes(x=Year, y=Shipments)) + geom_line(data = Q3Shipments, mapping = aes(x=Year, y=Shipments), color="orange", show.legend = TRUE) +
geom_point(data = Q4Shipments, mapping = aes(x=Year, y=Shipments)) + geom_line(data = Q4Shipments, mapping = aes(x=Year, y=Shipments), color="green", show.legend = TRUE) +
ylim(3500, 5000) + theme(axis.text.x = element_text(angle = 45, size = 15)) +
ggtitle("Shipments Per Quarter By Year") + theme(axis.title = element_text(size = 20)) +
theme(plot.title = element_text(size = 30)) + theme(axis.text.y = element_text(angle = 45, size = 15)) +
scale_color_manual(name="Quarters", breaks=c("Q1Shipments", "Q2Shipments", "Q3Shipments", "Q4Shipments"), values=c("Q1Shipments"="blue", "Q2Shipments"="purple", "Q3Shipments"="orange", "Q4Shipments"="green"))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论