1

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?

Lionette
  • 83
  • 1
  • 8
  • Consider changing the title using `qqnorm(... , main = myTitle)`. For example `main = paste0("ID = ", ids)` would generate title that specify each ID. – Oliver Oct 09 '19 at 19:13
  • I tried, it did not work. `my_qqplot = list() for (ids in unique(my_data$variable1)){ sub_my_qqplot = subset(x=my_qqplot, subset=variable1==ids) myTitle= paste0("ID = ", ids) my_qqplot[[ids]] = qqnorm(sub_my_qqplot2$value, pch=19, main = myTitle) qqline(sub_my_qqplot$value, col="red", lty =2, lwd = 3) }` – Lionette Oct 09 '19 at 19:45
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Oct 09 '19 at 20:24
  • I updated my question with an example. – Lionette Oct 09 '19 at 22:03

1 Answers1

1
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().

Kidae Kim
  • 499
  • 2
  • 9