Differences between revisions 2 and 5 (spanning 3 versions)
Revision 2 as of 2023-06-15 13:44:13
Size: 2394
Comment:
Revision 5 as of 2026-09-03 14:32:12
Size: 3745
Comment: Update content
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
= R Ggplot2 = = R ggplot2 =
Line 3: Line 3:
The '''`ggplot2`''' package is a graphing library. '''ggplot2''' is a package for plotting and other data visualization tasks.
Line 13: Line 13:
`ggplot2` is commonly used through the [[R/TidyVerse|tidyverse]] package. `ggplot2` is a part of the [[R/Tidyverse|tidyverse]] collection. 
Line 21: Line 21:
`ggplot()` returns a `ggplot` object. Components are inserted with the addition (`+`) operator. The core `ggplot` function returns a `ggplot` object. This object does nothing on its own; geometry functions must be applied by 'adding' to the object.

As a basic example:
Line 25: Line 27:
ggplot(data = DATA, mapping = aes(MAPPINGS)) +  GEOM_FUNC()
ggplot(data = df, mapping = aes(x = foo, y = bar)) + geom_point() + geom_smooth()
Line 27: Line 30:

Geometry function include:

 * `geom_point()`
 * `geom_smooth()`
   * The `method` option determines how the line is calculated.
   * If unset and if there are fewer than 1,000 cases, [[Statistics/LocallyEstimatedScatterplotSmoothing|LOESS]] is used. If unset and if there are 1,000 cases or more, [[Statistics/GeneralizedAdditiveModel|GAM]] is used.
   * Other available options include `"lm"` and `"glm"`.
 * `geom_abline(intercept=A, slope=B)`
 * `geom_hline(yintercept=Y)
 * `geom_vline(xintercept=X)
   * Can pass a vector to add multiple lines.



=== Aesthetic Mappings ===

The `aes` option creates an aesthetic `mapping` object. The main point of this function call is to standardize property names. For example, `color`, `colour`, `col`, and `fg` are all coerced to the same property name. The `mapping` option can be specified for either the `ggplot` function for global properties, or for a geometry function for local properties. Consider:

{{{
ggplot(data = df, mapping = aes(x = foo, y = bar)) + geom_point(aes(color="blue")) + geom_smooth(aes(color="red"))
}}}

The `aes` function can also evaluate functions of vectors, like:

{{{
aes(x = foo/bar, y = baz^2)
}}}

Note however that the function eagerly evaluates properties, so tricks have to be used if attempting to abstract the function call, as by a macro.
Line 45: Line 78:
A set of default themes are provided by the `ggplot2` package. theya re accessible from these functions: A set of default themes are built into the `ggplot2` package and accessible from functions:
Line 58: Line 91:
Otherwise, construct a theme object using `theme()`. Alternatively, construct a new theme using the `theme` function.
Line 61: Line 94:
library(tidyverse)
mytheme <- theme(OPTIONS)
ggplot(data = DATA, mapping = aes(MAPPINGS)) + GEOM_FUNC() + mytheme
ggplot(data = df, mapping = aes(x = foo, y = bar)) + mytheme
Line 69: Line 100:
library(tidyverse)
ggplot(data = DATA, mapping = aes(MAPPINGS)) + GEOM_FUNC() + theme_minimal() + theme(OPTIONS)
ggplot(data = df, mapping = aes(x = foo, y = bar)) + theme_minimal() + mytheme
Line 80: Line 110:
theme(text = element_text(family = "DejaVu")) mytheme <- theme(text = element_text(family = "DejaVu"))
Line 86: Line 116:
theme(plot.title = element_text(size = 20)) mytheme <- theme(plot.title = element_text(size = 20))
Line 94: Line 124:
theme(axis.text.x = element_text(angle = 90, hjust = 0.5, vjust = 0.5)) mytheme <- theme(axis.text.x = element_text(angle = 90, hjust = 0.5, vjust = 0.5))
Line 100: Line 130:
theme(axis.text.y = element_text(colour = "grey20")) mytheme <- theme(axis.text.y = element_text(colour = "grey20"))
Line 106: Line 136:
theme(strip.text = element_text(face = "italic")) mytheme <- theme(strip.text = element_text(face = "italic"))
Line 115: Line 145:
[[https://ggplot2.tidyverse.org/reference/theme.html|ggplot2 theme reference]]

[[https://ggplot2.tidyverse.org/reference/ggtheme.html|ggplot2 default themes reference]]
[[https://ggplot2.tidyverse.org/reference/index.html|ggplot2 package reference]]

R ggplot2

ggplot2 is a package for plotting and other data visualization tasks.


Installation

ggplot2 is a part of the tidyverse collection.


Usage

The core ggplot function returns a ggplot object. This object does nothing on its own; geometry functions must be applied by 'adding' to the object.

As a basic example:

library(tidyverse)

ggplot(data = df, mapping = aes(x = foo, y = bar)) + geom_point() + geom_smooth()

Geometry function include:

  • geom_point()

  • geom_smooth()

    • The method option determines how the line is calculated.

    • If unset and if there are fewer than 1,000 cases, LOESS is used. If unset and if there are 1,000 cases or more, GAM is used.

    • Other available options include "lm" and "glm".

  • geom_abline(intercept=A, slope=B)

  • `geom_hline(yintercept=Y)
  • `geom_vline(xintercept=X)
    • Can pass a vector to add multiple lines.

Aesthetic Mappings

The aes option creates an aesthetic mapping object. The main point of this function call is to standardize property names. For example, color, colour, col, and fg are all coerced to the same property name. The mapping option can be specified for either the ggplot function for global properties, or for a geometry function for local properties. Consider:

ggplot(data = df, mapping = aes(x = foo, y = bar)) + geom_point(aes(color="blue")) + geom_smooth(aes(color="red"))

The aes function can also evaluate functions of vectors, like:

aes(x = foo/bar, y = baz^2)

Note however that the function eagerly evaluates properties, so tricks have to be used if attempting to abstract the function call, as by a macro.


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 built into the ggplot2 package and accessible from functions:

  • theme_grey()

  • theme_gray()

  • theme_bw()

  • theme_linedraw()

  • theme_light()

  • theme_dark()

  • theme_minimal()

  • theme_classic()

  • theme_void()

  • theme_test()

Alternatively, construct a new theme using the theme function.

ggplot(data = df, mapping = aes(x = foo, y = bar)) + mytheme

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

ggplot(data = df, mapping = aes(x = foo, y = bar)) + theme_minimal() + mytheme

Options

To set the font for a graph's text:

mytheme <- theme(text = element_text(family = "DejaVu"))

To enlarge the title:

mytheme <- 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:

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

To color the y-axis labels:

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

To italicize the group titles in a faceted plot:

mytheme <- theme(strip.text = element_text(face = "italic"))


See also

ggplot2 package reference


CategoryRicottone

R/Ggplot2 (last edited 2026-09-03 14:32:12 by DominicRicottone)