Multiple box plots within one group

我的未来我决定 提交于 2019-11-30 07:41:47

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.

Chase

Here's a shot in the dark at what you want, using ggplot2 and related tools.

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

Gives you something like this as an output:

This should do what you want, pretty minor change to the code. Thanks to Joran for the tip in R chat about dodge.

ggplot(combined_data_melt, aes(.id, value, dodge = variable)) +
  geom_boxplot(position = position_dodge(width = 0.8)) +
  theme_bw()

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!