R Data Visualization Recipes
上QQ阅读APP看书,第一时间看更新

Getting ready

Package gridExtra must be installed:

> if( !require(gridExtra)){ install.packages('gridExtra')}

The recipe also requires a function to withdraw the legends from the plots:

> g_legend <- function(p){
> tmp <- ggplot_gtable(ggplot_build(p))
> leg <- which(sapply(tmp$grobs, function(x) x$name) == 'guide-box')
> legend <- tmp$grobs[[leg]]
> return(legend)}

This later solution was found as a StackOverFlow answer given by Luciano Selzer. Now we're ready for action.