Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple boxplots using ggplot

Tags:

r

ggplot2

boxplot

I have a dataframe that looks like the one attached, with 6 columns and 1000 rows (tab separated). The column headings (0,30,60,120,240 and 360) are a time series (with 0 representing 0 mins, 30 meaning 30 mins and so on). I'd like to create 6 boxplots corresponding to the columns using ggplot2 in a single plot, keeping in mind that they need to be spaced based on the time difference. It seems I would need to melt the columns, but cant figure out a way to do so. Any help would be much appreciated.

        0       30       60       120      240     360
1       1       NA       NA       NA       1       1
2       NA      NA       NA       NA       NA      NA
3       NA      NA       1        1        1       1
4       0.5     0.21     0.15     1        0.38    0.8
5       0.63    1        0.86     0.85     0.94    0.91
like image 429
user2701186 Avatar asked Aug 20 '13 19:08

user2701186


People also ask

How do you make a Boxplot with multiple groups in R?

Box plot for multiple groups In order to create a box plot by group in R you can pass a formula of the form y ~ x , being x a numerical variable and y a categoriacal variable to the boxplot function. You can pass the variables accessing the data from the data frame using the dollar sign or subsetting the data frame.


1 Answers

Did you try just using melt?

library(reshape2)
ggplot(melt(df), aes(variable, value)) + geom_boxplot()

enter image description here

like image 88
Señor O Avatar answered Sep 22 '22 22:09

Señor O