我在 R 中有一个数据集,我想在其中显示错误栏,但是它没有正确绘制(见图)。我还包括了我的数据集。

ant.d<-structure(list(group.name = structure(c(1L, 18L, 20L, 24L, 8L,
                                        13L, 15L, 17L, 12L, 19L, 21L, 22L, 23L, 9L, 11L, 16L, 2L, 3L,
                                        4L, 5L, 6L, 7L, 10L, 14L), .Label = c("group 1", "group 10",
                                                                              "group 11", "group 12", "group 13", "group 14", "group 15 ",
                                                                              "group 16 ", "group 17", "group 18", "group 19", "group 2", "group 20",
                                                                              "group 21", "group 22", "group 23", "group 24", "group 3", "group 4 ",
                                                                              "group 5 ", "group 6", "group 7 ", "group 8 ", "group 9 "), class = "factor"),
               habitat.type = structure(c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
                                          2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
                                          1L), .Label = c("edge", "forest", "Pasture"), class = "factor"),
               species.richness = c(3L, 5L, 2L, 3L, 1L, 2L, 4L, 3L, 9L,
                                    5L, 5L, 4L, 4L, 4L, 8L, 7L, 4L, 3L, 5L, 2L, 3L, 6L, 2L, 1L
               ), X = c(2.875, 2.875, 2.875, 2.875, 2.875, 2.875, 2.875,
                        2.875, 5.75, 5.75, 5.75, 5.75, 5.75, 5.75, 5.75, 5.75, 3.25,
                        3.25, 3.25, 3.25, 3.25, 3.25, 3.25, 3.25), se = c(2.32340059786604,
                                                                          1.7996983644207, 2.84557296642458, 2.32340059786604, 4.02424788183988,
                                                                          2.84557296642458, 2.01212394091994, 2.32340059786604, 1.34141596061329,
                                                                          1.7996983644207, 1.7996983644207, 2.01212394091994, 2.01212394091994,
                                                                          2.01212394091994, 1.42278648321229, 1.52102272991811, 2.01212394091994,
                                                                          2.32340059786604, 1.7996983644207, 2.84557296642458, 2.32340059786604,
                                                                          1.64289231816395, 2.84557296642458, 4.02424788183988)), .Names = c("group.name",
                                                                                                                                             "habitat.type", "species.richness", "X", "se"), row.names = c(NA,
                                                                                                                                                                                                           -24L), class = "data.frame")

R ggplot2 - 相互分层的误差线-LMLPHP

我究竟做错了什么?我花了一些时间阅读 R 中的误差条,但我没有成功。
ant.d$se <- 1.96*(sd(ant.d$species.richness, na.rm=T)/sqrt(ant.d$species.richness))

p<-ggplot(data = ant.d, aes(y = species.richness, x = habitat.type)) +
  geom_bar(stat="identity",position="dodge")
p
p + geom_bar(position=dodge) + geom_errorbar(aes(ymax = species.richness + se, ymin=species.richness - se), position=dodge, width=0.25)

最佳答案

如果我正确理解您要实现的目标,那么最好在绘图之前汇总您的数据:

df <- aggregate(cbind(species.richness,se) ~ habitat.type, ant.d, mean)

ggplot(data = df, aes(x = habitat.type, y = species.richness)) +
  geom_bar(stat="identity", fill="grey") +
  geom_errorbar(stat="identity", aes(ymax = species.richness + se, ymin=species.richness - se), width=0.25)

这使:

R ggplot2 - 相互分层的误差线-LMLPHP

如果您想要每个 habitat.type 中的组,您可以是这样的:
ggplot(data = ant.d, aes(x = habitat.type, y = species.richness, fill = group.name)) +
  geom_bar(stat="identity", position=position_dodge(0.8)) +
  geom_errorbar(stat="identity", aes(ymax = species.richness + se, ymin=species.richness - se), width=0.25,
                position=position_dodge(0.8)) +
  scale_fill_discrete(guide = guide_legend(ncol=2))

这使:

R ggplot2 - 相互分层的误差线-LMLPHP

关于R ggplot2 - 相互分层的误差线,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32644315/

10-12 17:54