ggExtra

📊 Add marginal histograms to ggplot2, and more ggplot2 enhancements

https://github.com/daattali/ggextra

Science Score: 13.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • â—‹
    CITATION.cff file
  • âś“
    codemeta.json file
    Found codemeta.json file
  • â—‹
    .zenodo.json file
  • â—‹
    DOI references
  • â—‹
    Academic publication links
  • â—‹
    Committers with academic emails
  • â—‹
    Institutional organization owner
  • â—‹
    JOSS paper metadata
  • â—‹
    Scientific vocabulary similarity
    Low similarity (12.6%) to scientific vocabulary

Keywords

ggplot2 ggplot2-enhancements marginal-plots r r-package rstats

Keywords from Contributors

predict standardization report
Last synced: 6 months ago · JSON representation

Repository

📊 Add marginal histograms to ggplot2, and more ggplot2 enhancements

Basic Info
Statistics
  • Stars: 389
  • Watchers: 17
  • Forks: 46
  • Open Issues: 10
  • Releases: 5
Topics
ggplot2 ggplot2-enhancements marginal-plots r r-package rstats
Created almost 11 years ago · Last pushed 6 months ago
Metadata Files
Readme Funding License

README.md

ggExtra - Add marginal histograms to ggplot2, and more ggplot2 enhancements

CRAN
version CI
build

Copyright 2016 Dean Attali. Licensed under the MIT license.

ggExtra is a 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. You can view a live interactive demo to test it out!

Most other functions/layers are quite simple but are useful because they are fairly common ggplot2 operations that are a bit verbose.

This is an instructional document, but I also wrote a blog post about the reasoning behind and development of this package.

Note: it was brought to my attention that several years ago there was a different package called ggExtra, by Baptiste (the author of gridExtra). That old ggExtra package was deleted in 2011 (two years before I even knew what R is!), and this package has nothing to do with the old one.

Installation

ggExtra is available through both CRAN and GitHub.

To install the CRAN version:

install.packages("ggExtra")

To install the latest development version from GitHub:

install.packages("devtools")
devtools::install_github("daattali/ggExtra")

Marginal plots RStudio addin/gadget

ggExtra comes with an addin for ggMarginal(), which lets you interactively add marginal plots to a scatter plot. To use it, simply highlight the code for a ggplot2 plot in your script, and select ggplot2 Marginal Plots from the RStudio Addins menu. Alternatively, you can call the addin directly by calling ggMarginalGadget(plot) with a ggplot2 plot.

ggMarginal gadget screenshot

Usage

We’ll first load the package and ggplot2, and then see how all the functions work.

library("ggExtra")
library("ggplot2")

ggMarginal - Add marginal histograms/boxplots/density plots to ggplot2 scatterplots

ggMarginal() is an easy drop-in solution for adding marginal density plots/histograms/boxplots to a ggplot2 scatterplot. The easiest way to use it is by simply passing it a ggplot2 scatter plot, and ggMarginal() will add the marginal plots.

As a simple first example, let’s create a dataset with 500 points where the x values are normally distributed and the y values are uniformly distributed, and plot a simple ggplot2 scatterplot.

set.seed(30)
df1 <- data.frame(x = rnorm(500, 50, 10), y = runif(500, 0, 50))
p1 <- ggplot(df1, aes(x, y)) + geom_point() + theme_bw()
p1

And now to add marginal density plots:

ggMarginal(p1)

That was easy. Notice how the syntax does not follow the standard ggplot2 syntax - you don’t “add” a ggMarginal layer with p1 + ggMarginal(), but rather ggMarginal takes the object as an argument and returns a different object. This means that you can use magrittr pipes, for example p1 %>% ggMarginal().

Let’s make the text a bit larger to make it easier to see.

ggMarginal(p1 + theme_bw(30) + ylab("Two\nlines"))

Notice how the marginal plots occupy the correct space; even when the main plot’s points are pushed to the right because of larger text or longer axis labels, the marginal plots automatically adjust.

If your scatterplot has a factor variable mapping to a colour (ie. points in the scatterplot are colour-coded according to a variable in the data, by using aes(colour = ...)), then you can use groupColour = TRUE and/or groupFill = TRUE to reflect these groupings in the marginal plots. The result is multiple marginal plots, one for each colour group of points. Here’s an example using the iris dataset.

piris <- ggplot(iris, aes(Sepal.Length, Sepal.Width, colour = Species)) +
  geom_point()
ggMarginal(piris, groupColour = TRUE, groupFill = TRUE)

You can also show histograms instead.

ggMarginal(p1, type = "histogram")

There are several more parameters, here is an example with a few more being used. Note that you can use any parameters that the geom_XXX() layers accept, such as col and fill, and they will be passed to these layers.

ggMarginal(p1, margins = "x", size = 2, type = "histogram",
           col = "blue", fill = "orange")

In the above example, size = 2 means that the main scatterplot should occupy twice as much height/width as the margin plots (default is 5). The col and fill parameters are simply passed to the ggplot layer for both margin plots.

If you want to specify some parameter for only one of the marginal plots, you can use the xparams or yparams parameters, like this:

ggMarginal(p1, type = "histogram", xparams = list(binwidth = 1, fill = "orange"))

Last but not least - you can also save the output from ggMarginal() and display it later. (This may sound trivial, but it was not an easy problem to solve - see this discussion).

p <- ggMarginal(p1)
p

You can also create marginal box plots and violin plots. For more information, see ?ggExtra::ggMarginal.

Using ggMarginal() in R Notebooks or Rmarkdown

If you try including a ggMarginal() plot inside an R Notebook or Rmarkdown code chunk, you’ll notice that the plot doesn’t get output. In order to get a ggMarginal() to show up in an these contexts, you need to save the ggMarginal plot as a variable in one code chunk, and explicitly print it using the grid package in another chunk, like this:

```{r}
library(ggplot2)
library(ggExtra)
p <- ggplot(mtcars, aes(wt, mpg)) + geom_point()
p <- ggMarginal(p)
```
```{r}
grid::grid.newpage()
grid::grid.draw(p)
```

removeGrid - Remove grid lines from ggplot2

This is just a convenience function to save a bit of typing and memorization. Minor grid lines are always removed, and the major x or y grid lines can be removed as well (default is to remove both).

removeGridX is a shortcut for removeGrid(x = TRUE, y = FALSE), and removeGridY is similarly a shortcut for… .

df2 <- data.frame(x = 1:50, y = 1:50)
p2 <- ggplot2::ggplot(df2, ggplot2::aes(x, y)) + ggplot2::geom_point()
p2 + removeGrid()

For more information, see ?ggExtra::removeGrid.

rotateTextX - Rotate x axis labels

Often times it is useful to rotate the x axis labels to be vertical if there are too many labels and they overlap. This function accomplishes that and ensures the labels are horizontally centered relative to the tick line.

df3 <- data.frame(x = paste("Letter", LETTERS, sep = "_"),
                  y = seq_along(LETTERS))
p3 <- ggplot2::ggplot(df3, ggplot2::aes(x, y)) + ggplot2::geom_point()
p3 + rotateTextX()

For more information, see ?ggExtra::rotateTextX.

plotCount - Plot count data with ggplot2

This is a convenience function to quickly plot a bar plot of count (frequency) data. The input must be either a frequency table (obtained with base::table) or a data.frame with 2 columns where the first column contains the values and the second column contains the counts.

An example using a table:

plotCount(table(infert$education))

An example using a data.frame:

df4 <- data.frame("vehicle" = c("bicycle", "car", "unicycle", "Boeing747"),
                  "NumWheels" = c(2, 4, 1, 16))
plotCount(df4) + removeGridX()

For more information, see ?ggExtra::plotCount.

Owner

  • Name: Dean Attali
  • Login: daattali
  • Kind: user
  • Location: Toronto
  • Company: AttaliTech Ltd

Founder & Lead R-Shiny consultant @ AttaliTech Ltd. Passionate about writing open source tools. Extreme traveller.

GitHub Events

Total
  • Issues event: 3
  • Watch event: 10
  • Issue comment event: 4
  • Push event: 4
  • Fork event: 1
Last Year
  • Issues event: 3
  • Watch event: 10
  • Issue comment event: 4
  • Push event: 4
  • Fork event: 1

Committers

Last synced: 9 months ago

All Time
  • Total Commits: 234
  • Total Committers: 5
  • Avg Commits per committer: 46.8
  • Development Distribution Score (DDS): 0.201
Past Year
  • Commits: 1
  • Committers: 1
  • Avg Commits per committer: 1.0
  • Development Distribution Score (DDS): 0.0
Top Committers
Name Email Commits
Dean Attali d****i@g****m 187
Chris Baker c****r@g****m 44
Luke Smith l****i@s****t 1
Indrajeet Patil p****e@g****m 1
Alanocallaghan a****n@o****m 1
Committer Domains (Top 20 + Academic)

Issues and Pull Requests

Last synced: 6 months ago

All Time
  • Total issues: 74
  • Total pull requests: 32
  • Average time to close issues: 4 months
  • Average time to close pull requests: 5 days
  • Total issue authors: 42
  • Total pull request authors: 3
  • Average comments per issue: 4.08
  • Average comments per pull request: 3.53
  • Merged pull requests: 30
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 1
  • Pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Issue authors: 1
  • Pull request authors: 0
  • Average comments per issue: 0.0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • crew102 (16)
  • daattali (8)
  • IndrajeetPatil (7)
  • mikejiang (2)
  • wpetry (2)
  • PatrickOStats (2)
  • Sim19 (2)
  • jessexknight (1)
  • lavkan (1)
  • Yogesh1-11 (1)
  • flying-sheep (1)
  • prost2 (1)
  • giuseppec (1)
  • scbrown86 (1)
  • taylorreiter (1)
Pull Request Authors
  • crew102 (30)
  • jarauh (1)
  • IndrajeetPatil (1)
Top Labels
Issue Labels
enhancement (3) help wanted (3)
Pull Request Labels

Packages

  • Total packages: 2
  • Total downloads:
    • cran 9,735 last-month
  • Total docker downloads: 55,953
  • Total dependent packages: 32
    (may contain duplicates)
  • Total dependent repositories: 73
    (may contain duplicates)
  • Total versions: 18
  • Total maintainers: 1
cran.r-project.org: ggExtra

Add Marginal Histograms to 'ggplot2', and More 'ggplot2' Enhancements

  • Versions: 15
  • Dependent Packages: 30
  • Dependent Repositories: 68
  • Downloads: 9,735 Last month
  • Docker Downloads: 55,953
Rankings
Stargazers count: 1.0%
Forks count: 1.5%
Dependent packages count: 2.6%
Dependent repos count: 2.9%
Downloads: 4.0%
Average: 5.9%
Docker downloads count: 23.4%
Maintainers (1)
Last synced: 6 months ago
conda-forge.org: r-ggextra
  • Versions: 3
  • Dependent Packages: 2
  • Dependent Repositories: 5
Rankings
Dependent repos count: 14.8%
Dependent packages count: 19.6%
Average: 20.2%
Stargazers count: 20.6%
Forks count: 25.7%
Last synced: 6 months ago

Dependencies

DESCRIPTION cran
  • R >= 3.1.0 depends
  • R6 * imports
  • colourpicker >= 1.0 imports
  • ggplot2 >= 2.2.0 imports
  • grDevices * imports
  • grid >= 3.1.3 imports
  • gtable >= 0.2.0 imports
  • miniUI >= 0.1.1 imports
  • scales >= 0.2.0 imports
  • shiny >= 0.13.0 imports
  • shinyjs >= 0.5.2 imports
  • utils * imports
  • devtools * suggests
  • fontquiver * suggests
  • knitr >= 1.7 suggests
  • rmarkdown * suggests
  • rstudioapi >= 0.5 suggests
  • svglite * suggests
  • testthat * suggests
  • vdiffr * suggests
  • withr * suggests
.github/workflows/build.yml actions
  • actions/checkout v2 composite