如何使用ggplot中的分组棒棒糖图表制作误差线?

时间:2020-05-21 05:45:32

标签: r ggplot2

我有此数据用于棒棒糖分组图

grp     percent   percent_min   perc_max
Cold       82.3          81.5   83.5
Warm       84.4          82.2   86.3

这是我的图表的代码

  dataframe %>%
  ggplot(aes(grp, percent)) + 
  geom_linerange(aes(x = grp, 
                     ymin = 75, 
                     ymax = percent), 
                 position = position_dodge(width = 1)) +
  geom_point(size = 7, 
             position = position_dodge(width = 1)) 

我尝试使用geom_errorbar为两个线段添加误差线。

我不确定如何使它们与两者一起使用。

如何获得“冷”的一个错误栏和“温暖”的另一个错误栏?

1 个答案:

答案 0 :(得分:2)

您可以使用以下代码

library(tidyverse)

dataframe %>%
  ggplot(aes(grp, percent)) + 
  geom_linerange(aes(x = grp, 
                     ymin = 75, 
                     ymax = percent), 
                 position = position_dodge(width = 1)) +
  geom_point(size = 7, position = position_dodge(width = 1)) +
  geom_errorbar(aes(ymin = percent_min, ymax = perc_max))

enter image description here

数据

dataframe = structure(list(grp = structure(1:2, .Label = c("Cold", "Warm"
), class = "factor"), percent = c(82.3, 84.4), percent_min = c(81.5, 
82.2), perc_max = c(83.5, 86.3)), class = "data.frame", row.names = c(NA, 
-2L))