R中两个数据框的箱线图

boxplots from two dataframes in R

我想根据 R 中的 两个不同的数据帧 创建一个箱线图。在每个数据帧中,行代表样本。而列代表疾病。箱线图分布应该由每一行中的值组成。数据应该显示每个数据帧(控制组、实验组)中行分布之间的比较。所以如果每个数据框有6行,那么应该有12个框。

它应该看起来像这样。 https://i.stack.imgur.com/17OIk.png

两个数据框的行数相同,但列数不同,因为实验条件不同。我还希望按仅 一个 数据帧的行中位数对图进行重新排序,并且应为整个箱形图保留此顺序。

有什么想法吗??我是 R 的新手,非常感谢任何线索。

生成一些示例数据

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 和 df2 中添加一列来表示实验条件

df1$condition <- "experimental"
df2$condition <- "control"

将您的数据框绑定在一起

df3 <- rbind(df1, df2)

重塑数据

library(reshape2)
m.df <- melt(df3, id.var="condition")

根据您的示例使用 ggplot 绘制数据

library(ggplot2)
ggplot(m.df, aes(x=condition, y=value)) + geom_boxplot(aes(fill=variable))