Using columns as factors for plotting in R

boxplot, dataframe, factors, ggplot2, r

Solution

It will be much easier if you rearrange your data frame. This can be done with the `reshape2` package:

library(reshape2)
DOData2 <- melt(DOData)

Now, the creation of the plot is straightforward:

library(ggplot2)
ggplot(DOData2) +
  geom_boxplot(aes(y = value, x = Months, colour = variable))

Problem

I have a data frame that looks like this: ``` > head(DOData) Date Site1 Site2 Site3 Site4 Site5 Months 1 1/1/2012 1.07 3.32 11.35 6.26 5.39 January 2 1/2/2012 1.24 3.08 10.69 6.57 6.59 January 3 1/3/2012 1.94 2.69 11.86 6.23 6.23 January 4 1/4/2012 0.81 3.50 11.47 4.67 5.94 January 5 1/5/2012 1.41 3.11 10.38 7.44 5.40 January 6 1/6/2012 2.73 3.28 11.11 6.15 6.22 January . . . 361 12/26/2012 3.54 3.86 12.67 5.44 6.03 December 362 12/27/2012 2.05 3.42 10.27 6.05 7.10 December 363 12/28/2012 3.59 2.96 11.10 6.71 5.68 December 364 12/29/2012 1.81 3.57 11.20 7.20 7.71 December 365 12/30/2012 4.03 2.00 11.07 7.15 5.93 December 366 12/31/2012 1.93 2.03 11.90 6.06 8.46 December ``` i.e. a year's worth of data for 5 sites, with one row per day. I'd like to create some ggplot2 `boxplot`'s of data for individual months, using the five sites as factors. I suppose I could put all my data into a single column and then add a new column for the site name, but then I'd still need to somehow select the individual months. I wonder, do I really need to reorganize my data, or is there some way to use the columns as factors? Thanks for your help!

Original source