Marginal plot for only one axis
Sometimes we need a marginal plot at only one axis either the x-axis or the y-axis. In that situation, we use the margins parameter of the gg-marginal () function. The axis where we want the marginal plot to appear is given as the value of argument margins.
Syntax: ggMarginal( plot, type, margins )
Parameters:
- plot: Determines the base scatter plot over which marginal plot has to be added.
- type: Determines the type of marginal plot i.e. histogram, boxplot, and density.
- margins: Determines the axis where the marginal plot is required
Example: Here, are two plots one with the marginal plot on the x-axis other on the y-axis.
R
# load library tidyverse, gridExtra and ggExtra library (tidyverse) library (ggExtra) library (gridExtra) # set theme theme_set ( theme_bw (12)) # create x and y vector xAxis <- rnorm (1000) yAxis <- rnorm (1000) + xAxis + 10 # create sample data frame sample_data <- data.frame (xAxis, yAxis) # create scatter plot using ggplot() function plot <- ggplot (sample_data, aes (x=xAxis, y=yAxis))+ geom_point ()+ theme (legend.position= "none" ) # use ggMarginal function to create # marginal histogram on x-axis plot1 <- ggMarginal (plot, type= "histogram" , margins= 'x' ) # use ggMarginal function to # create marginal histogram on y-axis plot2 <- ggMarginal (plot, type= "histogram" , margins= 'y' ) # combine plots in a grid grid.arrange ( plot1, plot2, ncol=2) |
Output:
- xAxis and yAxis are two vectors produced by the rnorm function. The yAxis vector is created by summing the values from xAxis and adding 10 to each value, whereas the xAxis vector contains 1000 random integers drawn from a typical normal distribution.
- The data. frame function is used to build the sample_data data frame, containing the columns xAxis and yAxis.
- The ggplot function from ggplot2 is used to build the plot object. It states that the x values are taken from the sample_data’s xAxis column and the y values are taken from the yAxis column. The data points are plotted in a scatter plot using the geom_point function. The legend is dropped from the story by setting the legend. position to “none” in the theme function.
- The two more plots are built using the plot object and the ggMarginal method from ggExtra. With the type set to “histogram” and the margins parameter set to “x,” the initial call to ggMarginal generates a marginal histogram on the x-axis. With the type set to “histogram” and the margins parameter set to “y,” the second function generates a marginal histogram on the y-axis.
The two plots (plot 1 and plot 2) are combined into a grid using the grid. arrange function from gridExtra. The plots are organized in 2 columns using the ncol option, which is set to 2.
R ggplot2 – Marginal Plots
A marginal plot is a scatterplot that has histograms, boxplots, or dot plots in the margins of the x- and y-axes. It allows studying the relationship between 2 numeric variables. The base plot visualizes the correlation between the x and y axes variables. It is usually a scatterplot or a density plot. The marginal charts are commonly plotted on the top and right margin of the base plot and they show the distribution of x and y axes variables using a histogram, barplot, or density plot. This helps us to visualize the distribution intensity at different values of variables along both axes.
To plot a marginal plot in the R Language, 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.