ggplot将不同的图形合二为一 [英] ggplot combine different graphs in one

查看:94
本文介绍了ggplot将不同的图形合二为一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

借助于ggplot,我在R脚本中绘制了不同的图形. 为了比较它们,我需要将它们集成到一张图中.

I have plotted different graphs in my R-Script with the help of ggplot. To compare them I need to integrate them into one graph.

这是我当前用于单个图形的代码:

this is my current code for the single graphs:

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

有人对此有想法吗? 非常感谢:)菲尔

Has someone an idee how this works? thanks very much:) Phil

推荐答案

使用库gridExtra中的函数grid.arrange().最低要求在下面给出;

Use the function grid.arrange() from the library, gridExtra. A minimum reprex is given below;

library(ggplot2)
library(gridExtra)

p1 <- qplot(mpg, wt, data = mtcars, colour = cyl)
p2 <- qplot(mpg, data = mtcars) + ggtitle("title")
p3 <- qplot(mpg, data = mtcars, geom = "dotplot")
p4 <-
  p1 + facet_wrap( ~ carb, nrow = 1) + theme(legend.position = "none") +
  ggtitle("facetted plot")

# grid.arrange(p1, p2, nrow = 1)
grid.arrange(p1, p2, p3, p4, nrow = 2, ncol=2)

这篇关于ggplot将不同的图形合二为一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆