Skip to content

Instantly share code, notes, and snippets.

@steveharoz
Last active March 4, 2016 22:33
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 steveharoz/440466b4c650db7ad168 to your computer and use it in GitHub Desktop.
Save steveharoz/440466b4c650db7ad168 to your computer and use it in GitHub Desktop.
ordering in ggplot2
library(ggplot2)
data = data.frame(
a = rep(LETTERS[24:26], 3),
b = 1:9,
c = rep(LETTERS[1:3], each=3)
)
# uncomment to set facet order by refactoring the data
#data$a = factor(data$a, levels=c('Y', 'X', 'Z'))
ggplot(data, aes(x=c, y=b, fill=c)) +
geom_bar(stat='identity') +
# uncomment to set order of discrete axis
#scale_x_discrete(limits=c('C', 'A', 'B')) +
# uncomment to set order in legend
#scale_fill_discrete(breaks=c('B', 'C', 'A')) +
facet_grid(. ~ a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment