How do you combine multiple boxplots from a List of data-frames?
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.
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, 2022Comments
-
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 about 13 yearsCan't you just melt the list?
-
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 about 13 yearsindeed yes, it is the followup to that question. Thank you for the edit.
-
USER_1 about 8 yearsThis solution will not work if it is to plot directly from a list.
-
Diomidis Spinellis about 8 yearsNote that you also need to add "library(reshape)" for this to work.
-
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.