16

I have a data frame my.df of the following structure:

   A B  C
1  1 1  2
2  2 3  4
3  3 5  6
4 NA 7  8
5 NA 9 NA

How to build a box plot from it with column names on x axis and all the values on y?

There are many answers like:

ggplot(melt(my.df), aes(variable, value)) + geom_boxplot()

But I don't understand, what I actually should pass as "variable" and "value". I tried x=colnames(my.df)) and this partially works, however I still have no idea what to do with y.

Roman
  • 2,225
  • 5
  • 26
  • 55
  • Look at the output when you use `melt(df)` and you will see what is meant by "variable" and "value". (you have to load reshape2 for that to work). – talat Nov 24 '14 at 16:52

1 Answers1

36

You can use stack to transform the data frame:

library(ggplot2)
ggplot(stack(df), aes(x = ind, y = values)) +
  geom_boxplot()

enter image description here

Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168
  • Thanks, this works. But do you also know, how then to keep the order of columns? They are sorted alphabetically, however, I would like to preserve the existing order from original data frame (for example, B A C). – Roman Nov 25 '14 at 12:15
  • 2
    @Roman `ggplot(stack(df), aes(x = factor(ind, levels = names(df)), y = values)) + geom_boxplot()` – Sven Hohenstein Nov 25 '14 at 12:23
  • And how could you order it by values? I mean, from the lower the boxplot to the highest? – Jeni May 30 '20 at 08:25
  • 1
    You can reorder the df `df <- df %>% relocate(B, .before = A)` – Harley Apr 09 '21 at 05:24