1  A

1.1 geom_abline

Package

ggplot2 (Wickham 2016)

Description

Description Draw a straight line (\(Y=mX+c\)) for a given slope (\(m\)) and intercept (\(c\)).

Understandable aesthetics

Unlike most other geoms, geom_abline does not depend on the x and y variables that we map for the main plot. geom_abline has its own independent characteristics: intercept and slope.

See also

geom_point, geom_vline, geom_hline

Example

a1 <- ggplot() +
  geom_abline(intercept = 0, slope = 1) +
  labs(title = "a1: geom_abline only") +
  theme(aspect.ratio = 1)
a2 <- ggplot(happiness_gdp_income, aes(y =HappinessScore2016, x = HappinessScore2015)) +
  geom_abline(intercept = 0, slope = 1) +
  geom_point() +
  labs(title = "a2: geom_abline \n and geom_point") +
  theme(aspect.ratio = 1)
a1 | a2

1.2 geom_alluvium

Package

ggalluvial(Brunson and Read 2019; Brunson 2020)

Description

Create alluvial plot. An alluvial plot is a type of diagram that is particularly useful for visualizing categorical data and the flow or transition between different categorical variables over multiple stages or categories.

Understandable aesthetics

required aesthetics

Option 1 (See Example 1)

axis1 - First axis

axis2 - Second axis

y - Counts to determines the height/weight of the alluvium

Option 2 (alluvium time-series plot, see Example 2)

x - Time variable determining the horizontal axis

y - Determines the height/size of the flow

alluvium - According to this variable it defines the flow across the years. In our Example 2, Bangladesh and Sri Lanka.

optional aesthetics

alpha, colour, fill, linetype, size, group (group is used internally; arguments are ignored)

See also

geom_stratum, geom_flow, geom_line geom_lode

Example 1

This example shows the distribution of income level categories in 2021 and 2022 in the regions Sub-Saharan Africa and Latin America & Caribbean.

library(ggalluvial)
freq.table <- worldbankdata |>
  select(Country, Region, Year, Income) |>
  filter(Year > 2020) |>
  filter(Region %in% c("Sub-Saharan Africa", "Latin America & Caribbean")) |>
  group_by(Region, Year, Income) |>
  summarise(n = n()) |>
  drop_na()
freq.table
# A tibble: 14 × 4
# Groups:   Region, Year [4]
   Region                     Year Income     n
   <fct>                     <dbl> <fct>  <int>
 1 Latin America & Caribbean  2021 LM         5
 2 Latin America & Caribbean  2021 UM        19
 3 Latin America & Caribbean  2021 H         17
 4 Latin America & Caribbean  2022 LM         4
 5 Latin America & Caribbean  2022 UM        19
 6 Latin America & Caribbean  2022 H         18
 7 Sub-Saharan Africa         2021 L         24
 8 Sub-Saharan Africa         2021 LM        16
 9 Sub-Saharan Africa         2021 UM         6
10 Sub-Saharan Africa         2021 H          1
11 Sub-Saharan Africa         2022 L         22
12 Sub-Saharan Africa         2022 LM        18
13 Sub-Saharan Africa         2022 UM         6
14 Sub-Saharan Africa         2022 H          1
a1 <- freq.table |>
  ggplot(aes(y = n, axis1 = Income, axis2 = Region)) +
  geom_alluvium(aes(fill = as.factor(Year)), width = 1 / 12) +
  scale_fill_brewer(palette = "Dark2") +
  labs(title = "a1: geom_alluvium only")

a2 <- freq.table |>
  ggplot(aes(y = n, axis1 = Income, axis2 = Region)) +
  geom_alluvium(aes(fill = as.factor(Year)), width = 1 / 12) +
  geom_stratum(width = 1 / 12, fill = "black", color = "grey") +
  geom_label(stat = "stratum", aes(label = after_stat(stratum))) +  scale_fill_brewer(palette = "Dark2") +
  labs(title = "a2: geom_alluvium, \n geom_stratum and geom_label")
a1 / a2

Example 2

p1 <- worldbankdata |>
  filter(Country %in% c("Bangladesh", "Sri Lanka"),
         Year >= 2013, Year <= 2021) |>
  ggplot(aes(x = Year,
             y = Electricity,
             alluvium = Country,
             stratum = Country,
             fill = Country)) +
  geom_alluvium(alpha = 0.6) +
  #geom_stratum(width = 0.4, color = "black") +
  scale_x_continuous(breaks = 2013:2021) +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1)) +
  labs(title = "a1: geom_alluvium only",
       x = "Year",
       y = "Electricity",
       fill = "Country")
p2 <-  worldbankdata |>
  filter(Country %in% c("Bangladesh", "Sri Lanka"),
         Year >= 2013, Year <= 2021) |>
  ggplot(aes(x = Year,
             y = Electricity,
             alluvium = Country,
             stratum = Country,
             fill = Country)) +
  geom_alluvium(alpha = 0.6) +
  geom_stratum(width = 0.4, color = "black") +
  scale_x_continuous(breaks = 2013:2021) +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1)) +
  labs(title = "a2: geom_alluvium and geom_stratum",
       x = "Year",
       y = "Electricity",
       fill = "Country")
p1/p2

1.3 geom_arc

Package

ggforce(Pedersen 2022)

Description

Draw a circle or a segment of a circle.

Understandable aesthetic

required aesthetics

x0 - x coordinate of the center of the circle

y0 - y coordinate of the center of the circle

r - radius,

start - starting point of the circumference

end - end point of the circumference

optional aesthetics

color, linewidth, linetype, alpha, lineend

The statistical transformation to use on the data for this layer

stat_arc

See also

geom_arc2, geom_arc_bar

Example

In the example below, geom_arc() is applied twice to show how the black arc forms the full circle, while the red arc highlights a specific portion of that circle.

library(ggforce)
ggplot() +
  geom_arc(aes(x0 = 0, y0 = 0, r = 8, start = 1, end = 8)) +
  geom_arc(aes(x0 = 0, y0 = 0, r = 8, start = 1, end = 5), col = "red", size = 2) +
  theme(aspect.ratio = 1)

1.4 geom_arc_bar

Package

ggforce(Pedersen 2022)

Description

To draw pie chart and donut chart defining centre point, a radius and a start and end angle.

Understandable aesthetic

required aesthetics

x0 - x-coordinate of the center of the pie chart

y0 - y coordinate of the center of the circle

r0 - Inner radius of the arc bar. If it is 0, the chart becomes a pie chart (no hole)

r - Outer radius of the pie chart slices

amount - Determines the size (angle) of each slice. Larger counts produce larger slices

fill - Fill colour for each slice

exploid - Moves selected slices slightly outward from the center to highlight them

optional aesthetics

color, linewidth, linetype, alpha, lineend

The statistical transformation to use on the data for this layer

stat_arc

See also

geom_arc, geom_arc_bar

Example

# Count observations in each Income category
df <- worldbankdata |>
  filter(!is.na(Region)) |>
  count(Region, name = "count") |>
  mutate(
    Region = Region,
    focus1 = 0,
    focus2 = c(0.2, 0, 0, 0, 0, 0, 0)
  )
df
# A tibble: 7 × 4
  Region                     count focus1 focus2
  <fct>                      <int>  <dbl>  <dbl>
1 East Asia & Pacific         1343      0    0.2
2 Europe & Central Asia       2038      0    0  
3 Latin America & Caribbean   1512      0    0  
4 Middle East & North Africa   756      0    0  
5 North America                108      0    0  
6 South Asia                   288      0    0  
7 Sub-Saharan Africa          1693      0    0  
# Pie chart
a1 <- ggplot(df) +
  geom_arc_bar(aes(
    x0 = 0, y0 = 0,
    r0 = 0, r = 2,
    amount = count,
    fill = Region,
    explode = focus1
  ), stat = "pie") +
  scale_fill_brewer(palette = "Dark2") +
  theme(aspect.ratio = 1, legend.position = "bottom") +
  labs(fill = "Regions", title = "a1: with exploid = 0")

a2 <- ggplot(df) +
  geom_arc_bar(aes(
    x0 = 0, y0 = 0,
    r0 = 0, r = 2,
    amount = count,
    fill = Region,
    explode = focus2
  ), stat = "pie") +
  scale_fill_brewer(palette = "Dark2") +
  theme(aspect.ratio = 1, legend.position = "bottom") +
  labs(fill = "Regions", title = "a2: with exploid = 0.2 for East Asia and Pacific")
a1/a2

1.5 geom_area

Package

ggplot2 (Wickham 2016)

Description

Create an area plot. This cover the space between x-axis and line that connects the data points.

Understandable aesthetics

required aesthetics

x, y

optional aesthetics

alpha, colour, fill, group, linetype, linewidth

See also

geom_line, geom_ribbon

Example

a1 <- worldbankdata |>
  filter(Country == "Bangladesh") |>
  filter(Year >= 2013 & Year <= 2021) |>
  ggplot(aes(x = Year, y = Electricity)) +
  geom_area(alpha = 0.5) +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1)) +
  scale_x_continuous(breaks = 2013:2021) +
  labs(title = "a1: geom_area only")

a2 <- worldbankdata |>
  filter(Country == "Bangladesh") |>
  filter(Year >= 2013 & Year <= 2021) |>
  ggplot(aes(x = Year, y = Electricity)) +
  geom_area(alpha = 0.5) +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1)) +
  scale_x_continuous(breaks = 2013:2021) +
  geom_point(col = "red") +
  labs(title = "a2: geom_area \n and geom_point")
a1 | a2

1.6 geom_arima

Package

ggdemetra

Example

library(ggdemetra)
 worldbankdata |>
  filter(Country == "Bangladesh") |>
  filter(Year >= 2013 & Year <= 2021) |>
  ggplot(aes(x = Year, y = Electricity)) +
  geom_line() + 
  #geom_sa(color = "#155692", message = FALSE) +
  geom_arima(geom = "label",
               x_arima = - Inf, y_arima = -Inf, 
               vjust = -1, hjust = -0.1,
               message = FALSE)

1.7 geom_arrow

Package

metR (Campitelli 2021)

Description

Draws directional arrows (like vectors) on a plot, which can represent directions or flows — such as wind directions, movement, or gradients. The arrow from (x, y) and pointing in the direction (dx, dy) (vector components).

Understandable aesthetics

required aesthetics

x

y

optional aesthetics

arrow,type, arrow.angle, arrow.length, arrow.ends, arrow.type

See also

geom_line, geom_ribbon

1.7.1 Example

library(metR)
data <- tibble::tibble(
  x = c(10, 20, 30, 40, 50),      # Longitude or X-coordinates
  y = c(1, 2, 3, 4, 5),           # Latitude or Y-coordinates
  dx = c(1, 0, -1, 0, 1),         # Wind direction components (change in X)
  dy = c(1, -1, 0, 1, 0)          # Wind direction components (change in Y)
)

ggplot(data, aes(x = x, y = y)) +
  geom_point(color = "#1b9e77", size = 3) +  
  geom_arrow(aes(dx = dx, dy = dy), color = "#d95f02", size = 1, arrow.type = "closed") +
  labs(title = "Wind Directions at Different Locations", x = "Longitude", y = "Latitude")

1.8 geom_ash

Package

ggformula (Kaplan and Pruim 2023)

Description

Plot Average Shifted Histogram (ASH). The ASH is a nonparametric probability density estimator derived from a collection of histograms.

Understandable aesthetics

required aesthetics

x

y

optional aesthetics

color, fill, bins, alpha, size

See also

geom_histogram, geom_density

Example

library(ggformula)
p1 <- worldbankdata |>
  filter(Income == "LM") |>
  ggplot(aes(x = Electricity)) +
   geom_ash(bins = 20, color = "#d95f02") +   labs(title="Average Shifted Histogram")
p2 <- worldbankdata |>
  filter(Income == "LM") |>
  ggplot(aes(x = Electricity)) +
  geom_histogram(aes(y = stat(density)), color = "black", fill = "gray") +
   geom_ash(bins = 20, color = "#d95f02") + labs(title = "Histogram and Average Shifted Histogram")
p1|p2

1.9 geom_autodensity

Package

ggforce (Pedersen 2022)

Description

A matrix-style (commonly from GGally package) visualizations to automatically display variable distributions on diagonal panels, handling both continuous and discrete data while simplifying the mapping of panel variables.

Understandable aesthetics

required aesthetics

data

optional aesthetics

alpha, colour, group, linetype, linewidth

See also

geom_density, geom_histogram, geom_autohistogram, geom_autopoint,

Example

library(ggplot2)
library(GGally)

# Select numeric variables
df <- happiness_gdp_income |>
  select("HappinessScore2015", "HappinessScore2016", "HappinessScore2017")


ggplot(df) +
  facet_matrix(
    vars(
      HappinessScore2015, 
      HappinessScore2016, 
      HappinessScore2017
    ),
    layer.diag = 1
  ) +
  geom_autodensity()
Warning: Removed 16 rows containing non-finite outside the scale range
(`stat_autodensity()`).

1.10 geom_autohistogram

Package

ggforce (Pedersen 2022)

Description

Understandable aesthetics

required aesthetics

data

optional aesthetics

alpha, colour, group, linetype, linewidth

See also

geom_histogram, geom_auotodensity, geom_auotpoint

Example

library(dplyr)
library(ggplot2)
library(GGally)

# Prepare dataset
df <- happiness_gdp_income %>%
  select(HappinessScore2015, HappinessScore2016, HappinessScore2017,
         GDPpercapita2015) %>%
  rename(GDPpercapita = GDPpercapita2015) %>%
  # Create income category
  mutate(IncomeCategory = case_when(
    GDPpercapita < 1.2 ~ "Low",
    GDPpercapita >= 1.2 & GDPpercapita < 1.4 ~ "Medium",
    GDPpercapita >= 1.4 ~ "High"
  )) %>%
  mutate(
    IncomeCategory = factor(IncomeCategory, levels = c("Low", "Medium", "High")),
    # Create discrete happiness scores by rounding
    HappinessScore2015_discrete = factor(round(HappinessScore2015)),
    HappinessScore2016_discrete = factor(round(HappinessScore2016)),
    HappinessScore2017_discrete = factor(round(HappinessScore2017))
  )

# Plot facet matrix using discrete happiness scores
ggplot(df, aes(x = .panel_x, fill = IncomeCategory)) +
  facet_matrix(
    vars(HappinessScore2015_discrete, HappinessScore2016_discrete, HappinessScore2017_discrete),
    layer.diag = 1,
    grid.y.diag = FALSE
  ) +
  geom_autohistogram(alpha = 0.7, position = "dodge") +  # diagonal histograms
  geom_autopoint(alpha = 0.6, aes(color = IncomeCategory)) +  # off-diagonal scatterplots
  scale_fill_brewer(palette = "Set2") +
  scale_color_brewer(palette = "Set2") +
  theme_minimal() +
  labs(
    fill = "Income Category",
    color = "Income Category",
    title = "Discrete Happiness Scores by Income Category",
    x = "",
    y = ""
  )

1.11 geom_autopoint

Package

ggplot2 (Wickham 2016)

Description

Understandable aesthetics

required aesthetics

x

y

optional aesthetics

alpha, colour, group, linetype, linewidth

See also

geom_line, geom_ribbon

Example

1.12 geom_axis_hive

Package

ggplot2 (Wickham 2016)

Description

Understandable aesthetics

required aesthetics

data

optional aesthetics

alpha, colour, group, linetype, linewidth

See also

geom_line, geom_ribbon

Example

Brunson, Jason Cory. 2020. “Ggalluvial: Layered Grammar for Alluvial Plots.” Journal of Open Source Software 5 (49).
Brunson, Jason Cory, and Quentin D Read. 2019. “Package ‘Ggalluvial’.”
Campitelli, Elio. 2021. metR: Tools for Easier Analysis of Meteorological Fields. https://doi.org/10.5281/zenodo.2593516.
Kaplan, Daniel, and Randall Pruim. 2023. Ggformula: Formula Interface to the Grammar of Graphics. https://CRAN.R-project.org/package=ggformula.
Pedersen, Thomas Lin. 2022. Ggforce: Accelerating ’Ggplot2’. https://CRAN.R-project.org/package=ggforce.
Wickham, Hadley. 2016. Ggplot2: Elegant Graphics for Data Analysis. Springer-Verlag New York. https://ggplot2.tidyverse.org.