
上QQ阅读APP看书,第一时间看更新
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 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.