简体   繁体   English

用于为正态分布生成多个 QQ 图的循环

[英]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) dataframe是这样的(简单)

> 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.我可以使用循环 qqnorm 来做到这一点,它可以完美地工作并生成我需要的图,但是所有图的主标题都是相同的(这里我使用main = "myTitle" )并且在导出后我无法将每个 plot 关联到哪个子集。

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? 1)无论如何,我的每一个情节都有不同的标题吗? 2) Can I save them all as separate plots? 2)我可以将它们全部保存为单独的图吗?

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. pdf()将您的图像保存到当前工作目录中。 Check this with getwd() .getwd()检查这个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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