1

Note: I haven't found any post related to my question and I'm new to stack overflow, sorry if this has been done before.
Consider the following plot:

data.frame(var = c("a","b", "c"), num = c(2,3,4)) %>% 
  ggplot(aes(var, num)) + 
  geom_bar(stat = "identity") + 
  geom_point() + 
  geom_line()

With this I am able to make a barplot with points at the middle of each barplot. However, adding geom_line results in the following error:

geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?

I just want to make a line that joins each point in the barplot. How do I do so?

stefan
  • 90,330
  • 6
  • 25
  • 51
AntPalmer
  • 51
  • 6

1 Answers1

1

welcome to SO! This happens because each observation is treated as a separate group by geom_line(). Set the groups to 1 to revert this feature.

data.frame(var = c("a","b", "c"), num = c(2,3,4)) %>% ggplot(aes(var, num)) + geom_bar(stat = "identity") + geom_point() + geom_line(aes(group = 1))

enter image description here

Magnus Nordmo
  • 923
  • 7
  • 10