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:
Error: All select() inputs must resolve to integer column positions. The following do not: * i
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?
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.:
The tricky part is
select_
andget()
:The
get()
answers were from here : Remove quotes from a character vector in RHowever, in my case,it did not work in the loop.I think it is probably due to double loops in my code (I am not sure).
Anyway, there is an alternative way to make it:
I got the idea from here Looping over variables in ggplot