boxplot from row values in a dataframe

17,424

Solution 1

You had thought through the problem 95% of the way there. You were just missing that you needed to create a column out of the row.names(). Something like this should work assuming your data is stored in a data.frame named dat

library(ggplot2)
dat$group <- row.names(dat)
dat.m <- melt(dat, id.vars = "group")
ggplot(dat.m, aes(group, value)) + geom_boxplot()

enter image description here

Solution 2

Although you have already accepted the answer from @Chase, I show you a more simply way:

 boxplot(t(dat))
Share:
17,424
darked89
Author by

darked89

Updated on June 17, 2022

Comments

  • darked89
    darked89 almost 2 years

    I got following data frame (simplified here):

        H2475  H2481  H2669  H2843  H2872  H2873  H2881  H2909
    E1 24.470 26.481 15.120 18.490 16.189 11.422 14.886 18.512
    E2  1.016  0.363  0.509  1.190  1.855  0.958  0.771  0.815
    E3  0.671  0.637  0.571  0.447  0.116  0.452  0.403  0.753
    E4  3.448  2.826  2.183  2.607  4.288  2.526  2.820  3.523
    E5  2.548  1.916  1.126  1.553  1.089  1.228  0.887  1.065
    

    where E1 - E5 are row numbers. I would like to create a boxplot (or even better violin plot) for the values of each row. I want to ignore column IDs, and have row IDs as "factors".

    Something like this top answer: How to generate boxplot

    but with E1 - E5 as values. As you may guess I am new to melt/cast/reshape.

    Thank you very much for your help