Skip to content

Instantly share code, notes, and snippets.

@bhive01
Last active March 24, 2020 21:34
Show Gist options
  • Save bhive01/a340e05d0e17f599eb6bb62329c364a6 to your computer and use it in GitHub Desktop.
Save bhive01/a340e05d0e17f599eb6bb62329c364a6 to your computer and use it in GitHub Desktop.
library(agricolae)
library(tidyverse)
mtcars2 <-
mtcars %>%
#move rownames to new column called Model
rownames_to_column(var = "Model")
# how does cyl predict displacment
aov_cars <- aov(disp ~ cyl, data = mtcars2)
# tukey_hsd post-hoc test
tukey_hsd <- HSD.test(aov_cars, trt = "cyl")
#get the means and groups tables add the rownames as cylinder
tukey_mean <- rownames_to_column(tukey_hsd$means, var = "cyl")
# same, but select the cylinder and groups only, more explicit for joining
tukey_groups <- rownames_to_column(tukey_hsd$groups, var = "cyl") %>% select(cyl, groups)
#join them together
left_join(tukey_mean, tukey_groups, by = "cyl") %>%
# make a standard error of the mean column
mutate(sem = std/r) %>%
# lets plot now
ggplot(data = ., aes(x = cyl, y = disp, ymin = disp - sem, ymax = disp + sem, label = groups)) +
# lets add points for means
geom_point() +
# let's have an errorbar for ± sem
geom_errorbar() +
# let's now add our groups
geom_text(colour = "purple", nudge_x = -0.25)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment