1

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");

1 Answer 1

7

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Seriously? The OP basically says, "I haven't even tried to do this in ggplot2, show me how!" and you oblige? Really?
Because one of the explicit requirements on this site is that the OP has made at least a good faith attempt to solve their own problem. SO is not a place to come to get "starting from zero" tutorials.
Thanks for the great and very quick response. This will get my a long way. @Joran I did try to this in ggplot2 although completely unsuccessfully. What was meaned by "The completely new to ggplot2" was: "I have been trying this for three hours and it is the first time I use ggplot2 "
Look, you're free to do what you please. (And you have no way of knowing that was me that downvoted, and in fact it wasn't!) But answering these sorts of questions teaches people that they needn't bother to put any effort in whatsoever.
@MrOperator That's fine. Next time, include your (failed) attempt in your question.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.