How do you combine multiple boxplots from a List of data-frames?

14,229

Solution 1

Sam,

I'm assuming this is a follow up to this question? Maybe your sample data will illustrate the nuances of your needs better (the "categorized over attributes in another column" part), but the same melting approach should work here.

library(ggplot2)
library(reshape2)
#Fake data
a <- data.frame(a = rnorm(10))
b <- data.frame(b = rnorm(100))
c <- data.frame(c = rnorm(1000))

#In a list
myList <- list(a,b,c)


#In a melting pot
df <- melt(myList)

#Separate boxplots for each data.frame
qplot(factor(variable), value, data = df, geom = "boxplot")
#All values plotted together as one boxplot
qplot(factor(1), value, data = df, geom = "boxplot")

Solution 2

a<-data.frame(c(1,2),c("x","y"))
b<-data.frame(c(3,4,5),c("a","b","c"))
boxplot(c(a[1],b[1]))

With the "1"'s i select the column i want out of the data-frame.

A data-frames can not have different column-lengths (has to have same number of rows for each column), but you can tell boxplot to plot multiple datasets in parallel.

Share:
14,229
Sam
Author by

Sam

I am a Associate Research Scientist, and I work for the Yale Center for Genome Analysis. I help people at Yale make sense of the data generated using high-throughput sequencing methods.

Updated on June 04, 2022

Comments

  • Sam
    Sam almost 2 years

    This is a repost from the Statistics portion of the Stack Exchange. I had asked the question there, I was advised to ask this question here. So here it is.

    I have a list of data-frames. Each data-frame has a similar structure. There is only one column in each data-frame that is numeric. Because of my data-requirements it is essential that each data-frame has different lengths. I want to create a boxplot of the numerical values, categorized over the attributes in another column. But the boxplot should include information from all the data-frames.

    I hope it is a clear question. I will post sample data soon.

  • hadley
    hadley about 13 years
    Can't you just melt the list?
  • Chase
    Chase about 13 years
    @hadley - indeed you can. I hadn't used melt specifically with lists before, so thanks for the tip. Will modify accordingly.
  • Sam
    Sam about 13 years
    indeed yes, it is the followup to that question. Thank you for the edit.
  • USER_1
    USER_1 about 8 years
    This solution will not work if it is to plot directly from a list.
  • Diomidis Spinellis
    Diomidis Spinellis about 8 years
    Note that you also need to add "library(reshape)" for this to work.
  • Chase
    Chase about 8 years
    @DiomidisSpinellis - thanks, added that. Back in 2011 when this was answered, reshape was loaded by default when you loaded ggplot2...that's no longer the case so I updated the code.