MrOperator
MrOperator

Reputation: 231

Boxplots using ggplot2

I am completely new to using ggplot2 but heard of it's great plotting capabilities. I have a list with of different samples and for each sample observations according to three instruments. I would like to turn that into a figure with boxplots. I cannot include a figure but the code to make an example figure is included below. The idea is to have for each instrument a figure with boxplots for each sample.

In addition, next to the plots I would like to make a sort of legend giving a name to each of the sample numbers. I have no idea on how to start doing this with ggplot2.

Any help will be appreciated

The R-code to produce the example image is:

#Make data example
Data<-list();
Data$Sample1<-matrix(rnorm(30),10,3);
    Data$Sample2<-matrix(rnorm(30),10,3);
Data$Sample3<-matrix(rnorm(30),10,3);
    Data$Sample4<-matrix(rnorm(30),10,3);

#Make the plots
par(mfrow=c(3,1)) ;
boxplot(data.frame(Data)[seq(1,12,by=3)],names=c(1:4),xlab="Sample number",ylab="Instrument 1");
boxplot(data.frame(Data)[seq(2,12,by=3)],names=c(1:4),xlab="Sample number",ylab="Instrument 2");
boxplot(data.frame(Data)[seq(3,12,by=3)],names=c(1:4),xlab="Sample number",ylab="Instrument 3");

Upvotes: 1

Views: 7015

Answers (1)

Jonathan Christensen
Jonathan Christensen

Reputation: 3866

First, you'll want to set your data up differently: as a data.frame rather than a list of matrices. You want one column for sample, one column for instrument, and one column for the observed value. Here's a fake dataset:

df <- data.frame(sample = rep(c("One","Two","Three","Four"),each=30), 
                 instrument = rep(rep(c("My Instrument","Your Instrument","Joe's Instrument"),each=10),4),
                 value = rnorm(120))

> head(df)
  sample    instrument       value
1    One My Instrument  0.08192981
2    One My Instrument -1.11667766
3    One My Instrument  0.34117450
4    One My Instrument -0.42321236
5    One My Instrument  0.56033804
6    One My Instrument  0.32326817

To get three plots, we're going to use faceting. To get boxplots we use geom_boxplot. The code looks like this:

ggplot(df, aes(x=sample,y=value)) + 
  geom_boxplot() + 
  facet_wrap(~ instrument, ncol=1)

enter image description here

Rather than including a legend for the sample numbers, if you put the names directly in the sample variable it will print them below the plots. That way people don't have to reference numbers to names: it's immediately clear what sample each plot is for. Note that ggplot puts the factors in alphabetical order by default; if you want a different ordering you have to change it manually.

Upvotes: 7

Related Questions