Get data from ggplot()
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 hidden or 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) |
fill y count prop x PANEL group ...
...