使用R在循环中保存ggplot
我有一个带有数字和因子变量的数据集。我想用数字做一页,用因子var做另一页。首先,我用他的索引选择因子var 我的df是IRIS数据集使用R在循环中保存ggplot,r,loops,ggplot2,R,Loops,Ggplot2,我有一个带有数字和因子变量的数据集。我想用数字做一页,用因子var做另一页。首先,我用他的索引选择因子var 我的df是IRIS数据集 df<-iris df$y<-sample(0:1,nrow(iris),replace=TRUE) fact<-colnames(df)[sapply(df,is.factor)] index_fact<-which(names(df)%in%fact) df考虑使用aes\u字符串传递列名以更好地将x与df对齐: 图形 fact &
df<-iris
df$y<-sample(0:1,nrow(iris),replace=TRUE)
fact<-colnames(df)[sapply(df,is.factor)]
index_fact<-which(names(df)%in%fact)
df考虑使用aes\u字符串传递列名
以更好地将x与df对齐:
图形
fact <- colnames(random_df)[sapply(random_df,is.factor)]
index_fact <- which(names(random_df) %in% fact)
i_F=1
i_N=1
list_plotN <- list()
list_plotF <- list()
plot <- NULL
for (i in 1:length(random_df)){
# aes() VERSION
#plot <- ggplot(random_df, aes(x=random_df[,i], color=group, fill=group)) +
# xlab(names(random_df)[i])
# aes_string() VERSION
plot <- ggplot(random_df, aes_string(x=names(random_df)[i], color="group", fill="group")) +
xlab(names(random_df)[i])
if (is.factor(random_df[,i])){
p_factor <- plot + geom_bar()
list_plotF[[i_F]] <- p_factor
i_F=i_F+1
}else{
p_numeric <- plot + geom_histogram()
list_plotN[[i_N]] <- p_numeric
i_N=i_N+1
}
}
fact考虑使用aes\u字符串传递列名
以更好地将x与df对齐:
图形
fact <- colnames(random_df)[sapply(random_df,is.factor)]
index_fact <- which(names(random_df) %in% fact)
i_F=1
i_N=1
list_plotN <- list()
list_plotF <- list()
plot <- NULL
for (i in 1:length(random_df)){
# aes() VERSION
#plot <- ggplot(random_df, aes(x=random_df[,i], color=group, fill=group)) +
# xlab(names(random_df)[i])
# aes_string() VERSION
plot <- ggplot(random_df, aes_string(x=names(random_df)[i], color="group", fill="group")) +
xlab(names(random_df)[i])
if (is.factor(random_df[,i])){
p_factor <- plot + geom_bar()
list_plotF[[i_F]] <- p_factor
i_F=i_F+1
}else{
p_numeric <- plot + geom_histogram()
list_plotN[[i_N]] <- p_numeric
i_N=i_N+1
}
}
事实我并没有那么好地理解你的for循环代码。但从我看来,它似乎是保存在每个循环的最后一个情节。我已经用lapply重建了我认为你需要的东西。只要有可能,我通常更喜欢lappy而不是for循环
Lapply获取一个值列表和一个函数,并将该函数应用于每个值。你可以像我一样单独定义你的函数,这样一切看起来都更干净。然后您只需在lappy命令中提到函数
在我们的例子中,列表是数据帧df
中的列列表。它应用的函数首先创建基本图。然后,它会快速检查所查看的列是否是一个因子。。如果它是一个因子,它会创建一个条形图,否则它会创建一个直方图
histOrBar <- function(var) {
basePlot <- ggplot(df, aes_string(var))
if ( is.factor(df[[var]]) ) {
basePlot + geom_bar()
} else {
basePlot + geom_histogram()
}
}
loDFs <- lapply(colnames(df), histOrBar)
histOrBar我并没有那么好地理解你的for循环代码。但从我看来,它似乎是保存在每个循环的最后一个情节。我已经用lapply重建了我认为你需要的东西。只要有可能,我通常更喜欢lappy而不是for循环
Lapply获取一个值列表和一个函数,并将该函数应用于每个值。你可以像我一样单独定义你的函数,这样一切看起来都更干净。然后您只需在lappy命令中提到函数
在我们的例子中,列表是数据帧df
中的列列表。它应用的函数首先创建基本图。然后,它会快速检查所查看的列是否是一个因子。。如果它是一个因子,它会创建一个条形图,否则它会创建一个直方图
histOrBar <- function(var) {
basePlot <- ggplot(df, aes_string(var))
if ( is.factor(df[[var]]) ) {
basePlot + geom_bar()
} else {
basePlot + geom_histogram()
}
}
loDFs <- lapply(colnames(df), histOrBar)
histOrBar在您的示例中,我看不到y
的任何值这可能有帮助在您的示例中,我看不到y
的任何值这可能真的有帮助吗?你有什么错误?对我来说效果很好。想想看,你的问题还不清楚。它总是有相同的变量。。。这是什么意思?如果没有aes\u string
,x轴在两个图之间不会改变,我假设这是您的同一变量问题。如果您看到list\u plotF(因子变量的预期图形)和list\u plotN(预期图形数值变量),您可以检查这两个列表是否相同。偶数绘图是包含所有绘图的列表。我编辑了显示在上下文中使用aes()
和aes\u string()
的差异。我认为iris
可能不是一个好例子,因为除了物种之外,所有列都是数字。请参阅我所有类型的列的随机数据(为再现性设定种子):char、num、int、bool和date。查看问题和解决方案输出。您使用的是什么IDE?斯图迪奥?RGui?命令行中的Rscript?你是怎么看情节的?真的吗?你有什么错误?对我来说效果很好。想想看,你的问题还不清楚。它总是有相同的变量。。。这是什么意思?如果没有aes\u string
,x轴在两个图之间不会改变,我假设这是您的同一变量问题。如果您看到list\u plotF(因子变量的预期图形)和list\u plotN(预期图形数值变量),您可以检查这两个列表是否相同。偶数绘图是包含所有绘图的列表。我编辑了显示在上下文中使用aes()
和aes\u string()
的差异。我认为iris
可能不是一个好例子,因为除了物种之外,所有列都是数字。请参阅我所有类型的列的随机数据(为再现性设定种子):char、num、int、bool和date。查看问题和解决方案输出。您使用的是什么IDE?斯图迪奥?RGui?命令行中的Rscript?如何查看绘图?是否有可能传递类似df的apply参数@Sahir MoosviThere是一组应用函数。base apply函数可以接受数据帧。是否可以传递apply的类似df的参数@Sahir MoosviThere是一组应用函数。基本应用函数可以接受一个数据帧。
fact <- colnames(random_df)[sapply(random_df,is.factor)]
index_fact <- which(names(random_df) %in% fact)
i_F=1
i_N=1
list_plotN <- list()
list_plotF <- list()
plot <- NULL
for (i in 1:length(random_df)){
# aes() VERSION
#plot <- ggplot(random_df, aes(x=random_df[,i], color=group, fill=group)) +
# xlab(names(random_df)[i])
# aes_string() VERSION
plot <- ggplot(random_df, aes_string(x=names(random_df)[i], color="group", fill="group")) +
xlab(names(random_df)[i])
if (is.factor(random_df[,i])){
p_factor <- plot + geom_bar()
list_plotF[[i_F]] <- p_factor
i_F=i_F+1
}else{
p_numeric <- plot + geom_histogram()
list_plotN[[i_N]] <- p_numeric
i_N=i_N+1
}
}
histOrBar <- function(var) {
basePlot <- ggplot(df, aes_string(var))
if ( is.factor(df[[var]]) ) {
basePlot + geom_bar()
} else {
basePlot + geom_histogram()
}
}
loDFs <- lapply(colnames(df), histOrBar)