Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
library(tidyverse)
df <- tibble(category = c("A", "B")) %>% 
  group_by(category) %>% 
  mutate(values = map(100, rnorm)) %>% 
  unnest_longer(col = values)  %>% 
  group_by(category) %>% 
  mutate(pred = mean(values))

df %>% 
  distinct(pred)
#> # A tibble: 2 x 2
#> # Groups:   category [2]
#>   category   pred
#>   <chr>     <dbl>
#> 1 A        0.158 
#> 2 B        0.0502

ggplot(df,
       aes(x = category,
           y = values)) + 
  geom_jitter(colour = "gray") +
  geom_segment(data = {df %>% filter(category == "A")},
               x = 0.4,
               xend = 1.4,
               aes(y = pred,
                   yend = pred)) +
  geom_segment(data = {df %>% filter(category == "B")},
               x = 1.6,
               xend = 2.6,
               aes(y = pred,
                   yend = pred))

Created on 2020-11-06 by the reprex package (v0.3.0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment