Adding marginal histograms using ggExtra
Another way to go is to draw histograms or even density distributions in the margins. Drawing tailor made plots in the margins would require more code. On the other hand, if there is no need for greater customization ggExtra
package can be used to spare many code lines. This recipe is demonstrating how to use ggExtra
to easily draw histograms in the margins of a scatterplot.
Getting ready
In order to properly execute this recipe, the ggExtra
package must be locked and loaded. Run the following code to make that happen:
> if( !require(ggExtra)){ install.packages('ggExtra')}
Once ggExtra
is installed we can go on.
How to do it...
- Draw a
ggplot2
scatterplot like this:
library(ggplot2) base_p <- ggplot(iris, aes(x = Petal.Length, y = Petal.Width, colour = Species)) scatter <- base_p + geom_point( alpha = .5, aes(shape = Species)) + geom_rug(alpha = .5, sides = 'tr', show.legend = F) + theme(legend.position = 'bottom')
- Load
ggExtra
and input the...