0

I've been trying to do this boxplot now for a whole day. I give up soon. Please help. I just want a simple boxplot for my 3 groups, A, B and C. This is my data simplified:

> mydata 
   A01 A02 A03 B01 B02 B03 C01 C02 C03
id0001    1    2    3    6    7    8    11    12    13

A01, A02 and A03 should make up the confidence interval for A, etc. How can I have a very simple boxplot with the 3 groups on the x-axis? Although boxes would be great too, I would be so grateful if someone could help me make my plot similar to this one. Eventually, I want to make a graph that contains more than one ID, so it looks like this one, but with error bars like the first one.

If someone could help me or give me tips on how to proceed, I would appreciate it so much!

Thank you.

5
  • 2
    You don't have enough data to draw a box plot. IIRC boxplots only make sense once you have more than 6 observations per group. The data you show has only 3 observations per group. Try a dotplot instead. Commented Nov 6, 2012 at 11:09
  • I do have more data, this was only an example of how an extract of the data look. I have about 6-7 values for each group. Anyway, I do believe it should be possible to plot the confidence intervals or error bars with only 3 data points. The images I linked to are based on 3 replicates for each line (gene), hence only 3 values per group. Commented Nov 6, 2012 at 11:17
  • You seem to be confusing confidence intervals/error bars with a box plot. A box plot is described here whereas a confidence interval is described here. Which do you want? Commented Nov 6, 2012 at 11:36
  • Thank you for sticking with me. You are probably right. I'm learning. Maybe box plots are wrong with this data. How can I view my data with error bars or confidence intervals like the images I linked? Have you any idea? I thought of them like box plots without the boxes. Probably they are something else then. Commented Nov 6, 2012 at 20:01
  • Do you think I should make a new question and remove the boxplot part? Commented Nov 8, 2012 at 17:30

1 Answer 1

5

Assuming you do want a boxplot (or any other plot from ggplot2) you'll need to coerce the data into the correct format. Here is an example based on similar data to that which you show:

df <- data.frame(matrix(c(1:3, 6:8, 11:13), nrow = 1))
names(df) <- paste0(rep(LETTERS[1:3], each = 3), 1:3)

sdf <- stack(df)

sdf <- transform(sdf, group = substring(ind, 1, 1),
                 obs = substring(ind, 2))

The data sdf are now in normalized format (or long format):

> head(sdf)
  values ind group obs
1      1  A1     A   1
2      2  A2     A   2
3      3  A3     A   3
4      6  B1     B   1
5      7  B2     B   2
6      8  B3     B   3

These data can then be easily plotted:

require(ggplot2)
plt <- ggplot(sdf, aes(x = group, y = values)) +
    geom_boxplot()
plt

producing:

enter image description here

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

Comments

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.