A scatterplot is a graph in which the values of **two variables** are plotted along two axes, the pattern of the resulting points revealing any **correlation** present.

It is sometimes important to check the **distribution of variables** of the X and Y axis. This can be done by adding **marginal plots**. The ggExtra library allows one to do it easily thanks to Dean Attali.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# library library(ggplot2) library(ggExtra) # The mtcars dataset is proposed in R head(mtcars) # classic plot : p=ggplot(mtcars, aes(x=wt, y=mpg, color=cyl, size=cyl)) + geom_point() + theme(legend.position="none") # with marginal histogram ggMarginal(p, type="histogram") # marginal density ggMarginal(p, type="density") # marginal boxplot ggMarginal(p, type="boxplot") |

Note that you can customize the margins’ width or their features, and you can show the margin for only one axis.

1 2 3 4 5 6 7 8 9 10 |
# Set relative size of marginal plots (main plot 10x bigger than marginals) ggMarginal(p, type="histogram", size=10) # Custom marginal plots: ggMarginal(p, type="histogram", fill = "slateblue", xparams = list( bins=10)) # Show only marginal plot for x axis ggMarginal(p, margins = 'x', color="purple", size=4) |

Not what you are looking for ? Make a new search !

## Leave a Reply