ggMarginal | R Documentation |
Create a ggplot2 scatterplot with marginal density plots (default) or histograms, or add the marginal plots to an existing scatterplot.
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 )
p |
A ggplot2 scatterplot to add marginal plots to. If |
data |
The data.frame to use for creating the marginal plots. Ignored
if |
x |
The name of the variable along the x axis. Ignored if |
y |
The name of the variable along the y axis. Ignored if |
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
|
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 |
groupFill |
If |
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()
).
The grid
and gtable
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.
## Not run: 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) ## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.