How To Make Scatterplot with Marginal Histograms in R?
In this article, we will discuss how to make a Scatterplot with Marginal Histograms in the R Language.
To do so we will use the ggExtra package of the R Language. The ggExtra is a collection of functions and layers to enhance ggplot2. The ggMarginal() function can be used to add marginal histograms/boxplots/density plots to ggplot2 scatterplots.
To install the ggExtra package we use:
After installation, we can load the package and use the following function to make a marginal histogram with a scatter plot.
Syntax: ggMarginal( plot, type=”histogram” )
Creating basic scatter plot with marginal histogram:
Here, is a basic scatter plot with marginal histogram using the ggMarginal function of ggExtra package.
Color scatter plot with marginal histogram by group:
To color scatter plot by the group we use the col parameter of ggplot() function. To color the marginal histogram by the group we use groupColour and groupfill as true.
Syntax: ggMarginal( plot, type=”histogram”, groupColour = TRUE, groupFill = TRUE )
Example: Here, we have a scatter plot with marginal histograms both colored by the group. We use boolean values for groupColor and groupFill according to formatting preference.