Skip to content

Instantly share code, notes, and snippets.

@jeffreyhanson
Last active August 29, 2015 14:26
Show Gist options
  • Save jeffreyhanson/4f111566a535e7edb36f to your computer and use it in GitHub Desktop.
Save jeffreyhanson/4f111566a535e7edb36f to your computer and use it in GitHub Desktop.
# deps
library(magrittr)
library(dplyr)
library(ggplot2)
library(gridExtra)
# load data
data(iris)
# add in factor versions of the columns
# Species is alphabetically ordered
# Species2 is randomly ordered
iris %>% mutate(
Species=factor(Species),
Species2=factor(Species, levels=c("virginica", "setosa", "versicolor"))
) %>% group_by(
Species,
Species2
) %>% summarise(
Sepal.Length=mean(Sepal.Length)
) -> meanDF
# make panels
p1=ggplot(meanDF, aes(x=Species, y=Sepal.Length)) + geom_bar(stat="identity") + ggtitle('default alphabetic order')
p2=ggplot(meanDF, aes(x=Species2, y=Sepal.Length)) + geom_bar(stat="identity")+ ggtitle('random order')
# make plots
grid.arrange(p1, p2, ncol=2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment