Ggplot number of points according to x value

I have a question from Hadley Wickham's ggplot2 book. I have this framework here:

class <- mpg %>%
   group_by(class) %>% 
   summarise(n = n(), hwy = mean(hwy))

I want to build a graph that looks like this: enter image description here

I tried:

class %>% ggplot(aes(n, hwy)) +
          geom_count()

This does not give me the above chart. Does anyone have any helpful suggestions?

+4
source share
1 answer

Try this and see the comment from @JakeKaupp below.

library(tidyverse)
class <- mpg %>%
count(class) %>% 
mutate(label = paste0("n = ", n))

ggplot(data = mpg, aes(class, hwy)) +
 geom_jitter(width = 0.1) +
 stat_summary(geom = "point", fun.y = mean, colour = "red", size = 5) +
 geom_text(data = class, aes(y = 10, label = label))

enter image description here

+4
source

Source: https://habr.com/ru/post/1692142/


All Articles