Differences between revisions 1 and 4 (spanning 3 versions)
Revision 1 as of 2023-06-15 13:40:57
Size: 2382
Comment:
Revision 4 as of 2026-05-05 15:51:37
Size: 2382
Comment: Capitalization
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
= R Ggplot2 = = R ggplot2 =
Line 3: Line 3:
'''`ggplot2`''' is a graphing library. The '''`ggplot2`''' package is a graphing library.
Line 13: Line 13:
`ggplot2` is commonly used through the [[R/TidyVerse|tidyverse]] package. `ggplot2` is commonly used through [[R/Tidyverse|Tidyverse]].

R ggplot2

The ggplot2 package is a graphing library.


Installation

ggplot2 is commonly used through Tidyverse.


Usage

ggplot() returns a ggplot object. Components are inserted with the addition (+) operator.

library(tidyverse)
ggplot(data = DATA, mapping = aes(MAPPINGS)) +  GEOM_FUNC()


Theming

Themes are used to customize the appearance of a graph. They are applied to a ggplot object with the addition (+) operator.

library(tidyverse)
ggplot(data = DATA, mapping = aes(MAPPINGS)) +  GEOM_FUNC() + THEME_OBJ

Theme Objects

A set of default themes are provided by the ggplot2 package. theya re accessible from these functions:

  • theme_grey()

  • theme_gray()

  • theme_bw()

  • theme_linedraw()

  • theme_light()

  • theme_dark()

  • theme_minimal()

  • theme_classic()

  • theme_void()

  • theme_test()

Otherwise, construct a theme object using theme().

library(tidyverse)
mytheme <- theme(OPTIONS)
ggplot(data = DATA, mapping = aes(MAPPINGS)) +  GEOM_FUNC() + mytheme

A common strategy is to use one of the above default themes as a starting point and then to apply customizations.

library(tidyverse)
ggplot(data = DATA, mapping = aes(MAPPINGS)) +  GEOM_FUNC() + theme_minimal() + theme(OPTIONS)

Options

To set the font for a graph's text:

theme(text = element_text(family = "DejaVu"))

To enlarge the title:

theme(plot.title = element_text(size = 20))

Note that targetting title instead of plot.title would affect the size of all title-like objects (e.g. axes labels, legend title, and so on).

To rotate the x-axis labels 90 degrees:

theme(axis.text.x = element_text(angle = 90, hjust = 0.5, vjust = 0.5))

To color the y-axis labels:

theme(axis.text.y = element_text(colour = "grey20"))

To italicize the group titles in a faceted plot:

theme(strip.text = element_text(face = "italic"))


See also

ggplot2 theme reference

ggplot2 default themes reference


CategoryRicottone

R/Ggplot2 (last edited 2026-05-05 15:51:37 by DominicRicottone)