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")
Change marginal plot type
ggMarginal(p, type = “boxplot”)
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")
yplot <- ggdensity(iris, "Sepal.Width", fill = "Species", palette = "jco")+ rotate()
Cleaning the plots
sp <- sp + rremove("legend") yplot <- yplot + clean_theme() + rremove("legend")
xplot <- xplot + clean_theme() + rremove("legend")
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))
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())
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()
yplot <- ggboxplot(iris, x = "Species", y = "Sepal.Width", color = "Species", fill = "Species", palette = "jco", alpha = 0.5, ggtheme = theme_bw())
Cleaning the plots
sp <- sp + rremove("legend") yplot <- yplot + clean_theme() + rremove("legend")
xplot <- xplot + clean_theme() + rremove("legend")
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))
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")
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)