Last active
December 6, 2016 15:09
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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