How to create a marginal plot in R?
How to create a marginal plot in R?, A scatterplot with histograms, boxplots, or dot plots in the x- and y-axes is known as a marginal plot.
It enables the investigation of the relationship between two numeric variables.
The base plot depicts the relationship between the variables on the x and y axes. A scatterplot or a density plot is commonly used.
Cluster Sampling in R With Examples »
The marginal charts are frequently plotted on the top and right margins of the base plot, and they use a histogram, barplot, or density plot to depict the distribution of x and y axes variables.
This allows us to see the strength of the distribution for different values of variables along both axes.
The ggExtra package of the R Language will be used to plot a marginal plot in the R Language.
The ggExtra is a set of methods and layers that extend the capabilities of ggplot2.
To add marginal histograms/boxplots/density plots to ggplot2 scatterplots, use the ggMarginal() function.
Install the ggExtra package first as follows: Type the following R code:
install.packages("ggExtra");
How to create a marginal plot in R
library("ggExtra") library(ggplot2) library(ggpubr) p <- ggscatter(iris, x = "Sepal.Length", y = "Sepal.Width", color = "Species", palette = "jco", size = 3, alpha = 0.6) ggMarginal(p, type = "density")
![source: imgur.com](https://i.imgur.com/YIKnovu.png)
Change marginal plot type
ggMarginal(p, type = “boxplot”)
![source: imgur.com](https://i.imgur.com/OlriEYn.png)
In the scatter plot and marginal plots, one restriction of ggExtra is that it cannot handle multiple groups. The cowplot package is used in the R code below to propose a solution.
Cluster Sampling in R-Cluster or area sampling in a nutshell »
Scatter plot colored by groups (“Species”)
sp <- ggscatter(iris, x = "Sepal.Length", y = "Sepal.Width", color = "Species", palette = "jco", size = 3, alpha = 0.6)+ border()
Marginal density plot of x (top panel) and y (right panel)
xplot <- ggdensity(iris, "Sepal.Length", fill = "Species", palette = "jco")
![source: imgur.com](https://i.imgur.com/JeI9OjD.png)
yplot <- ggdensity(iris, "Sepal.Width", fill = "Species", palette = "jco")+ rotate()
![source: imgur.com](https://i.imgur.com/rMZ1cYs.png)
Cleaning the plots
sp <- sp + rremove("legend") yplot <- yplot + clean_theme() + rremove("legend")
![source: imgur.com](https://i.imgur.com/t5cOVxY.png)
xplot <- xplot + clean_theme() + rremove("legend")
![source: imgur.com](https://i.imgur.com/p3AFk4W.png)
Arranging the plot using cowplot
Customer Segmentation K Means Cluster »
library(cowplot) plot_grid(xplot, NULL, sp, yplot, ncol = 2, align = "hv", rel_widths = c(2, 1), rel_heights = c(1, 2))
![source: imgur.com](https://i.imgur.com/gdtCsox.png)
Add marginal boxplot
Scatter plot colored by groups (“Species”)
sp <- ggscatter(iris, x = "Sepal.Length", y = "Sepal.Width", color = "Species", palette = "jco", size = 3, alpha = 0.6, ggtheme = theme_bw())
![source: imgur.com](https://i.imgur.com/MhCoOKS.png)
Marginal boxplot of x (top panel) and y (right panel)
xplot <- ggboxplot(iris, x = "Species", y = "Sepal.Length", color = "Species", fill = "Species", palette = "jco", alpha = 0.5, ggtheme = theme_bw())+ rotate()
![source: imgur.com](https://i.imgur.com/C9BV8yz.png)
yplot <- ggboxplot(iris, x = "Species", y = "Sepal.Width", color = "Species", fill = "Species", palette = "jco", alpha = 0.5, ggtheme = theme_bw())
![source: imgur.com](https://i.imgur.com/Zc24YYT.png)
Cleaning the plots
sp <- sp + rremove("legend") yplot <- yplot + clean_theme() + rremove("legend")
![source: imgur.com](https://i.imgur.com/4npQpIg.png)
xplot <- xplot + clean_theme() + rremove("legend")
![source: imgur.com](https://i.imgur.com/GHz5IGZ.png)
Arranging the plot using cowplot
library(cowplot) plot_grid(xplot, NULL, sp, yplot, ncol = 2, align = "hv", rel_widths = c(2, 1), rel_heights = c(1, 2))
![source: imgur.com](https://i.imgur.com/Q4g0yTQ.png)
The inclusion of extra spaces between the main plot and the marginal density plots is the problem with the aforementioned plots.
Cluster Analysis in R » Unsupervised Approach »
Claus Wilke recently shared the following approach for making a flawless scatter plot with marginal density plots or histogram plots in a tweet:
library(cowplot) pmain <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species))+ geom_point()+ ggpubr::color_palette("jco")
![source: imgur.com](https://i.imgur.com/eqRd4Tk.png)
Marginal densities along x-axis
xdens <- axis_canvas(pmain, axis = "x")+ geom_density(data = iris, aes(x = Sepal.Length, fill = Species), alpha = 0.7, size = 0.2)+ ggpubr::fill_palette("jco")
Marginal densities along y-axis
Need to set coord_flip = TRUE, if you plan to use coord_flip()
ydens <- axis_canvas(pmain, axis = "y", coord_flip = TRUE)+ geom_density(data = iris, aes(x = Sepal.Width, fill = Species), alpha = 0.7, size = 0.2)+ coord_flip()+ ggpubr::fill_palette("jco") p1 <- insert_xaxis_grob(pmain, xdens, grid::unit(.2, "null"), position = "top") p2<- insert_yaxis_grob(p1, ydens, grid::unit(.2, "null"), position = "right") ggdraw(p2)
![source: imgur.com](https://i.imgur.com/IoB8BgT.png)