2

I am very new to R. I have figured out how to make qqnorm plots on a subset of my dataframe. However, I would like to make qqnorm plots on subsets that are defined by two factors (one factor has 48 categories (brain_region) and each of those categories can be further subdivided by another factor, which has three levels (GroupID)). I have tried the following:

by(t, t[,"GroupID"], function(x) tapply(t$FA,t$brain_region,qqnorm))

but it does not seem to be working. I'm also not sure if this is the best approach, as I'm new to this program.

I would also like to save each of the separately generated qqnorm plot with the x axis as labeled as "FA" and the title with the specific level of each of the two factors (brain region/GroupID). Thank you very much for any help.

Henrik
  • 65,555
  • 14
  • 143
  • 159
user3136034
  • 23
  • 1
  • 3
  • 1
    Hi and welcome to SO! [**This**](http://stackoverflow.com/questions/19599745/qqline-in-ggplot2-with-facets/19600903#19600903) may be a start. For next time, please note that people are generally much more happy to help if you provide a [minimal, reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) together with the code you have tried. Thanks! – Henrik Dec 26 '13 at 09:31

1 Answers1

2

Plotting is one of the few things where apply isn't the optimal solution. ggplot offers you enough possibilities to get this done, as shown in this answer.

Plotting all levels in one go

If you use the base plots, you can better use a for loop for this. Plus, if you want to plot different plots on the same graphics device, you can use eg par(mfrow=) or layout (see the help page ?layout)

Let's take the built-in data set iris as an example:

data(iris)

op <- par(mfrow=c(1,3))
for(i in levels(iris$Species)){
  tmp <- with(iris, Petal.Width[Species==i])
  qqnorm(tmp,xlab="Petal.Width",main=i)
  qqline(tmp)
}
par(op)
rm(i,tmp)

gives :

enter image description here

Don't forget to clean up your workspace after using a for loop. Not really obligatory, but it can prevent serious confusion later on.

Combine two factors

In order to get this done for 2 factor levels at the same time, you can either construct a nested for-loop, or combine both factors into a single factor. Take the dataset mtcars:

data(mtcars)
mtcars$cyl <- factor(mtcars$cyl)
mtcars$am <- factor(mtcars$am,
                    labels=c('automatic','manual'))

To combine both levels, you can use this simple construct :

mtcars$combined <- factor(paste(mtcars$cyl,mtcars$am,sep='/'))

And then do the same again. With two for loops, your code would like like the code below. Be warned though that this only works if you have data for every combination of the factors, and you don't have too many levels. If you have a lot of levels, you better save the plots by using eg png() (see ?png for info) instead of plotting them all on the same graphics device.

lcyl <- levels(mtcars$cyl)
lam <- levels(mtcars$am)

par(mfrow=c(length(lam),length(lcyl)))

for(i in lam){
  for(j in lcyl){
    tmp <- with(mtcars,mpg[am==i & cyl==j])
    qqnorm(tmp,xlab="Petal.Width",
           main=paste(i,j,sep="/"))
    qqline(tmp)    
  }
}

gives :

enter image description here

Community
  • 1
  • 1
Joris Meys
  • 106,551
  • 31
  • 221
  • 263