如何使用for函数在R中构造ggplot2绘图的面板

如何使用for函数在R中构造ggplot2绘图的面板,r,plot,ggplot2,R,Plot,Ggplot2,我是一个试图在ggplot2中构建多个情节的初学者。在R中使用mtcars数据集 library(datasets) data (mtcars) library (ggplot2) ## convert to factor some variables to avoid problems factors<-c(2,9,10,11) mtcars[,factors]<-lapply(mtcars[,factors],factor) 库(数据集) 数据(mtcars) 图书馆(GG2)

我是一个试图在ggplot2中构建多个情节的初学者。在R中使用mtcars数据集

library(datasets)
data (mtcars)
library (ggplot2)
## convert to factor some variables to avoid problems
factors<-c(2,9,10,11)
mtcars[,factors]<-lapply(mtcars[,factors],factor)
库(数据集)
数据(mtcars)
图书馆(GG2)
##将某些变量转换为因子以避免出现问题

系数如果您希望绘图位于
g1
g\u n

g <- lapply(variables, function(var) {
  ggplot(mtcars, aes_string(x="mpg", y=var, color="am")) + geom_point(shape=1) 
})
names(g) <- paste0("g", seq(g))
list2env(g, .GlobalEnv)

g执行此操作的常用方法是将数据重新格式化为长格式,例如使用
restrape2::melt()
variables<- c ("cyl","disp","hp","drat","wt","qsec","vs","gear","carb") 
g <- lapply(variables, function(var) {
  ggplot(mtcars, aes_string(x="mpg", y=var, color="am")) + geom_point(shape=1) 
})
names(g) <- paste0("g", seq(g))
list2env(g, .GlobalEnv)