简体   繁体   English

如何在R中绘制成对的成组分组的条形图?

[英]How to plot bar graphs of grouped groups of pairs in R?

I have a dataframe as: 我有一个数据框为:

Sex Tissue  Stage   Mean    SE
M   X   Larva   9.2 1.3
F   X   Larva   8.4 1.1
M   Y   Larva   9.2 1.4
F   Y   Larva   7.4 0.3
M   X   Pupa    2.1 0.1
F   X   Pupa    5.3 0.2
M   Y   Pupa    9.5 0.5
F   Y   Pupa    2.5 0.1
M   Z   Pupa    6.3 0.4
F   Z   Pupa    9.2 1.1
M   X   Adult   1.2 0.1
F   X   Adult   3.1 0.2
M   Y   Adult   6.3 0.5
F   Y   Adult   9.2 0.6
M   Z   Adult   1.2 0.1
F   Z   Adult   2.3 0.1

How can I create a bar graph formatted as shown below (but also with standard error bars too) where all of the M's are the same color and all of the F's are the same colour? 如何创建如下所示的条形图(也具有标准误差条),其中所有M都具有相同的颜色,而所有F都具有相同的颜色?

100 |               _     _ _    _         
    |  _   _       | |  _| | |  | |  _   _  
    | | | | |_     | |_| | | |  | | | | | |  
Mean| | |_| | |   _| | | | | |  | | | | | |_
    | | | | | |  | | | | | | |  | |_| |_| | |
 0  |_|_|_|_|_|__|_|_|_|_|_|_|__|_|_|_|_|_|_|_
       M F M F    M F M F M F    M F M F M F
        X   Y      X   Y   Z      X   Y   Z 
        Larva         Pupa          Adult

Data 数据

dd <- read.table(header = TRUE, text = "Sex Tissue  Stage   Mean    SE
M   X   Larva   9.2 1.3
F   X   Larva   8.4 1.1
M   Y   Larva   9.2 1.4
F   Y   Larva   7.4 0.3
M   X   Pupa    2.1 0.1
F   X   Pupa    5.3 0.2
M   Y   Pupa    9.5 0.5
F   Y   Pupa    2.5 0.1
M   Z   Pupa    6.3 0.4
F   Z   Pupa    9.2 1.1
M   X   Adult   1.2 0.1
F   X   Adult   3.1 0.2
M   Y   Adult   6.3 0.5
F   Y   Adult   9.2 0.6
M   Z   Adult   1.2 0.1
F   Z   Adult   2.3 0.1")

Here's a ggplot2 version. 这是ggplot2版本。 The code below places "F" or "M" below each bar. 下面的代码在每个小节的下方放置“ F”或“ M”。 You can also choose not to include these letters and instead place a legend next to the plot. 您也可以选择不包括这些字母,而在图旁边放置图例。

library(ggplot2)

dd$Stage = factor(dd$Stage, levels=c("Larva","Pupa","Adult"))

pd = position_dodge(0.9)

ggplot(dd, aes(Tissue, Mean, colour=Sex)) + 
  geom_bar(stat="identity", position=pd, fill="grey95", lwd=1) +
  geom_text(aes(label=Sex, y=-0.5), position=pd, size=3.5) +
  geom_errorbar(aes(ymin=Mean - SE, ymax=Mean + SE), width=0.1, position=pd) +
  facet_grid(. ~ Stage, switch="x", scales="free_x", space="free_x") +
  theme_bw() +
  theme(strip.background=element_rect(fill=NA, colour=NA),
        strip.text.x=element_text(size=12)) +
  scale_y_continuous(limits=c(-0.5, max(dd$Mean + dd$SE))) +
  guides(fill=FALSE, colour=FALSE) +
  labs(x="")

在此处输入图片说明

Here's a base r version 这是基本的R版本

dd$Stage <- factor(dd$Stage, levels = unique(dd$Stage))

bp <- barplot(dd$Mean, space = c(0, diff(as.numeric(dd$Stage)) != 0),
              col = c('tomato','dodgerblue2')[as.numeric(dd$Sex)],
              names.arg = dd$Sex, las = 1)
arrows(bp, dd$Mean, bp, dd$Mean + dd$SE,
       angle = 90, code = 3, xpd = NA, length = .1)

## text
tt <- tapply(bp, list(dd$Tissue, dd$Stage), mean)
text(c(tt), -2.5, labels = rownames(tt), xpd = NA)

tt <- tapply(bp, list(dd$Stage), mean)
text(c(tt), -3.5, labels = names(tt), xpd = NA)

在此处输入图片说明

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

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