r - Adding table to ggplot with facets -
reproducible code:
x = sample(1:12,100,replace=true) y = rnorm(100) z = sample(c('sample a','sample b'),100,replace=true) d = data.frame(x,y,z) ggplot(data=d, aes(factor(x),y)) + geom_boxplot() + stat_summary(fun.y=mean, geom="line", aes(group=1), color ='red') + stat_summary(fun.y=mean, geom="point", color='red') + xlab('months') + ylab('metric') + facet_wrap(~z) i want add table @ end of chart displays summary statistics- mean, median, quartiles , number of records each month on x-axis. not sure how possible facet layout. simplified version of chart , there multiple facets working with. thinking along lines of getting statistics stat_summary, can display @ end?
maybe need use grid library. here's example:
library(ggplot2) x = sample(1:12,100,replace=true) y = rnorm(100) z = sample(c('sample a','sample b'), 100, replace=true) d = data.frame(x,y,z) g1 <- ggplot(data=d, aes(factor(x),y)) + geom_boxplot() + stat_summary(fun.y=mean, geom="line", aes(group=1), color ='red') + stat_summary(fun.y=mean, geom="point", color='red') + xlab('months') + ylab('metric') + facet_wrap(~z) g2 <- ggplot() + theme_void() + xlim(0, 1) + ylim(0, 1) + annotate("text", x=0.5, y=0.5, label="draw summary here") library(grid) grid.newpage() pushviewport(viewport(layout=grid.layout(4,2))) print(g1, vp=viewport(layout.pos.row = 1:3, layout.pos.col = 1:2)) print(g2, vp=viewport(layout.pos.row = 4, layout.pos.col = 1)) print(g2, vp=viewport(layout.pos.row = 4, layout.pos.col = 2)) 
Comments
Post a Comment