简体   繁体   English

R中的ggplot2中用于2x2x2设计的堆叠条形图

[英]Stacked barplot for a 2x2x2 design in ggplot2 in R

I have a dataset that looks like this: 我有一个看起来像这样的数据集:

conifer.abundance <- c(6,7,8,2,3,4,5,1,7,8,9,8,7,6,5,1)
lily.abundance <- c(5,5,5,5,4,4,4,4,6,7,8,2,3,4,5,1)
type <- c("Control","Control","Control","Control","Control","Control","Control","Control","Treatment","Treatment","Treatment","Treatment","Treatment","Treatment","Treatment","Treatment")
class <- c("City","Rural","City","Rural","City","Rural","City","Rural","City","Rural","City","Rural","City","Rural","City","Rural")
climate <- c("wet","wet","dry","dry","wet","wet","dry","dry","wet","wet","dry","dry","wet","wet","dry","dry")
all.abundance <- conifer.abundance + lily.abundance
dat88 <- data.frame(climate,type,class,conifer.abundance, lily.abundance,all.abundance)

This is a 2x2x2 design. 这是2x2x2的设计。 I want to plot barplots such that the mean of all.abundance is represented as sum of mean conifer.abundance and mean lily.abundance (stacked) and it has a legend of its own. 我想绘制条形图,以便将all.abundance的均值表示为mean conifer.abundance和mean lily.abundance(堆叠)的总和,并且它具有自己的传说。 I tried following this code, but it seems like it using fill to stack the bars, but I need to use it for a different purpose here. 我尝试遵循代码,但是似乎使用fill来堆积条形图,但是在这里我需要将其用于其他目的。 Suppose, I have several more data points, I would also need to plot a bootstrapped confidence interval (as below). 假设我还有几个数据点,我还需要绘制一个自举的置信区间(如下所示)。 Any suggestions? 有什么建议么? Here is my current code for plotting the graph above. 这是我目前用于绘制上面图形的代码。

  pd <- position_dodge(0.82) 
  ggplot(dat88, aes(x=class, y=all.abundance, fill = climate)) + 
  theme_bw() + 
  stat_summary(geom="bar", fun.y=mean, position = "dodge") + 
  stat_summary(geom="errorbar", fun.data=mean_cl_boot,position = pd) + 
  ylab("Total Abundance") + 
  facet_grid(~type)

Please note that I have slightly changed the dataset to represent a more biologically fitting scenario. 请注意,我略微更改了数据集以代表更符合生物学的情况。

If you want to stack the height values for female & male, you'll need to melt / gather them into a single variable. 如果要堆叠女性和男性的身高值,则需要将其融化/收集到单个变量中。

The following two methods for manipulating the data frame are equivalent. 以下两种用于操纵数据帧的方法是等效的。 Depends on which packages you are more familiar with: 取决于您更熟悉哪些软件包:

# data.table package
dat2 <- data.table::melt(dat, measure.vars = c("male.height", "female.height"),
                         variable.name = "Gender", value.name = "height")

# tidyr package
dat3 <- tidyr::gather(dat, key = Gender, value = height, 
                      male.height, female.height, factor_key = TRUE)

> all.equal(dat2, dat3)
[1] TRUE

Since this is a 2 x 2 x 2 design, I added a dimension to facet_grid to show both type and species. 由于这是2 x 2 x 2的设计,因此我向facet_grid添加了一个尺寸,以显示类型和种类。 If that's not needed, simply revert to facet_grid(~type) : 如果不需要,只需恢复为facet_grid(~type)

ggplot(dat2,
       aes(x = class, y = height, fill = Gender)) +
  geom_col() +
  ylab("Total Height") +
  facet_grid(species~type) +
  scale_fill_discrete(breaks = c("female.height", "male.height"),
                      labels = c("female", "male"))

方面图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM