Learn R Programming

ggExtra (version 0.10.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", "violin", "densigram"),
  margins = c("both", "x", "y"),
  size = 5,
  ...,
  xparams = list(),
  yparams = list(),
  groupColour = FALSE,
  groupFill = FALSE
)

Value

An object of class ggExtraPlot. This object can be printed to show the plots or saved using any of the typical image-saving functions (for example, using png() or pdf()).

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. Ignored if p is provided.

x

The name of the variable along the x axis. Ignored if p is provided.

y

The name of the variable along the y axis. Ignored if p is provided.

type

What type of marginal plot to show. One of: [density, histogram, boxplot, violin, densigram] (a "densigram" is when a density plot is overlaid on a histogram).

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_histogram(), geom_boxplot(), or geom_violin() accepts can be used. For example, colour = "red" can be used for any marginal plot type, and binwidth = 10 can be used for histograms.

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.

groupColour

If TRUE, the colour (or outline) of the marginal plots will be grouped according to the variable mapped to colour in the scatter plot. The variable mapped to colour in the scatter plot must be a character or factor variable. See examples below.

groupFill

If TRUE, the fill of the marginal plots will be grouped according to the variable mapped to colour in the scatter plot. The variable mapped to colour in the scatter plot must be a character or factor variable. See examples below.

See Also

Examples

Run this code
if (FALSE) {
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)
}

Run the code above in your browser using DataLab