Written and maintained by CASRAI Editorial Board
Last updated
ggplot2 is the R package for building graphics from a declared specification: you supply a data frame, map its variables to visual properties (aesthetics), and add layers. Install it with install.packages("ggplot2") or as part of the tidyverse, then use ggsave() to export the figure at an explicit size and resolution. People searching “ggplots in R” almost always mean plots made with ggplot2; this guide walks the whole sequence from a first plot to a saved, journal-ready file.
Looking for one chart type? Go straight to CASRAI’s guides on scatterplots in R, histograms in R, box plots in R or bar plots in R. This page covers what they share.
What ggplot2 is used for in research
The ggplot2 project site describes it as “a system for declaratively creating graphics, based on The Grammar of Graphics”: you provide the data, tell ggplot2 how to map variables to aesthetics, and it handles the details of drawing. In research work this means one plot specification can be kept in a script alongside the analysis, re-run when data change, and re-exported at the size a journal or slide deck needs.
The name is often typed “ggplot” or “ggplots”. The package is ggplot2 and its main function is ggplot().
Install or access
ggplot2 is distributed through CRAN, the Comprehensive R Archive Network. The CRAN package page lists version 4.0.3, published 2026-04-22, under an MIT license with a file LICENSE, and requires R 4.1 or later. The ggplot2 site notes that 4.0.0 was a major release in September 2025.
- Install R 4.1 or later.
- Install the package. The ggplot2 site gives two routes:
install.packages("tidyverse")(described as the easiest; it includes ggplot2) orinstall.packages("ggplot2"). - To try the development version, the site gives
install.packages("pak")followed bypak::pak("tidyverse/ggplot2"). - Load it in each session with
library(ggplot2).
Step-by-step: from first plot to saved figure
1. Make a first plot
The example on the ggplot2 home page uses the built-in mpg data set (234 cars, 7 vehicle classes):
library(ggplot2)
ggplot(mpg, aes(displ, hwy, colour = class)) +
geom_point()
Read it in three parts. ggplot(mpg, ...) names the data. aes(displ, hwy, colour = class) maps engine displacement to the x axis, highway mileage to the y axis and vehicle class to point colour. geom_point() is the layer that draws points. Layers are joined with +.
2. Understand aesthetics and layers
The R for Data Science chapter on data visualization (2nd edition, chapter 1) teaches the same structure in order: start with ggplot(data = ...), add aesthetic mappings, then add geom layers. It also distinguishes global mappings (set in ggplot(), shared by all layers) from local mappings (set inside one geom), and notes that ggplot2 calls usually omit the data = and mapping = names, as the example above does.
3. Split a plot into panels with facets
The facet_wrap() reference gives the signature facet_wrap(facets, nrow = NULL, ncol = NULL, scales = "fixed", ...). Facets are specified with vars(), and scales accepts “fixed”, “free”, “free_x” or “free_y”. Its example builds a base plot and then adds the facet:
p <- ggplot(mpg, aes(displ, hwy)) + geom_point()
p + facet_wrap(vars(class))
The reference page says facet_wrap() generally makes better use of screen space than facet_grid().
4. Choose a theme
Themes change the non-data appearance. The ggplot2 theme reference lists these complete themes:
| Theme | What the reference says |
|---|---|
theme_gray() / theme_grey() |
The default: grey background, white gridlines |
theme_bw() |
Dark-on-light; often better for projected presentations; default base_size 11 |
theme_linedraw() |
Thin lines; the reference warns that journals may reject it |
theme_light() |
A light theme in the same family |
theme_dark() |
A dark theme in the same family |
theme_minimal() |
No background annotations |
theme_classic() |
Axis lines, no gridlines |
theme_void() |
Completely empty |
Add one to a plot with +, for example p + theme_bw(). Label axes and legends with labs(), as in the reference example labs(x = ..., y = ..., colour = "Gears").
5. Save the figure with ggsave()
The ggsave() reference gives the signature:
ggsave(filename, plot = get_last_plot(), device = NULL, path = NULL,
scale = 1, width = NA, height = NA,
units = c("in", "cm", "mm", "px"), dpi = 300,
limitsize = TRUE, bg = NULL, create.dir = FALSE, ...)
- If
deviceisNULLit is guessed from the file extension, so"figure.pdf"writes a PDF. - If
widthandheightare not set, they default to the size of the current graphics device. Set them explicitly withunitsso the output does not depend on your screen. dpidefaults to 300 and also accepts “retina” (320), “print” (300) and “screen” (72).limitsize = TRUEblocks images larger than 50 by 50 inches.
The reference example is ggsave("mtcars.pdf", width = 20, height = 20, units = "cm").
Output and how to read it
In the home-page example, each point is one of the 234 cars: horizontal position shows engine displacement, vertical position shows highway mileage, and colour shows vehicle class. Every aesthetic you map inside aes() is a variable in the data, so you can read the plot by asking which column each visual property stands for. For a figure that goes into a manuscript, record the ggplot2 version (CRAN lists 4.0.3 as of 2026-10-29) and the R version, and cite the software; see how to cite software, code and R packages.
Troubleshooting
| Symptom | What the documentation says |
|---|---|
| Saved file is a different size than expected | ggsave() takes width and height from the current device unless you set them; set width, height and units. |
| Saved file has the wrong format | The format is guessed from the extension when device is NULL; change the extension or set device. |
ggsave() refuses a very large image |
limitsize = TRUE blocks images over 50 by 50 inches; use a smaller size. |
| Facet panels all share the same axes | facet_wrap() defaults to scales = "fixed"; use “free”, “free_x” or “free_y” to let them vary. |
| Installation fails | The CRAN page lists R 4.1 or later as a requirement for the current version. |
For error messages, the common-problems section of R for Data Science chapter 1 is the next stop.
Free alternatives
ggplot2 is free (MIT license), and several other free R packages draw graphics. The table lists three, with details from the R documentation index and CRAN package pages.
| Option | What the documentation says | License (CRAN) |
|---|---|---|
Base R graphics (graphics package) |
The R Graphics Package; its index lists functions such as plot, hist, barplot and boxplot |
Documented in the R manual index |
| lattice (version 0.23-1) | “A powerful and elegant high-level data visualization system inspired by Trellis graphics, with an emphasis on multivariate data”; requires R 4.0.0 or later | GPL-2 | GPL-3 |
| plotly (version 4.12.1) | Creates interactive web graphics from ggplot2 graphs and/or a custom interface to the plotly.js JavaScript library | MIT + file LICENSE |
For interactive charts, see CASRAI’s guide to Plotly in R. For single chart types, use the chart-specific guides linked at the top of this page.
Frequently asked questions
Is “ggplot” the same as ggplot2?
In everyday use, yes. The package is named ggplot2 and its main function is ggplot(). You load it with library(ggplot2).
Do I need the tidyverse to use ggplot2?
No. The ggplot2 site lists install.packages("ggplot2") as an alternative to installing the whole tidyverse.
Which R version does ggplot2 need?
The CRAN page for version 4.0.3 lists R 4.1 or later.
Which theme should I use for a journal figure?
The theme reference notes that theme_bw() is often better for projected presentations and that journals may reject theme_linedraw(). Check your target journal’s figure instructions for its own requirements.
Sources
Checked 2026-10-29.
- ggplot2 home page: https://ggplot2.tidyverse.org/
- CRAN package page for ggplot2: https://cran.r-project.org/web/packages/ggplot2/index.html
- ggplot2 reference: ggsave(): https://ggplot2.tidyverse.org/reference/ggsave.html
- ggplot2 reference: facet_wrap(): https://ggplot2.tidyverse.org/reference/facet_wrap.html
- ggplot2 reference: complete themes: https://ggplot2.tidyverse.org/reference/ggtheme.html
- Wickham, Cetinkaya-Rundel and Grolemund, R for Data Science (2e), Data visualization: https://r4ds.hadley.nz/data-visualize.html
- CRAN package page for lattice: https://cran.r-project.org/web/packages/lattice/index.html
- CRAN package page for plotly: https://cran.r-project.org/web/packages/plotly/index.html
- R graphics package index: https://stat.ethz.ch/R-manual/R-devel/library/graphics/html/00Index.html








