{"id":13711255,"url":"https://github.com/daattali/ggExtra","last_synced_at":"2025-05-06T20:32:28.879Z","repository":{"id":29312952,"uuid":"32846312","full_name":"daattali/ggExtra","owner":"daattali","description":"📊 Add marginal histograms to ggplot2, and more ggplot2 enhancements","archived":false,"fork":false,"pushed_at":"2024-06-05T14:22:46.000Z","size":2213,"stargazers_count":387,"open_issues_count":11,"forks_count":45,"subscribers_count":17,"default_branch":"master","last_synced_at":"2025-04-08T14:03:29.915Z","etag":null,"topics":["ggplot2","ggplot2-enhancements","marginal-plots","r","r-package","rstats"],"latest_commit_sha":null,"homepage":"http://daattali.com/shiny/ggExtra-ggMarginal-demo/","language":"R","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/daattali.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"daattali"}},"created_at":"2015-03-25T06:15:00.000Z","updated_at":"2025-03-27T01:08:21.000Z","dependencies_parsed_at":"2023-02-15T14:45:59.950Z","dependency_job_id":"5d42a2f7-f7b5-498a-bfea-db566f728142","html_url":"https://github.com/daattali/ggExtra","commit_stats":{"total_commits":233,"total_committers":6,"mean_commits":"38.833333333333336","dds":0.2532188841201717,"last_synced_commit":"5321e4a70ee7768c96ae7df7111841b3306bc5f0"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/daattali%2FggExtra","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/daattali%2FggExtra/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/daattali%2FggExtra/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/daattali%2FggExtra/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/daattali","download_url":"https://codeload.github.com/daattali/ggExtra/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252764640,"owners_count":21800722,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ggplot2","ggplot2-enhancements","marginal-plots","r","r-package","rstats"],"created_at":"2024-08-02T23:01:06.267Z","updated_at":"2025-05-06T20:32:28.012Z","avatar_url":"https://github.com/daattali.png","language":"R","readme":"# ggExtra - Add marginal histograms to ggplot2, and more ggplot2 enhancements\n\n[![CRAN\nversion](https://www.r-pkg.org/badges/version/ggExtra)](https://cran.r-project.org/package=ggExtra)\n[![CI\nbuild](https://github.com/daattali/ggExtra/actions/workflows/build.yml/badge.svg)](https://github.com/daattali/ggExtra/actions/workflows/build.yml)\n\n\u003e *Copyright 2016 [Dean Attali](https:/deanattali.com). Licensed under\n\u003e the MIT license.*\n\n`ggExtra` is a collection of functions and layers to enhance ggplot2.\nThe flagship function is `ggMarginal`, which can be used to add marginal\nhistograms/boxplots/density plots to ggplot2 scatterplots. You can view\na [live interactive\ndemo](https:/daattali.com/shiny/ggExtra-ggMarginal-demo/) to test it\nout!\n\nMost other functions/layers are quite simple but are useful because they\nare fairly common ggplot2 operations that are a bit verbose.\n\nThis is an instructional document, but I also wrote [a blog\npost](https:/deanattali.com/2015/03/29/ggExtra-r-package/) about the\nreasoning behind and development of this package.\n\nNote: it was brought to my attention that several years ago there was a\ndifferent package called `ggExtra`, by Baptiste (the author of\n`gridExtra`). That old `ggExtra` package was deleted in 2011 (two years\nbefore I even knew what R is!), and this package has nothing to do with\nthe old one.\n\n## Installation\n\n`ggExtra` is available through both CRAN and GitHub.\n\nTo install the CRAN version:\n\n    install.packages(\"ggExtra\")\n\nTo install the latest development version from GitHub:\n\n    install.packages(\"devtools\")\n    devtools::install_github(\"daattali/ggExtra\")\n\n## Marginal plots RStudio addin/gadget\n\n`ggExtra` comes with an addin for `ggMarginal()`, which lets you\ninteractively add marginal plots to a scatter plot. To use it, simply\nhighlight the code for a ggplot2 plot in your script, and select\n*ggplot2 Marginal Plots* from the RStudio *Addins* menu. Alternatively,\nyou can call the addin directly by calling `ggMarginalGadget(plot)` with\na ggplot2 plot.\n\n![ggMarginal gadget screenshot](inst/img/ggmarginal-gadget.png)\n\n## Usage\n\nWe’ll first load the package and ggplot2, and then see how all the\nfunctions work.\n\n    library(\"ggExtra\")\n    library(\"ggplot2\")\n\n## `ggMarginal` - Add marginal histograms/boxplots/density plots to ggplot2 scatterplots\n\n`ggMarginal()` is an easy drop-in solution for adding marginal density\nplots/histograms/boxplots to a ggplot2 scatterplot. The easiest way to\nuse it is by simply passing it a ggplot2 scatter plot, and\n`ggMarginal()` will add the marginal plots.\n\nAs a simple first example, let’s create a dataset with 500 points where\nthe x values are normally distributed and the y values are uniformly\ndistributed, and plot a simple ggplot2 scatterplot.\n\n    set.seed(30)\n    df1 \u003c- data.frame(x = rnorm(500, 50, 10), y = runif(500, 0, 50))\n    p1 \u003c- ggplot(df1, aes(x, y)) + geom_point() + theme_bw()\n    p1\n\n\u003cimg src=\"inst/vignette_files/ggExtra_files/figure-markdown_strict/init-plot-1.png\" style=\"display: block; margin: auto;\" /\u003e\n\nAnd now to add marginal density plots:\n\n    ggMarginal(p1)\n\n\u003cimg src=\"inst/vignette_files/ggExtra_files/figure-markdown_strict/ggmarginal-basic-1.png\" style=\"display: block; margin: auto;\" /\u003e\n\nThat was easy. Notice how the syntax does not follow the standard\nggplot2 syntax - **you don’t “add” a ggMarginal layer with\n`p1 + ggMarginal()`, but rather ggMarginal takes the object as an\nargument** and returns a different object. This means that you can use\nmagrittr pipes, for example `p1 %\u003e% ggMarginal()`.\n\nLet’s make the text a bit larger to make it easier to see.\n\n    ggMarginal(p1 + theme_bw(30) + ylab(\"Two\\nlines\"))\n\n\u003cimg src=\"inst/vignette_files/ggExtra_files/figure-markdown_strict/ggmarginal-large-1.png\" style=\"display: block; margin: auto;\" /\u003e\n\nNotice how the marginal plots occupy the correct space; even when the\nmain plot’s points are pushed to the right because of larger text or\nlonger axis labels, the marginal plots automatically adjust.\n\nIf your scatterplot has a factor variable mapping to a colour (ie.\npoints in the scatterplot are colour-coded according to a variable in\nthe data, by using `aes(colour = ...)`), then you can use\n`groupColour = TRUE` and/or `groupFill = TRUE` to reflect these\ngroupings in the marginal plots. The result is multiple marginal plots,\none for each colour group of points. Here’s an example using the iris\ndataset.\n\n    piris \u003c- ggplot(iris, aes(Sepal.Length, Sepal.Width, colour = Species)) +\n      geom_point()\n    ggMarginal(piris, groupColour = TRUE, groupFill = TRUE)\n\n\u003cimg src=\"inst/vignette_files/ggExtra_files/figure-markdown_strict/ggmarginal-grouping-1.png\" style=\"display: block; margin: auto;\" /\u003e\n\nYou can also show histograms instead.\n\n    ggMarginal(p1, type = \"histogram\")\n\n\u003cimg src=\"inst/vignette_files/ggExtra_files/figure-markdown_strict/ggmarginal-hist-1.png\" style=\"display: block; margin: auto;\" /\u003e\n\nThere are several more parameters, here is an example with a few more\nbeing used. Note that you can use any parameters that the `geom_XXX()`\nlayers accept, such as `col` and `fill`, and they will be passed to\nthese layers.\n\n    ggMarginal(p1, margins = \"x\", size = 2, type = \"histogram\",\n               col = \"blue\", fill = \"orange\")\n\n\u003cimg src=\"inst/vignette_files/ggExtra_files/figure-markdown_strict/ggmarginal-params-1.png\" style=\"display: block; margin: auto;\" /\u003e\n\nIn the above example, `size = 2` means that the main scatterplot should\noccupy twice as much height/width as the margin plots (default is 5).\nThe `col` and `fill` parameters are simply passed to the ggplot layer\nfor both margin plots.\n\nIf you want to specify some parameter for only one of the marginal\nplots, you can use the `xparams` or `yparams` parameters, like this:\n\n    ggMarginal(p1, type = \"histogram\", xparams = list(binwidth = 1, fill = \"orange\"))\n\n\u003cimg src=\"inst/vignette_files/ggExtra_files/figure-markdown_strict/ggmarginal-extraparams-1.png\" style=\"display: block; margin: auto;\" /\u003e\n\nLast but not least - you can also save the output from `ggMarginal()`\nand display it later. (This may sound trivial, but it was not an easy\nproblem to solve - [see this\ndiscussion](https:/stackoverflow.com/questions/29062766/store-output-from-gridextragrid-arrange-into-an-object)).\n\n    p \u003c- ggMarginal(p1)\n    p\n\n\u003cimg src=\"inst/vignette_files/ggExtra_files/figure-markdown_strict/ggmarginal-save-1.png\" style=\"display: block; margin: auto;\" /\u003e\n\nYou can also create marginal box plots and violin plots. For more\ninformation, see `?ggExtra::ggMarginal`.\n\n#### Using `ggMarginal()` in R Notebooks or Rmarkdown\n\nIf you try including a `ggMarginal()` plot inside an R Notebook or\nRmarkdown code chunk, you’ll notice that the plot doesn’t get output. In\norder to get a `ggMarginal()` to show up in an these contexts, you need\nto save the ggMarginal plot as a variable in one code chunk, and\nexplicitly print it using the `grid` package in another chunk, like\nthis:\n\n    ```{r}\n    library(ggplot2)\n    library(ggExtra)\n    p \u003c- ggplot(mtcars, aes(wt, mpg)) + geom_point()\n    p \u003c- ggMarginal(p)\n    ```\n    ```{r}\n    grid::grid.newpage()\n    grid::grid.draw(p)\n    ```\n\n## `removeGrid` - Remove grid lines from ggplot2\n\nThis is just a convenience function to save a bit of typing and\nmemorization. Minor grid lines are always removed, and the major x or y\ngrid lines can be removed as well (default is to remove both).\n\n`removeGridX` is a shortcut for `removeGrid(x = TRUE, y = FALSE)`, and\n`removeGridY` is similarly a shortcut for…\n\u003cleave as exercise for reader\u003e.\n\n    df2 \u003c- data.frame(x = 1:50, y = 1:50)\n    p2 \u003c- ggplot2::ggplot(df2, ggplot2::aes(x, y)) + ggplot2::geom_point()\n    p2 + removeGrid()\n\n\u003cimg src=\"inst/vignette_files/ggExtra_files/figure-markdown_strict/removeGrid-1.png\" style=\"display: block; margin: auto;\" /\u003e\n\nFor more information, see `?ggExtra::removeGrid`.\n\n## `rotateTextX` - Rotate x axis labels\n\nOften times it is useful to rotate the x axis labels to be vertical if\nthere are too many labels and they overlap. This function accomplishes\nthat and ensures the labels are horizontally centered relative to the\ntick line.\n\n    df3 \u003c- data.frame(x = paste(\"Letter\", LETTERS, sep = \"_\"),\n                      y = seq_along(LETTERS))\n    p3 \u003c- ggplot2::ggplot(df3, ggplot2::aes(x, y)) + ggplot2::geom_point()\n    p3 + rotateTextX()\n\n\u003cimg src=\"inst/vignette_files/ggExtra_files/figure-markdown_strict/rotateTextX-1.png\" style=\"display: block; margin: auto;\" /\u003e\n\nFor more information, see `?ggExtra::rotateTextX`.\n\n## `plotCount` - Plot count data with ggplot2\n\nThis is a convenience function to quickly plot a bar plot of count\n(frequency) data. The input must be either a frequency table (obtained\nwith `base::table`) or a data.frame with 2 columns where the first\ncolumn contains the values and the second column contains the counts.\n\nAn example using a table:\n\n    plotCount(table(infert$education))\n\n\u003cimg src=\"inst/vignette_files/ggExtra_files/figure-markdown_strict/plotCount-table-1.png\" style=\"display: block; margin: auto;\" /\u003e\n\nAn example using a data.frame:\n\n    df4 \u003c- data.frame(\"vehicle\" = c(\"bicycle\", \"car\", \"unicycle\", \"Boeing747\"),\n                      \"NumWheels\" = c(2, 4, 1, 16))\n    plotCount(df4) + removeGridX()\n\n\u003cimg src=\"inst/vignette_files/ggExtra_files/figure-markdown_strict/plotCount-df-1.png\" style=\"display: block; margin: auto;\" /\u003e\n\nFor more information, see `?ggExtra::plotCount`.\n","funding_links":["https://github.com/sponsors/daattali"],"categories":["Plot layers","R","パッケージ紹介","Table of Contents","ggplot"],"sub_categories":["ggExtra","Visualization","Miscellaneous"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdaattali%2FggExtra","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdaattali%2FggExtra","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdaattali%2FggExtra/lists"}