Title: | Add Marginal Histograms to 'ggplot2', and More 'ggplot2' Enhancements |
---|---|
Description: | Collection of functions and layers to enhance 'ggplot2'. The flagship function is 'ggMarginal()', which can be used to add marginal histograms/boxplots/density plots to 'ggplot2' scatterplots. |
Authors: | Dean Attali [aut, cre], Christopher Baker [aut] |
Maintainer: | Dean Attali <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.10.1.9000 |
Built: | 2024-11-02 04:43:38 UTC |
Source: | https://github.com/daattali/ggextra |
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 )
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)
## 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)
This gadget and addin allow you to select a ggplot2 plot and interactively
use ggMarginal
to build marginal plots on top of your scatterplot.
ggMarginalGadget(plot)
ggMarginalGadget(plot)
plot |
A ggplot2 scatterplot |
An object of class ggExtraPlot
. This object can be printed to
show the marginal plots or saved using any of the typical image-saving functions
To use the RStudio addin, highlight the code for a plot in RStudio and
select ggplot2 Marginal Plots from the RStudio Addins menu. This will
embed the marginal plots code into your script. Alternatively, you can call
ggMarginalGadget()
with a ggplot2 plot, and the gadget will return
a plot object.
if (interactive()) { plot <- ggplot2::ggplot(mtcars, ggplot2::aes(wt, mpg)) + ggplot2::geom_point() plot2 <- ggMarginalGadget(plot) }
if (interactive()) { plot <- ggplot2::ggplot(mtcars, ggplot2::aes(wt, mpg)) + ggplot2::geom_point() plot2 <- ggMarginalGadget(plot) }
Create a bar plot of count (frequency) data that is stored in a data.frame or table.
plotCount(x, ...)
plotCount(x, ...)
x |
A data.frame or table. See 'Details' for more information. |
... |
Extra parameters to pass to the barplot. Any parameter that
|
The argument to this function is expected to be either a data.frame or a table.
If a data.frame is provided, it must have exactly two columns: the first column contains the unique values in the data, and the second column is the corresponding integer frequencies to each value.
If a table is provided, it must have exactly one row: the rownames are the unique values in the data, and the row values are the corresponding integer frequencies to each value.
A ggplot2 object that can be have more layers added onto it.
plotCount(table(infert$education)) df <- data.frame("vehicle" = c("bicycle", "car", "unicycle", "Boeing747"), "NumWheels" = c(2, 4, 1, 16)) plotCount(df) + removeGridX()
plotCount(table(infert$education)) df <- data.frame("vehicle" = c("bicycle", "car", "unicycle", "Boeing747"), "NumWheels" = c(2, 4, 1, 16)) plotCount(df) + removeGridX()
Remove grid lines from a ggplot2 plot, to have a cleaner and simpler plot
removeGrid(x = TRUE, y = TRUE) removeGridX() removeGridY()
removeGrid(x = TRUE, y = TRUE) removeGridX() removeGridY()
x |
Whether to remove grid lines from the x axis. |
y |
Whether to remove grid lines from the y axis. |
Minor grid lines are always removed.
removeGrid
removes the major grid lines from the x and/or y axis
(both by default).
removeGridX
is a shortcut for removeGrid(x = TRUE, y = FALSE)
removeGridY
is a shortcut for removeGrid(x = FALSE, y = TRUE)
A ggplot2 layer that can be added to an existing ggplot2 object.
df <- data.frame(x = 1:50, y = 1:50) p <- ggplot2::ggplot(df, ggplot2::aes(x, y)) + ggplot2::geom_point() p + removeGrid() p + removeGrid(y = FALSE) p + removeGridX()
df <- data.frame(x = 1:50, y = 1:50) p <- ggplot2::ggplot(df, ggplot2::aes(x, y)) + ggplot2::geom_point() p + removeGrid() p + removeGrid(y = FALSE) p + removeGridX()
Rotate the labels on the x axis to be rotated so that they are vertical, which is often useful when there are many overlapping labels along the x axis.
rotateTextX(angle = 90, hjust = 1, vjust = 0.5)
rotateTextX(angle = 90, hjust = 1, vjust = 0.5)
angle |
Angle (in [0, 360]) |
hjust |
Horizontal justification (in [0, 1]) |
vjust |
Vertical justification (in [0, 1]) |
This function is quite simple, but it can be useful if you don't have the exact syntax to do this engraved in your head.
A ggplot2 layer that can be added to an existing ggplot2 object.
df <- data.frame(x = paste("Letter", LETTERS, sep = "_"), y = seq_along(LETTERS)) p <- ggplot2::ggplot(df, ggplot2::aes(x, y)) + ggplot2::geom_point() p + rotateTextX()
df <- data.frame(x = paste("Letter", LETTERS, sep = "_"), y = seq_along(LETTERS)) p <- ggplot2::ggplot(df, ggplot2::aes(x, y)) + ggplot2::geom_point() p + rotateTextX()
Launch a Shiny app that shows a demo of what can be done with
ggExtra::ggMarginal
.
runExample()
runExample()
This example is also available online.
## Only run this example in interactive R sessions if (interactive()) { runExample() }
## Only run this example in interactive R sessions if (interactive()) { runExample() }