0
votes

I'm trying to make a lollipop plot (with geom_segment and geom_point) similar to a bar plot but my Y is count instead of a provided variable from dataset and I can't find anywhere how to do this.

My code:

data %>%
  ggplot(aes(x=x)) +
  geom_point(stat="count") + 
  geom_segment(aes(xend=x, y=0, yend=..count..))

How do I specify correctly the yend value? Geom_point alone seems to be working just fine. I believe I don't understand the correct use of ..count.. within ggplot.

1

1 Answers

0
votes

It seems best to do some calculations before running ggplot. So:

data %>% 
  group_by(x) %>% 
  summarise(count = n()) %>% 
  ggplot(aes(x, count)) + 
  geom_segment(aes(x=x, xend=x, y=0, yend=count)) + 
  geom_point()