Drawing marginal histogram using gridExtra
If you seek a more tailor made result, there would bemore code to do. The solution this recipe presents is to draw three plots and later arrange them into a 2x2 grid using the gridExtra
package. Since it's a 2x2 grid there would be a blank space left to fill, let's move the legends there . This recipe works with ggplot2
.
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.
How to do it...
- Draw the center plot using
ggplot2
and extract the legend using theg_legend()
function:
> library(ggplot2) > main...