为ggplot barplot复制错误栏

时间:2018-10-03 12:25:24

标签: r ggplot2 bar-chart errorbar

我是ggplot的新手,在条形图中绘制误差线时遇到问题。 一个最小的工作示例如下:

abun_all <- data.frame("Tree.genus" = c(rep("Acer", 5), rep("Betula", 5), rep("Larix", 5), rep("Picea", 5), rep("Pinus", 5), rep("Quercus", 5)),
               "P.sampled" = c(sample(c(seq(from = 0.001, to = 0.06, by = 0.0005)), 30)),
               "Insects.sampled" = c(sample(c(seq(from = 1.667, to = 533, by = 1.335)), 30)),
               "Category" = as.factor(c(sample(c(seq(from = 1, to = 3, by = 1)), 30, replace = T))),
               "P.sampled_mean" = c(sample(c(seq(from = 0.006, to = 0.178, by = 0.0005)), 30)),
               "P.sampled_sd" = c(sample(c(seq(from = 0.004, to = 0.2137, by = 0.0005)), 30)))

ggplot(data = abun_all, aes(x = as.factor(Tree.genus), y = P.sampled , fill = Category)) +
geom_bar(stat = "identity", position = position_dodge(1)) +
geom_errorbar(aes(ymin = P.sampled - (P.sampled_mean+P.sampled_sd), ymax = P.sampled + (P.sampled_mean+P.sampled_sd)), width = 0.1, position = position_dodge(1)) + scale_fill_discrete(name = "Category",
                  breaks = c(1, 2, 3),
                  labels = c("NrAm in SSM", "NrAm in FR", "Eurp in FR")) +
xlab("Genus") + ylab("No. of Focus sp. per total insect abundance")

注意:这些值只是随机的,并不代表实际数据,但足以说明问题!

问题似乎是针对每个类别为每个Tree.genus的总数绘制了误差线。我该如何使用它?

编辑:我只用每个P.sampled组合的最大值手工创建了另一个Df,现在该图看起来像我想要的样子(除了两个缺失的误差线)。

abun_plot <- data.frame("Tree.genus" = rep(genera, each = 3),
                      "P.sampled" = c(0.400000000, 0.100000000, 0.500000000, 0.200000000, 0.100000000, 0.042857143, 0.016666667, 0.0285714286, 0.0222222222, 0.020000000, 0, 0.010000000, 0.060000000, 0.025000000, 0.040000000, 0.250000000, 0.150000000, 0.600000000),
                      "Category" = as.factor(rep(c(1,2,3), 3)),
                      "P.sampled_SD" = as.numeric(c(0.08493057, 0.02804758, 0.19476489, 0.04533747, 0.02447665, 0.01308939, 0.004200168, "NA", 0.015356359, 0.005724859, "NA", "NA", 0.01633612, 0.01013794, 0.02045931, 0.07584737, 0.05760980, 0.21374053)),
                      "P.sampled_Mean" = as.numeric(c(0.07837134, 0.05133333, 0.14089286, 0.04537983, 0.02686200, 0.01680721, 0.005833333, 0.028571429, 0.011363636, 0.01101331, "NA", 0.01000000, 0.02162986, 0.01333333, 0.01668582, 0.08705221, 0.04733333, 0.17870370)))

ggplot(data = abun_plot, aes(x = as.factor(Tree.genus), y = P.sampled , fill = Category)) +
geom_bar(stat = "identity", position = position_dodge(1)) +
geom_errorbar(aes(ymin = P.sampled - P.sampled_SD, ymax = P.sampled + P.sampled_SD), width = 0.1, position = position_dodge(1)) +
scale_fill_discrete(name = "Category",
                    breaks = c(1, 2, 3),
                    labels = c("NrAm in SSM", "NrAm in FR", "Eurp in FR")) +
xlab("Genus") + ylab("No. of Focus sp. per total insect abundance")

由于手动执行此操作需要花费大量时间,而其他多个图也存在相同的问题,因此,我更喜欢使用原始df(abun_all)。我可以在ggplot()函数中将df子集化以获得所需的输出吗?

1 个答案:

答案 0 :(得分:0)

由于您只想显示属和类别的每种组合的最大值,因此可以使用几个dplyr函数(在tidyverseggplot2中)进行分组属和类别,然后取各自的最高价值。这样,您就不会像在第二个块中那样手动构建abun_plot

library(dplyr)
library(ggplot2)

abun_plot <- abun_all %>%
  group_by(Tree.genus, Category) %>%
  top_n(1, P.sampled_mean)

head(abun_plot)
#> # A tibble: 6 x 6
#> # Groups:   Tree.genus, Category [6]
#>   Tree.genus P.sampled Insects.sampled Category P.sampled_mean P.sampled_sd
#>   <fct>          <dbl>           <dbl> <fct>             <dbl>        <dbl>
#> 1 Acer          0.041            295.  3                0.0125       0.044 
#> 2 Acer          0.044             81.8 1                0.166        0.037 
#> 3 Acer          0.0085           379.  2                0.155        0.134 
#> 4 Betula        0.0505           183.  2                0.170        0.0805
#> 5 Betula        0.0325            61.7 3                0.0405       0.0995
#> 6 Betula        0.0465           326.  1                0.0985       0.188

之后,绘图将按您最初的预期进行:

ggplot(data = abun_plot, aes(x = as.factor(Tree.genus), y = P.sampled , fill = Category)) +
  geom_col(position = position_dodge(1)) +
  geom_errorbar(aes(ymin = P.sampled - P.sampled_sd, ymax = P.sampled + P.sampled_sd), width = 0.1, position = position_dodge(1)) +
  scale_fill_discrete(name = "Category",
                      breaks = c(1, 2, 3),
                      labels = c("NrAm in SSM", "NrAm in FR", "Eurp in FR")) +
  xlab("Genus") + ylab("No. of Focus sp. per total insect abundance")

还值得注意的是,从ggplot2开始发行了几个版本,您可以使用geom_col()代替geom_bar(stat = "identity")

reprex package(v0.2.1)于2018-10-03创建