0

I have a dataset that is something like this:

A, B, C, D, E
1, 2, 3, 4, 5
2, 3, 4, 5, 6
3, 4, 5, 6, 7
...

I want to make a ggplot2 graph in R that is a boxplot where each box corresponds to a letter. According to what I researched, the way to do this is having something like this as dataset:

Letter, Value
A, 1
A, 2
A, 3
...
B, 2
B, 3
B, 4
...
...
E, 5
E, 6
E, 7

The original dataset doesn't need to have the same amount of elements for each box actually. Is there any way to a) do the boxplot with the original dataset without changing it, using ggplot2 (not the builtin boxplot of R), this is what I really want, if there is no way, then: b) transform one dataset to another with R?

Thank you!

P.S. (I don't know if I can ask this in the same question, if no, I am very sorry) If you know any good tutorial for beginners in ggplot2, that actually teaches how to use ggplot instead of qplot, I would appreciate it a lot. Thank you again!

crscardellino
  • 261
  • 5
  • 16
  • 1
    what about ggplot(data=yourdataset, aes(x=Letter, y=Value) + geom_boxplot()? – MLavoie Jan 04 '16 at 23:46
  • you will need to convert your data into long form as you identified in your question. This is easy using the reshape package and the melt function. when in long form just use the code that @MLavoie posted and it should work – Tom Jan 04 '16 at 23:47

1 Answers1

0

Try this code:

library(tidyr)
library(ggplot2)

df <- data.frame(A = c(1, 2, 3, 4), B = c(2, 3, 4, 5), C = c(3, 4, 5, 6), D = c(4, 5, 6, 8), E = c(5, 6, 7, 8))
df <- gather(df, Letter, Value)
ggplot(df, aes(x = Letter, y = Value)) + geom_boxplot()

Gather in tidyr allows you to convert multiple columns into categorical values as you can see by displaying the modified data frame.

Gopala
  • 10,363
  • 7
  • 45
  • 77