ggExtra (version 0.2.0)

ggMarginal: Add marginal density/histogram to ggplot2 scatterplots

Description

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.

See Also

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

Examples

Run this code
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)
    }
  }
}

Run the code above in your browser using DataCamp Workspace