scale_color_manual 在 ggplot 中仅显示一种颜色

发布于 2025-01-17 04:22:17 字数 1443 浏览 3 评论 0原文

我使用 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"))

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文