5

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

1 Answer 1

8

Did you try just using melt?

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

enter image description here

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

4 Comments

Thanks Senor. That worked with plotting. But what about changing the width of x axis ticks? I'd like 0,30 and 60 to be much closer (since they are only 30 min apart), while the later time points spread out even more as they have a wider time gap between them.
You'll need to convert df$variable to numeric (it's a factor right now), and then have aes(..., group=variable) in ggplot. See for example: stackoverflow.com/questions/10805643/…
@Señor O, how can you sort the boxplots of the dataframe by its median, to plot firts the lowest and on the right the highest?
@Jeni so this was 7 years ago but I think you'll have to use factors for your x variable and either assign the median rank to their value/level or just name the variable its median.

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.