首页 > 解决方案 > R- ggplot:一个坐标系中的五个图

问题描述

一小时前我问了类似的问题,得到了一些不错的答案,但不是我要找的答案,可能是因为我的问题没有以正确的方式提出。这就是我再次发布它的方式,我在 ggplot 的帮助下在我的 R-Script 中绘制了不同的图表。为了比较它们,我需要将它们整合到一个图表中。

这是我当前的单图代码:

p1 <- ggplot(merch42, aes(x = day_code, y = avg_logistic_review_score, col = "red"))+   geom_smooth(method = "loess", span = 1/25, col = "red")

p2 <- ggplot(merch323, aes(x = day_code, y = avg_logistic_review_score, col = "blue"))+
  geom_smooth(method = "loess", span = 1/25, col = "blue")

p3 <- ggplot(merch24, aes(x = day_code, y = avg_logistic_review_score, col = "green"))+
  geom_smooth(method = "loess", span = 1/25, col = "green")

p4 <- ggplot(merch180, aes(x = day_code, y = avg_logistic_review_score, col = "yellow"))+
  geom_smooth(method = "loess", span = 1/25, col = "yellow")

p5 <- ggplot(merch505, aes(x = day_code, y = avg_logistic_review_score, col = "merch505"))+
  geom_smooth(method = "loess", span = 1/25, col = "black")

有人知道这是如何工作的吗?非常感谢:)菲尔

在这里,我已经在一页上对它们进行了比较。现在我需要在坐标系统中集成所有内容。
在此处输入图像描述

标签: rggplot2

解决方案


考虑堆叠(即行绑定)所有数据帧,为每个数据帧添加一个指标变量,如类型,然后使用映射到指标变量的颜色进行绘图,甚至定义手动颜色:

final_df <- rbind(transform(merch42, type = "merch42"),
                  transform(merch323, type = "merch323"),
                  transform(merch24, type = "merch24"),
                  transform(merch180, type = "merch180"),
                  transform(merch505, type = "merch505"))

ggplot(final_df, aes(x = day_code, y = avg_logistic_review_score, color = type)) +
  geom_smooth(method = "loess", span = 1/25) +
  scale_color_manual(values = c("red", "blue", "green", "yellow", "black"))

推荐阅读