R-ggplot:一个坐标系中的五个图

时间:2018-04-25 14:38:41

标签: r ggplot2

我在一小时前问了类似的问题,得到了一些不错的答案,但不是我要找的那个,很可能是因为我的问题没有以正确的方式表达。这是我再次发布的方式  我在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")

有人知道这有用吗?非常感谢:)菲尔

我已经在一页上对它们进行了比较。现在我需要将所有坐标系统集成在一起 enter image description here

2 个答案:

答案 0 :(得分:3)

考虑堆叠(即行绑定)所有数据帧,向每个数据帧添加一个指示符变量,如 type ,然后用颜色绘制映射到指示变量,甚至定义手动颜色:

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"))

答案 1 :(得分:0)

我不确定我是否理解你的问题,但如果你想把你的五行放到一个地块中,你可以做到以下几点:

p <- ggplot(merch42, aes(x = day_code, y = avg_logistic_review_score)) +
geom_smooth(method = "loess", span = 1/25, col = "red") + 
geom_smooth(merch323, aes(x = day_code, y = avg_logistic_review_score),
method = "loess", span = 1/25, col = "blue")+
geom_smooth(merch24, aes(x = day_code, y = avg_logistic_review_score),
method = "loess", span = 1/25, col = "green") + 
geom_smooth(merch180, aes(x = day_code, y = avg_logistic_review_score),
method = "loess", span = 1/25, col = "yellow") + 
geom_smooth(merch505, aes(x = day_code, y = avg_logistic_review_score), 
method = "loess", span = 1/25, col = "black")