ggMarginal

0th

Percentile

Add marginal density/histogram to ggplot2 scatterplots

Create a ggplot2 scatterplot with marginal density plots (default) or histograms, or add the marginal plots to an existing scatterplot.

Usage
ggMarginal(p, data, x, y, type = c("density", "histogram", "boxplot"),
  margins = c("both", "x", "y"), size = 5, ..., xparams, yparams)
Arguments
p
A ggplot2 scatterplot to add marginal plots to. If p is not provided, then all of data, x, and y must be provided.
data
The data.frame to use for creating the marginal plots. Optional if p is provided and the marginal plots are reflecting the same data.
x
The name of the variable along the x axis. Optional if p is provided and the x aesthetic is set in the main plot.
y
The name of the variable along the y axis. Optional if p is provided and the y aesthetic is set in the main plot.
type
What type of marginal plot to show. One of: [density, histogram, boxplot].
margins
Along which margins to show the plots. One of: [both, x, y].
size
Integer describing the relative size of the marginal plots compared to the main plot. A size of 5 means that the main plot is 5x wider and 5x taller than the marginal plots.
...
Extra parameters to pass to the marginal plots. Any parameter that geom_line(), geom_bar(), or geom_boxplot() accept can be used. For example, colour = "red" can be used for any marginal plot type, an
xparams
List of extra parameters to use only for the marginal plot along the x axis.
yparams
List of extra parameters to use only for the marginal plot along the y axis.
Value

  • An object of class ggExtraPlot. This extra class gets added onto a ggplot2 object in order for the print generic to easily work with this object. This means that the return value from this function can be printed or saved for later.

Note

The grid and gridExtra packages are required for this function. Since the size parameter is used by ggMarginal, if you want to pass a size to the marginal plots, you cannot use the ... parameter. Instead, you must pass size to both xparams and yparams. For example, ggMarginal(p, size = 2) will change the size of the main vs marginal plot, while ggMarginal(p, xparams = list(size=2), yparams = list(size=2)) will make the density plot outline thicker.

See Also

http://daattali.com/shiny/ggExtra-ggMarginal-demo/{Demo Shiny app}

Aliases
  • ggMarginal
Examples
if (requireNamespace("ggplot2", quietly = TRUE)) {
  if (requireNamespace("gridExtra", quietly = TRUE)) {
    if (requireNamespace("grid", quietly = TRUE)) {
      p <- ggplot2::ggplot(mtcars, ggplot2::aes(wt, mpg)) + ggplot2::geom_point()
      ggMarginal(p)

      set.seed(30)
      df <- data.frame(x = rnorm(500, 50, 10), y = runif(500, 0, 50))
      p2 <- ggplot2::ggplot(df, ggplot2::aes(x, y)) + ggplot2::geom_point()
      ggMarginal(p2)
      ggMarginal(p2, type = "histogram")
      ggMarginal(p2, margins = "x")
      ggMarginal(p2, size = 2)
      ggMarginal(p2, colour = "red")
      ggMarginal(p2, colour = "red", xparams = list(colour = "blue", size = 3))
      p2 <- p2 + ggplot2::ggtitle("Random data") + ggplot2::theme_bw(30)
      ggMarginal(p2)

      ggMarginal(data = df, x = "x", y = "y")

      set.seed(30)
      df2 <- data.frame(x = c(rnorm(250, 50, 10), rnorm(250, 100, 10)),
                        y = runif(500, 0, 50))
      p3 <- ggplot2::ggplot(df2, ggplot2::aes(x, y)) + ggplot2::geom_point()
      ggMarginal(p3)
    }
  }
}
Documentation reproduced from package ggExtra, version 0.1.5.2, License: MIT + file LICENSE

Community examples

cjsantanav@zoho.com at Jul 10, 2020 ggExtra v0.9

library(ggplot2) # basic usage p <- ggplot(mtcars, aes(wt, mpg)) + geom_point() ggMarginal(p) # using some parameters set.seed(30) df <- data.frame(x = rnorm(500, 50, 10), y = runif(500, 0, 50)) p2 <- ggplot(df, aes(x, y)) + geom_point() ggMarginal(p2) ggMarginal(p2, type = "histogram") ggMarginal(p2, margins = "x") ggMarginal(p2, size = 2) ggMarginal(p2, colour = "red") ggMarginal(p2, colour = "red", xparams = list(colour = "blue", size = 3)) ggMarginal(p2, type = "histogram", bins = 10) # Using violin plot ggMarginal(p2, type = "violin") # Using a "densigram" plot ggMarginal(p2, type = "densigram") # specifying the data directly instead of providing a plot ggMarginal(data = df, x = "x", y = "y") # more examples showing how the marginal plots are properly aligned even when # the main plot axis/margins/size/etc are changed set.seed(30) df2 <- data.frame(x = c(rnorm(250, 50, 10), rnorm(250, 100, 10)), y = runif(500, 0, 50)) p2 <- ggplot(df2, aes(x, y)) + geom_point() ggMarginal(p2) p2 <- p2 + ggtitle("Random data") + theme_bw(30) ggMarginal(p2) p3 <- ggplot(df2, aes(log(x), y - 500)) + geom_point() ggMarginal(p3) p4 <- p3 + scale_x_continuous(limits = c(2, 6)) + theme_bw(50) ggMarginal(p4) # Using groupColour and groupFill # In order to use either of these arguments, we must map 'colour' in the # scatter plot to a factor or character variable p <- ggplot(mtcars, aes(x = wt, y = drat, colour = factor(vs))) + geom_point() ggMarginal(p, groupColour = TRUE) ggMarginal(p, groupColour = TRUE, groupFill = TRUE) # } # NOT RUN { # }