来自 table 的带 SError 的条形图

Barplot with SError from table

我正在尝试从下面生成的 table 中绘制分组条形图。

Group.1                S.obs     se.obs    S.chao1   se.chao1
Cliona celata complex  499.7143  59.32867  850.6860  65.16366
Cliona viridis         285.5000  51.68736  462.5465  45.57289
Dysidea fragilis       358.6667  61.03096  701.7499  73.82693
Phorbas fictitius      525.9167  24.66763  853.3261  57.73494

到目前为止,我尝试了以下方法,但效果不佳:

library(dplyr)
library(tidyr)
library(ggplot2)

data.frame(t(agg_media)) %>%
add_rownames() %>%
gather(group, value, - c(rowname, se.chao1)) -> media_2
gather(group, value, - c(rowname, se.obs)) -> media_3

#take out error bars from S.obs
# mutate(media2, se.chao1 = replace(se.chao1, which(group == "S.obs"),NA)) -> media3

dodge <- position_dodge(width=0.9)

g <- ggplot(data = agg_media, aes(x = rowname, y = value, fill = group)) +
   geom_bar(position = "dodge", stat = "identity") + 
   geom_errorbar(data = media_2, aes(ymax = value + se.chao1, ymin = value - se.chao1),
            position = dodge, width = 0.25) +
   geom_errorbar(data = media_3, aes(ymax = value + se.obs, ymin = value - se.obs),
            position = dodge, width = 0.25) +
 labs(x = "Sponge Species", y = "Averaged OTU Richness") +
 scale_y_continuous(expand = c(0,0))

ggsave(g, file = "Obs_Est_OTUs.svg")

重点是将se.obs作为S.obs的标准误差,将se.chao1作为S.chao1的标准误差,并将它们绘制成分组条形图...

我做错了什么?

这是你想要的吗?

加载您的数据片段:

txt <- '"Group.1"                "S.obs"   "se.obs"  "S.chao1" "se.chao1"
"Cliona celata complex"  499.7143  59.32867  850.6860  65.16366
"Cliona viridis"         285.5000  51.68736  462.5465  45.57289
"Dysidea fragilis"       358.6667  61.03096  701.7499  73.82693
"Phorbas fictitius"      525.9167  24.66763  853.3261  57.73494'

dat <- read.table(text = txt, header = TRUE)

并加载一些包。特别是,我将使用 tidyr 进行数据操作,这并不真正适合熔铸或重塑概念

library("ggplot2")
library("tidyr")

这三个步骤以合适的格式获取数据。首先我们收集变量,就像 melt() 但我们需要告诉它不收集哪个变量,即哪个变量是 id 变量

mdat <- gather(dat, S, value, -Group.1)

S 是我要创建的包含变量名称的列,value 是我要创建的包含所选列中的数据的列的名称,- Group.1 表示 处理除 group.1 之外的所有列。这给出:

                 Group.1        S     value
1  Cliona celata complex    S.obs 499.71430
2         Cliona viridis    S.obs 285.50000
3       Dysidea fragilis    S.obs 358.66670
4      Phorbas fictitius    S.obs 525.91670
5  Cliona celata complex   se.obs  59.32867
6         Cliona viridis   se.obs  51.68736
7       Dysidea fragilis   se.obs  61.03096
8      Phorbas fictitius   se.obs  24.66763
9  Cliona celata complex  S.chao1 850.68600
10        Cliona viridis  S.chao1 462.54650
11      Dysidea fragilis  S.chao1 701.74990
12     Phorbas fictitius  S.chao1 853.32610
13 Cliona celata complex se.chao1  65.16366
14        Cliona viridis se.chao1  45.57289
15      Dysidea fragilis se.chao1  73.82693
16     Phorbas fictitius se.chao1  57.73494

接下来,我希望 S 变量数据在周期 (.) 上拆分为两个变量,我称之为 typevartype 包含值 Sse 并且 var 包含 obschao1

mdat <- separate(mdat, S, c("type","var"))

给出:

                 Group.1 type   var     value
1  Cliona celata complex    S   obs 499.71430
2         Cliona viridis    S   obs 285.50000
3       Dysidea fragilis    S   obs 358.66670
4      Phorbas fictitius    S   obs 525.91670
5  Cliona celata complex   se   obs  59.32867
6         Cliona viridis   se   obs  51.68736
7       Dysidea fragilis   se   obs  61.03096
8      Phorbas fictitius   se   obs  24.66763
9  Cliona celata complex    S chao1 850.68600
10        Cliona viridis    S chao1 462.54650
11      Dysidea fragilis    S chao1 701.74990
12     Phorbas fictitius    S chao1 853.32610
13 Cliona celata complex   se chao1  65.16366
14        Cliona viridis   se chao1  45.57289
15      Dysidea fragilis   se chao1  73.82693
16     Phorbas fictitius   se chao1  57.73494

数据处理的最后一步是展开当前紧凑的数据,以便我们有列 Sse,我们使用 spread()(这是一个有点像重塑中的铸造)

mdat <- spread(mdat, type, value)

这给了我们

mdat

> mdat
                Group.1   var        S       se
1 Cliona celata complex chao1 850.6860 65.16366
2 Cliona celata complex   obs 499.7143 59.32867
3        Cliona viridis chao1 462.5465 45.57289
4        Cliona viridis   obs 285.5000 51.68736
5      Dysidea fragilis chao1 701.7499 73.82693
6      Dysidea fragilis   obs 358.6667 61.03096
7     Phorbas fictitius chao1 853.3261 57.73494
8     Phorbas fictitius   obs 525.9167 24.66763

现在完成了,我们可以绘制

ggplot(mdat, aes(x = Group.1, y = S, fill = var)) +
    geom_bar(position = "dodge", stat = "identity") +
    geom_errorbar(mapping = aes(ymax = S + se, ymin = S - se),
                  position = position_dodge(width=0.9), width = 0.25)

你只需要调用一次geom_errorbar()因为它有美学ymaxymin可以同时设置。

这样就产生了