Skip to main content
v2026.11,858 entries · CC-BY 4.0

ggplots in R: Build, Style and Save Plots with ggplot2

ggplots in R explained: install ggplot2, build a first plot with aesthetics and facets, pick a theme and export with ggsave() at the right size and dpi.

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.

  1. Install R 4.1 or later.
  2. Install the package. The ggplot2 site gives two routes: install.packages("tidyverse") (described as the easiest; it includes ggplot2) or install.packages("ggplot2").
  3. To try the development version, the site gives install.packages("pak") followed by pak::pak("tidyverse/ggplot2").
  4. 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 device is NULL it is guessed from the file extension, so "figure.pdf" writes a PDF.
  • If width and height are not set, they default to the size of the current graphics device. Set them explicitly with units so the output does not depend on your screen.
  • dpi defaults to 300 and also accepts “retina” (320), “print” (300) and “screen” (72).
  • limitsize = TRUE blocks 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.

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about ggplots in R: Build, Style and Save Plots with ggplot2

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →

Ask CASRAI · Regulatory Radar

Research-admin question? Get an answer that links its sources.

An AI assistant specialized in research administration. Every answer links its sources to check before you act. 2 questions free, no account. $29/month after.

  • Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.
  • Every answer numbers its sources and links each one, so you can check the source yourself.