使用ggplot2循环后使用grid_extra

使用ggplot2循环后使用grid_extra,r,R,我必须在我的数据上运行许多GGPLOT,所以我尝试循环变量。我想使用grid_extra将地块放置在网格排列中。我已经成功地为循环编写了代码,以便为我的列表创建绘图,但不确定如何将其扩展到grid extra。这是我的密码: data("mtcars") mtcars$gear=as.factor(mtcars$gear) lflist=list("mpg", "hp", "drat", "wt") lfplot=list() for(i in seq_along(lflist)) { l

我必须在我的数据上运行许多GGPLOT,所以我尝试循环变量。我想使用grid_extra将地块放置在网格排列中。我已经成功地为循环编写了代码,以便为我的列表创建绘图,但不确定如何将其扩展到grid extra。这是我的密码:

data("mtcars")
mtcars$gear=as.factor(mtcars$gear)

lflist=list("mpg", "hp", "drat", "wt")
lfplot=list()
for(i in seq_along(lflist)) {
  lfplot=ggplot(data=subset(mtcars, !is.na(gear)), aes(x=gear, 
  y=lflist[i]))+geom_boxplot()+
  stat_summary(fun.y=median, geom="line", aes(group=1), colour="red")+
  stat_summary(fun.y=median, geom="point", colour="red")+
  theme(axis.text.x=element_text(size=8),axis.title.x = element_blank())

}

我会使用
cowplot

data("mtcars")
mtcars$gear=as.factor(mtcars$gear)

lflist=list("mpg", "hp", "drat", "wt")
lfplot=list()
for(i in seq_along(lflist)) {
    lfplot[[i]] <- ggplot(data=subset(mtcars, !is.na(gear)), aes_string(x="gear",y=lflist[[i]])) + geom_boxplot() +
    stat_summary(fun.y=median, geom="line", aes(group=1), colour="red")+
    stat_summary(fun.y=median, geom="point", colour="red")+
    theme(axis.text.x=element_text(size=8),axis.title.x = element_blank())
}

library(cowplot)

plot_grid(plotlist = lfplot)
数据(“mtcars”)
mtcars$档位=平均系数(mtcars$档位)
lflist=列表(“mpg”、“hp”、“drat”、“wt”)
lfplot=list()
对于(沿(lflist)顺序排列的i){

lfplot[[i]]我会使用
cowplot

data("mtcars")
mtcars$gear=as.factor(mtcars$gear)

lflist=list("mpg", "hp", "drat", "wt")
lfplot=list()
for(i in seq_along(lflist)) {
    lfplot[[i]] <- ggplot(data=subset(mtcars, !is.na(gear)), aes_string(x="gear",y=lflist[[i]])) + geom_boxplot() +
    stat_summary(fun.y=median, geom="line", aes(group=1), colour="red")+
    stat_summary(fun.y=median, geom="point", colour="red")+
    theme(axis.text.x=element_text(size=8),axis.title.x = element_blank())
}

library(cowplot)

plot_grid(plotlist = lfplot)
数据(“mtcars”)
mtcars$档位=平均系数(mtcars$档位)
lflist=列表(“mpg”、“hp”、“drat”、“wt”)
lfplot=list()
对于(沿(lflist)顺序排列的i){

lfplot[[i]]给你。我们还在内联评论中添加了一些提示。如果有任何不清楚的地方,请告诉我们

library(ggplot2)

data("mtcars")
mtcars$gear=as.factor(mtcars$gear)

# In such simple cases, it is advisable to use vectors rather than list
# lflist = list("mpg", "hp", "drat", "wt") 
lflist = c("mpg", "hp", "drat", "wt")
lfplots = list()

for(i in seq_along(lflist)) { # Hint, you can loop directry over the entries (for element in lflist)
  # Create your plot
  lfplot = ggplot(data=subset(mtcars, !is.na(gear)), aes(x=gear, y=lflist[i])) + 
    geom_boxplot()+
    stat_summary(fun.y=median, geom="line", aes(group=1), colour="red") +
    stat_summary(fun.y=median, geom="point", colour="red") +
    theme(axis.text.x=element_text(size=8),
          axis.title.x = element_blank())

  # Add your plot to the list
  lfplots[[length(lfplots) + 1]] = lfplot
}


library(gridExtra)
grid.arrange(grobs = lfplots, nrow = 2, ncol = 2)

给你。我们还在内联评论中添加了一些提示。如果有任何不清楚的地方,请告诉我们

library(ggplot2)

data("mtcars")
mtcars$gear=as.factor(mtcars$gear)

# In such simple cases, it is advisable to use vectors rather than list
# lflist = list("mpg", "hp", "drat", "wt") 
lflist = c("mpg", "hp", "drat", "wt")
lfplots = list()

for(i in seq_along(lflist)) { # Hint, you can loop directry over the entries (for element in lflist)
  # Create your plot
  lfplot = ggplot(data=subset(mtcars, !is.na(gear)), aes(x=gear, y=lflist[i])) + 
    geom_boxplot()+
    stat_summary(fun.y=median, geom="line", aes(group=1), colour="red") +
    stat_summary(fun.y=median, geom="point", colour="red") +
    theme(axis.text.x=element_text(size=8),
          axis.title.x = element_blank())

  # Add your plot to the list
  lfplots[[length(lfplots) + 1]] = lfplot
}


library(gridExtra)
grid.arrange(grobs = lfplots, nrow = 2, ncol = 2)

我在尝试绘制其中一个图形时出错:
错误:stat_boxplot需要以下缺少的美学:y
您在哪里定义
y
?在绘制图形列表之前,您确定可以绘制每个图形吗?感谢您的回复。我在
lflist
中定义了我的y-这些是我正在循环的变量。我是-r我想@Masoud是对的。我必须使用
aes_string
修改你的代码,如下所示。@Lyngbakr我还想添加
as.character(lflist[I])
为了确保它能与所有版本的
ggplot
一起工作,我在尝试绘制其中一个版本时出错:
错误:stat\u boxplot需要以下缺失的美学:y
您在哪里定义
y
?在创建绘图列表之前,您确定可以绘制每个版本吗?感谢您的回复。我定义了我的y在
lflist
中-这些是我正在循环的变量。我重新运行了代码,没有出现错误。我认为@Masoud是正确的。我必须使用
aes\u string
修改您的代码,如下所示。@Lyngbakr我还将添加
作为.character(lflist[I])
确保它能与所有版本的
ggplot一起工作
非常感谢!工作完美。非常感谢!工作完美。