How to do it...

  1. 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')
  1. Load ggExtra and input the ggplot object into ggMarginal() function:
library(ggExtra)
ggMarginal(scatter, iris, type = 'histogram', bins = 150)

The result is shown by figure 2.9:

Figure 2.9 - Marginal histograms by the scatterplots.