在ggplot2中使用for循环来生成一个列表 [英] Use for loop in ggplot2 to generate a list
本文介绍了在ggplot2中使用for循环来生成一个列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望使用 for循环
来生成 ggplots
的列表。
I would like to use for loop
to generate a list of ggplots
.
sig_snp<-c("rs644045_A","rs12997044_C","rs17739727_A")
p_re<-list()
for ( i in sig_snp){
test %>% dplyr::select(i,type,micro1) %>%
ggplot(aes(factor(type),log(micor1) )) +
geom_boxplot(aes(fill = factor(i)))+
xlab('')+ylab('log abundance')->p_re[[i]]
}
错误显示如下:
The erro shows below:
错误:所有select()输入都必须解析为整数列位置。
以下不是:
* i
Error: All select() inputs must resolve to integer column positions. The following do not: * i
我测试了每个 i
在for循环中以这种方式:
I have tested each i
in the for loop in this way:
test %>% dplyr::select(rs644045_A,type,micro1) %>%
ggplot(aes(factor(type),log(micor1) )) +
geom_boxplot(aes(fill = factor(rs644045_A)))+
xlab('')+ylab('log abundance')
它单独工作,但为什么不工作循环?
It worked singly, but why not work in the loop?
推荐答案
如果您需要在列表中保留每个SNP的ggplot输出,最好使用lapply,输出列表,例如:
If you need to keep ggplot output per SNP in a list, it is maybe better to use lapply which will output list, e.g.:
library(ggplot2)
#dummy data
test <- mtcars
#significant SNPs
sig_snp <- c("mpg", "cyl", "disp")
#create list of ggplots per SNP
p_re <-
lapply(sig_snp, function(i){
ggplot(test, aes_string("qsec", i)) +
geom_point()
})
#assign names
names(p_re) <- sig_snp
#plot
p_re$mpg
这篇关于在ggplot2中使用for循环来生成一个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文