Reputation: 71
I'd like to create a boxplot from two different dataframes in R. In each dataframe, the rows represent samples. While the columns represent diseases. The boxplot distribution should be made from the values in each row. The data is supposed to show the comparison between the row distributions in each data frame(control, experimental group). So if there are 6 rows in each data frame, there should be 12 boxes.
It should look something like this. https://i.sstatic.net/17OIk.png
Both data frames have the same number of rows, but a different number of columns, since the experimental conditions were different. I'd also like the plots to be reordered by the row median of only one of the data frames, and this order should be preserved for the entirety of the box plot.
Any ideas?? I'm new to R and would appreciate any leads.
Upvotes: 2
Views: 9037
Reputation: 56
df1 <- data.frame(disease.a=rnorm(10,2),
disease.b=rnorm(10,2),
disease.c=rnorm(10,2)) # experimental group
df2 <- data.frame(disease.a=rnorm(10,0),
disease.b=rnorm(10,0),
disease.c=rnorm(10,0)) # control group
df1$condition <- "experimental"
df2$condition <- "control"
df3 <- rbind(df1, df2)
library(reshape2)
m.df <- melt(df3, id.var="condition")
library(ggplot2)
ggplot(m.df, aes(x=condition, y=value)) + geom_boxplot(aes(fill=variable))
Upvotes: 3