一组内多个箱形图 [英] Multiple box plots within one group

查看:531
本文介绍了一组内多个箱形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了一些解决方案,但不完全正确我想要的是。我在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,并且类似。 b
$ b

我想在所有数据框和所有列的同一图中并排绘制箱形图。我没有找到这样的情节,所以我不能上传图片,但我会尝试解释。



现在从上面的数据来看,情节将有20个盒子地块。前4个盒子图应该彼此接近,x轴名称应该是Gene1(对于所有4个盒子图),然后再绘制一小块空间,并再次绘制4个盒子图,其中x轴名称为Gene2等等。



我可以很容易地在一个绘图中绘制所有箱形图,但我无法区分数据帧。意思是,它应该清楚地告诉我们,前4个盒子的地块来自Gene1,接下来的4个盒子地块来自Gene2等等。

请让我知道如果问题不明确。

解决方案

我怀疑这是你想要的,而且事实上并不是很复杂绘图功能在标准图形包中。这些组被绘制为4个独立的面板,但共享的y轴和标题绘制在外部边缘,看起来像一个单一的情节。

 <$因为您没有提供任何
Gene< - data.frame(矩阵(rweibull(100 * 4,1),100))
名称(Gene) < - paste0(Ind,1:4)
基因< - rep(list(Gene),4)

#设置面板
layout(t 1:4))
par(oma = c(2,4,4,0),mar = rep(1,4),cex = 1)
#`mar`控制每个boxplot group

#计算范围以便面板可比较
my.ylim <-c(min(sapply(Gene,min)),max(sapply(Gene,max) ))

#绘制所有的盒子
for(i in 1:length(Gene)){
boxplot(Gene [[i]],ylim = my.ylim, ($ i $ 1 $)b $ b $(


多文字(表达或你有什么,2,3)
}
}
title(看看我所有的基因!,outer = TRUE)



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


I found some solutions but not exactly what I want. I have 5 dataframes in R and each dataframe has 4 columns:

Lets say name of the first dataframe is "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

and remaining dataframes are called "Gene2", "Gene3","Gene4","Gene5" and are similar.

I want to plot boxplots side by side in same plot for all dataframes and for all columns. I did not find any plot like this, so I can't upload a picture but I will try to explain.

Now from above data, the plot will have 20 box plots. First 4 box plot should be close to each other and x-axis name should be "Gene1" (for all 4 box plots) and then a little space in plot and again 4 box plots with x-axis name "Gene2" and so on.

I can easily plot all the box plots in one plot but I am not able to distinguish dataframes. Meaning, it should clearly show us that first 4 box plots are from "Gene1" and next 4 box plots are from "Gene2" and so on.

Please let me know if the problem is not clear.

解决方案

I suspect this is what you want, and it is in fact not very complicated to do with the plotting functions in the standard graphics package. The groups are plotted as 4 separate panels, but with a shared y-axis and title plotted in the outer margin it looks like a single plot.

# 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)

By the way, I recommend storing your data frames in a list rather than mimicing a list by naming them "Gene1", "Gene2", "Gene3" and "Gene4". It is a lot easier to automate that way. If you still want to store them as separate variables, replace Gene[[i]] with get(paste0("Gene", i)) and my.ylim <- ... with min(c(min(Gene1), min(Gene2) ... etc.

这篇关于一组内多个箱形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆