Cauder
Cauder

Reputation: 2627

How do I make error bars with a grouped lollipop chart in ggplot?

I have this data for a grouped lollipop chart

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

This is the code for my chart

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

I tried to add error bars for both of the lineranges with geom_errorbar.

I'm not sure how to get it to work with both of them.

How can I get one error bar for "cold" and another error bar for "warm?"

Upvotes: 1

Views: 411

Answers (1)

UseR10085
UseR10085

Reputation: 8198

You can use the following code

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

Data

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

Upvotes: 2

Related Questions