Skip to content

Instantly share code, notes, and snippets.

@riinuots
Last active December 6, 2016 15: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 riinuots/a6af08ac11f7b5ede69572a7559a704b to your computer and use it in GitHub Desktop.
Save riinuots/a6af08ac11f7b5ede69572a7559a704b to your computer and use it in GitHub Desktop.
ggplot includes built in and seamless functionality that summarises your data before plotting it. As shown in the example below, ggplot_build() can be used to access the summarised dataset.
library(ggplot2)
p = ggplot(diamonds, aes(x = color, fill=cut(price,
breaks = quantile(price),
include.lowest=TRUE,
labels = c('Expensive',
'More expensive',
'Very expensive',
'Ridiculously expensive')))) +
geom_bar(position='fill') +
facet_wrap(~cut) +
scale_fill_brewer('Price', palette='OrRd') +
theme(legend.position='top')
p
p_build = ggplot_build(p)
plot_data = as.data.frame(p_build$data)
head(plot_data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment