


ggMarginal(p1, margins = "x", size = 2, type = "histogram", Note that you can use any parameters that the geom_XXX() layers accept, such as col and fill, and they will be passed to these layers. There are several more parameters, here is an example with a few more being used. GgMarginal(piris, groupColour = TRUE, groupFill = TRUE) piris <- ggplot(iris, aes(Sepal.Length, Sepal.Width, colour = Species)) + Here's an example using the iris dataset. The result is multiple marginal plots, one for each colour group of points. )), then you can use groupColour = TRUE and/or groupFill = TRUE to reflect these groupings in the marginal plots. points in the scatterplot are colour-coded according to a variable in the data, by using aes(colour =. If your scatterplot has a factor variable mapping to a colour (ie. Plot's points are pushed to the right because of larger text or longer axis labels, Notice how the marginal plots occupy the correct space even when the main ggMarginal(p1 + theme_bw(30) + ylab("Two\nlines")) Let's make the text a bit larger to make it easier to see. X values are normally distributed and the y values are uniformly distributed,Īnd plot a simple ggplot2 scatterplot. The easiest way to use it is by simply passing it a ggplot2 scatter plot, and ggMarginal() will add the marginal plots.Īs a simple first example, let's create a dataset with 500 points where the GgMarginal() is an easy drop-in solution for adding marginalĭensity plots/histograms/boxplots to a ggplot2 scatterplot. GgMarginal - Add marginal histograms/boxplots/density plots to ggplot2 scatterplots

We'll first load the package and ggplot2, and then see how all the functions work. Alternatively, you can call the addin directly by calling ggMarginalGadget(plot) with a ggplot2 plot.
Ggplot histograms code#
To use it, simply highlight the code for a ggplot2 plot in your script, and select ggplot2 Marginal Plots from the RStudio Addins menu. GgExtra comes with an addin for ggMarginal(), which lets you interactively add marginal plots to a scatter plot.
Ggplot histograms install#
To install the latest development version from GitHub: install.packages("devtools")ĭevtools::install_github("daattali/ggExtra") To install the CRAN version: install.packages("ggExtra") GgExtra is available through both CRAN and GitHub. That old ggExtra package was deleted in 2011 (two years before I even knew what R is!), and this package has nothing to do with the old one. Note: it was brought to my attention that several years ago there was a different package called ggExtra, by Baptiste (the author of gridExtra). This is an instructional document, but I also wrote a blog post about the reasoning Most other functions/layers are quite simple but are useful because they are fairly common ggplot2 operations that are a bit verbose. The flagship function is ggMarginal, which can be used to add marginal histograms/boxplots/density plots to ggplot2 scatterplots.

GgExtra is a collection of functions and layers to enhance ggplot2. $value, mean = mean(.$value), sd = sd(.GgExtra - Add marginal histograms to ggplot2, and more ggplot2 enhancements ggExtra - Add marginal histograms to ggplot2, and more ggplot2 enhancementsĬopyright 2016 Dean Attali. If you want the y-axis to have frequency counts, there are a number of options:įirst simulate some data. To fit both on the same graph, one or other needs to be rescaled so that their areas match. the binwidth times the total number of non-missing observations. The area under a density curve equals 1, and the area under the histogram equals the width of the bars times the sum of their height ie. This has been answered here and partially here.
