简体   繁体   中英

loop for generating multiple QQ-plot for normal distribution

I have a data set with multiple variables and hundreds of entries. The dataframe is like this (simple)

> my_Data
# A tibble: 9 x 3
  Gene  Time  Expression
  <chr> <chr>      <dbl>
1 Gene1 W1         18.8 
2 Gene2 W1         13.9 
3 Gene3 W1         20.9 
4 Gene1 W2          9.29
5 Gene2 W2         10.9 
6 Gene3 W2         12.2 
7 Gene1 W3         13.8 
8 Gene2 W3         23.9 
9 Gene3 W3         17.4 
> 

I need to test for normal distribution for each combination of variables. I can do this using looping the qqnorm, it works perfectly and generates the plots that I need, but the main title of all plots are the same (here I used main = "myTitle" ) and after exporting I cannot correlate each plot to which subsets.

here is my code

attach(my_Data)
my_qq = list()
for (ids in unique(my_Data$Gene)){
  sub_Data = subset(x=my_Data, subset=Gene==ids)

  my_qq[[ids]] = qqnorm(sub_Data$Expression, main = "myTitle", pch=19) 
  qqline(sub_Data$Expression, col="red", lty =2, lwd = 3)
}

1)Is there anyway that each one of my plots have a different title? 2) Can I save them all as separate plots?

my_Data <- data.frame(Gene=rep(c("Gene1", "Gene2", "Gene3"), 3), 
                      Time=rep(c("W1", "W2", "W3"), each=3), 
                      Expression=c(18.8, 13.9, 20.9, 9.29, 10.9, 12.2, 13.8, 23.9, 17.4))

for (id in unique(my_Data$Gene)){
  sub_Data <- my_Data[which(my_Data$Gene == id), ]$Expression

  pdf(paste0(id, ".pdf"))
  qqnorm(sub_Data, main=paste("Gene =", id))
  qqline(sub_Data, col="red", lty =2, lwd = 3)
  dev.off()
}

pdf() saves your image into the current working directory. Check this with getwd() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM