如何在R中向barplot添加错误栏

时间:2018-02-08 18:27:31

标签: r ggplot2

我是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的新手,请尽可能简单地解释!

1 个答案:

答案 0 :(得分:2)

这是

之后的基本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 ....")

看起来像这样:

Basic boxplot

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

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 ....")

Improved plot

注意:如果您有生成置信区间的原始数据,则可以使用箱形图(使用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. 带有抖动的基础数据。
  2. scatter plot

    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. 箱图
    2. boxplot of the underlying data

      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. 小提琴情节
      2. Violin plot

        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标准差
        2. Mean with 1 and 2 standard deviations

          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. 抖动点和小提琴情节
          2. enter image description here

            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 ....") 
            

            以及与原始数据重叠的任何其他摘要图。当你有大量数据时会出现这种情况,在这种情况下,其中一个摘要图本身会更好。