Skip to content

Instantly share code, notes, and snippets.

@thoolihan
Last active August 3, 2017 21:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thoolihan/821f5d88210ac3a271cf75c0fbd38fec to your computer and use it in GitHub Desktop.
Save thoolihan/821f5d88210ac3a271cf75c0fbd38fec to your computer and use it in GitHub Desktop.
arrange and plot not working together
library(tidyverse)
mtcars %>%
mutate(car = rownames(.)) %>%
arrange(hp) %>%
ggplot(aes(x = car, y = hp)) +
geom_point() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
# should be
# see https://stackoverflow.com/questions/20041136/avoid-ggplot-sorting-the-x-axis-while-plotting-geom-bar
mtcars %>%
arrange(hp) %>%
mutate(car = factor(rownames(.), levels = rownames(.))) %>%
ggplot(aes(x = car, y = hp, color = factor(cyl))) +
geom_point() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment