一组内的多个箱形图

Vik*_*kas 3 r ggplot2 boxplot

我找到了一些解决方案但不完全是我想要的.我在R中有5个数据帧,每个数据帧有4列:

让我们说第一个数据帧的名称是"Gene1"

Ind1     Ind2       Ind3      Ind4
1          3         3.2        2.5
1          3         4          2
1.5        2         2.2        1
3.4        2         1          3

其余的数据帧称为"Gene2","Gene3","Gene4","Gene5",并且类似.

我想在所有数据帧和所有列的相同图中并排绘制箱线图.我没有找到这样的情节,所以我无法上传图片,但我会尝试解释.

现在从上面的数据来看,该图将有20个箱形图.前4个盒子图应该彼此接近,x轴名称应该是"Gene1"(对于所有4个盒子图)然后在图中有一点空间,再次是4个框图,其中x轴名称为"Gene2",依此类推.

我可以在一个图中轻松绘制所有箱形图,但我无法区分数据框.意思是,它应该清楚地告诉我们前4个箱形图来自"Gene1",接下来的4个箱形图来自"Gene2",依此类推.

如果问题不明确,请告诉我.

Bac*_*lin 11

我怀疑这是你想要的,事实上,标准graphics包中的绘图功能并不是很复杂.这些组被绘制为4个单独的面板,但是在外边距中绘制了共享的y轴和标题,它看起来像一个单独的图.

# Faking the data, since you didn't provide any
Gene <- data.frame(matrix(rweibull(100*4, 1), 100))
names(Gene) <- paste0("Ind", 1:4)
Gene <- rep(list(Gene), 4)

# Setup the panels
layout(t(1:4))
par(oma=c(2, 4, 4, 0), mar=rep(1, 4), cex=1)
# `mar` controls the space around each boxplot group

# Calculating the range so that the panels are comparable
my.ylim <- c(min(sapply(Gene, min)), max(sapply(Gene, max)))

# Plot all the boxes
for(i in 1:length(Gene)){
    boxplot(Gene[[i]], ylim=my.ylim, axes=FALSE)
    mtext(paste("Gene", i), 1, 0)
    if(i == 1){
        axis(2, las=1)
        mtext("Expression or what you have", 2, 3)
    }
}
title("Look at all my genes!", outer=TRUE)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

顺便说一句,我建议将数据框存储在列表中,而不是通过命名它们"Gene1","Gene2","Gene3"和"Gene4"来模仿列表.以这种方式自动化要容易得多.如果你仍然想将它们存储作为独立变量,替换Gene[[i]]get(paste0("Gene", i))my.ylim <- ...min(c(min(Gene1), min(Gene2) ...等.


Cha*_*ase 6

这是一个在黑暗中拍摄你想要的东西,使用ggplot2和相关的工具.

library(ggplot2)
library(reshape2)
library(plyr)

Gene1 <- read.table(text = "Ind1     Ind2       Ind3      Ind4
1          3         3.2        2.5
1          3         4          2
1.5        2         2.2        1
3.4        2         1          3", header = TRUE)

#Make a copy of Gene1
Gene2 <- Gene1

#A Round about way to rbind these together with an ID column
combined_data <- ldply(list(Gene1 = Gene2, Gene2 = Gene2))

#Melt into the long format needed by ggplot2
combined_data_melt <- melt(combined_data, id.vars = 1)

#Plot and use facet_wrap for each data.frame
ggplot(combined_data_melt, aes(variable, value)) +
  geom_boxplot() +
  facet_wrap(~.id, ncol = 1) +
  theme_bw()
Run Code Online (Sandbox Code Playgroud)

给你这样的东西作为输出:

在此输入图像描述

这应该做你想要的,对代码的微小改动.感谢Joran关于躲闪的R聊天提示.

ggplot(combined_data_melt, aes(.id, value, dodge = variable)) +
  geom_boxplot(position = position_dodge(width = 0.8)) +
  theme_bw()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述