Barplot与SError从表

时间:2015-05-27 17:55:31

标签: r ggplot2 vegan

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

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的标准错误并将它们绘制为分组的条形图......

我在这里做错了什么?

1 个答案:

答案 0 :(得分:5)

这是你想要的吗?

加载您的数据片段:

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包含值Ssevar包含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

这会产生

enter image description here