【问题标题】:How to add error bars to barplot in R [closed]如何在 R 中向 barplot 添加误差线 [关闭]
【发布时间】:2018-02-08 18:27:31
【问题描述】:

我是 R 新手并制作了一个图表,但我想尽可能简单地添加误差线,但我不知道如何。

ana <- read.table(text="Infiltration    Grazing Burn
3301.145496 G   S
8165.771889 U   S
9937.833576 G   L
11576.5892  U   L
32739.07643 G   N
25923.84328 U   N", header=TRUE)

这是我的数据,下面是我使用的代码。

    barplot(xtabs(ana$Infiltration ~ ana$Grazing + ana$Burn ),beside = TRUE, col = c( "tan4", "darkgreen"), xlab = "Burn Treatment", names = c( "Long Rotation", "Burned 1954", "Short Rotation" ) , ylab = "Mean Infiltration Rate (mm/h) " , legend = c( "Grazed", "Ungrazed"), args.legend = list(title = "Graze Treatment", x = "topright", cex = .7), ylim = c(0, 35000)  ) 

由于我是 R 新手,请尽可能简单地解释!

【问题讨论】:

  • 我知道我添加的表格格式不正确,我不知道如何编辑它
  • 嘿,不幸的是,在基本 R 图中添加误差线并不是那么简单。我认为使用 ggplot2 更容易很多。这是一个 R 包,可用于制作绘图。试试这个教程:cookbook-r.com/Graphs/Plotting_means_and_error_bars_(ggplot2)
  • 另外,您的示例数据没有任何可用于误差线长度的值。
  • 您是如何计算误差的?您的数据没有错误。而且只有一个数字没有“错误”。

标签: r ggplot2


【解决方案1】:

这是您所追求的基本ggplot2 实现

library(dplyr)
library(ggplot2)
library(magrittr)

## Read in the q data
df <- read.table(text = "Infiltration    Grazing Burn
                 3301.145496 G   S
                 8165.771889 U   S
                 9937.833576 G   L
                 11576.5892  U   L
                 32739.07643 G   N
                 25923.84328 U   N",
                 header = TRUE)
## Add test Lower and upper bounds, trans varnames

df <- df %>% 
  mutate(ll = Infiltration * 0.9,
         hh = Infiltration * 1.1) %>% 
  mutate(Grazing = Grazing %>%
           recode(G = "Grazed", U = "Ungrazed"),
         Burn = Burn %>% 
           recode(S = "Short Rotation", L = "Long Rotation", N = "Burned 194")) %>% 
  rename(`Graze Treatment` = Grazing)

## Basic boxplot with ci's
df %>% 
  ggplot(aes(x = Burn, y = Infiltration, fill = `Graze Treatment`)) +
  geom_bar(stat = "identity", position = "dodge") +
  geom_errorbar(aes(ymin = ll, ymax = hh), position = "dodge") +
  theme_minimal() +
  labs(y =  "Mean Infiltration Rate (mm/h)",
       caption = "Errorbars represent ....")

看起来像这样:

一般来说,带有胡须的箱线图有点难以解释。使用这样的东西可能会更好..

df %>% 
  ggplot(aes(x = Burn, y = Infiltration, fill = `Graze Treatment`, col = `Graze Treatment`)) +
  geom_point(stat = "identity", position = position_dodge(width = 1), size = 3) +
  geom_linerange(aes(ymin = ll, ymax = hh), position = position_dodge(width = 1),
                 alpha = 0.4, size = 3) +
  theme_minimal() +
  expand_limits(y = 0) +
  labs(y =  "Mean Infiltration Rate (mm/h)",
       caption = "Errorbars represent ....")

注意:如果您有生成置信区间的原始数据,则最好使用箱线图(geom_boxplot)、小提琴图(geom_violin)甚至脊图(ggridges:geom_density_ridges)。

一些可能的扩展

如果基础数据可用,我们可以做得更好。有几个选项,您选择哪一个取决于您的用例和数据大小。

首先让我们生成一些示例数据。

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

## Read in the q data
df <- read.table(text = "Infiltration    Grazing Burn
                 3301.145496 G   S
                 8165.771889 U   S
                 9937.833576 G   L
                 11576.5892  U   L
                 32739.07643 G   N
                 25923.84328 U   N",
                 header = TRUE)
## Generate and clean some sample data
df <- df %>% 
  as_tibble %>% 
  mutate(Infiltration = map(Infiltration, function(x) {
    tibble(Infiltration = rnorm(n = 1000, 
                                mean = x, 
                                sd = 0.1 * x),
           id = 1:1000)
    })) %>% 
  unnest() %>% 
  mutate(Grazing = Grazing %>%
           recode(G = "Grazed", U = "Ungrazed"),
         Burn = Burn %>% 
           recode(S = "Short Rotation", L = "Long Rotation", N = "Burned 194")) %>% 
  rename(`Graze Treatment` = Grazing)

现在让我们做一些情节。

  1. 有抖动的基础数据。

df %>% 
  ggplot(aes(x = Burn, y = Infiltration, fill = `Graze Treatment`, col = `Graze Treatment`)) +
  geom_jitter(position = position_jitterdodge(), alpha = 0.1) +
  theme_minimal() +
  expand_limits(y = 0) +
  labs(y =  "Mean Infiltration Rate (mm/h)",
       caption = "Errorbars represent ....") 
  1. 箱线图

df %>% 
  ggplot(aes(x = Burn, y = Infiltration, fill = `Graze Treatment`, col = `Graze Treatment`)) +
  geom_boxplot(alpha = 0.4) +
  theme_minimal() +
  expand_limits(y = 0) +
  labs(y =  "Mean Infiltration Rate (mm/h)",
       caption = "Errorbars represent ....") 
  1. 小提琴情节

df %>% 
  ggplot(aes(x = Burn, y = Infiltration, fill = `Graze Treatment`, col = `Graze Treatment`)) +
  geom_violin(draw_quantiles = c(0.25, 0.5, 0.75), alpha = 0.4) +
  theme_minimal() +
  expand_limits(y = 0) +
  labs(y =  "Mean Infiltration Rate (mm/h)",
       caption = "Errorbars represent ....") 
  1. 具有平均值、1 和 2 个标准差的点

df %>% 
  group_by(`Graze Treatment`, Burn) %>% 
  summarise(
         mean = mean(Infiltration),
         sd = sd(Infiltration),
         lll = mean - 2 * sd,
         ll = mean - sd,
         hh = mean + sd,
         hhh = mean + 2*sd) %>% 
  ggplot(aes(x = Burn, y = mean, fill = `Graze Treatment`, col = `Graze Treatment`)) +
  geom_point(stat = "identity", position = position_dodge(width = 1), size = 3) +
  geom_linerange(aes(ymin = lll, ymax = hhh), position = position_dodge(width = 1),
                 alpha = 0.4, size = 3) +
  geom_linerange(aes(ymin = ll, ymax = hh), position = position_dodge(width = 1),
                 alpha = 0.6, size = 3) +
  theme_minimal() +
  expand_limits(y = 0) +
  labs(y =  "Mean Infiltration Rate (mm/h)",
       x = "Infiltration",
       caption = "Errorbars represent ....") 
  1. 抖动点和小提琴图

df %>% 
  ggplot(aes(x = Burn, y = Infiltration, fill = `Graze Treatment`, col = `Graze Treatment`)) +
  geom_violin(draw_quantiles = c(0.25, 0.5, 0.75),
              position = position_dodge(width = 1),
              aes(fill = NULL)) +
  geom_jitter(position = position_jitterdodge(dodge.width = 1), alpha = 0.01) +
  theme_minimal() +
  expand_limits(y = 0) +
  labs(y =  "Mean Infiltration Rate (mm/h)",
       caption = "Errorbars represent ....") 

以及任何其他覆盖原始数据的摘要图。当您有大量数据时,这种情况会下降,在这种情况下,单独的摘要图之一会更好。

【讨论】:

  • 哇。这太棒了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-08-01
  • 2020-03-25
  • 1970-01-01
  • 1970-01-01
  • 2013-11-18
  • 2021-10-07
  • 2019-09-17
相关资源
最近更新 更多