Marco
Marco

Reputation: 2797

How to connect means per group in ggplot?

I can do a scatterplot of two continuous variables like this:

mtcars %>% 
  ggplot(aes(x=mpg, y = disp)) + geom_point() +
  geom_smooth(method="auto", se=TRUE, fullrange=FALSE, level=0.95) 

enter image description here

I use cut to create 5 groups of mpg intervals for cars (any better command would do as well). I like to see the intervals in the graph, thus they are easy to understand.

mtcars %>% 
  mutate(mpg_groups = cut(mpg, 5)) %>% 
  group_by(mpg_groups) %>% 
  mutate(mean_disp = mean(disp)) %>% 
  ggplot(aes(x=mpg_groups, y = mean_disp)) + geom_point()

enter image description here

mpg_groups is a factor variable and can no longer be connected via geom_smooth().

# not working
mtcars %>% 
  mutate(mpg_groups = cut(mpg, 5)) %>% 
  group_by(mpg_groups) %>% 
  mutate(mean_disp = mean(disp)) %>% 
  ggplot(aes(x=mpg_groups, y = mean_disp)) + geom_point() +
  geom_smooth(method="auto", se=TRUE, fullrange=FALSE, level=0.95) 

What can I do with easy (tidyverse) code in order to create the mean values per group and connect them via line?

Upvotes: 0

Views: 31

Answers (1)

stefan
stefan

Reputation: 123768

As a more or less general rule, when drawing a line via ggplot2 you have to explicitly set the group aesthetic in all cases where the variable mapped on x isn't a numeric, i.e. use e.g. group=1 to assign all observations to one group which I call 1 for simplicity:

library(ggplot2)
library(dplyr, warn=FALSE)

mtcars %>%
  mutate(mpg_groups = cut(mpg, 5)) %>%
  group_by(mpg_groups) %>%
  mutate(mean_disp = mean(disp)) %>%
  ggplot(aes(x = mpg_groups, y = mean_disp, group = 1)) +
  geom_point() +
  geom_smooth(method = "auto", se = TRUE, fullrange = FALSE, level = 0.95)

Upvotes: 1

Related Questions