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(worldbankdata, aes(y = Cooking, x = Electricity)) +
  geom_abline(intercept = 0, slope = 1) +
  labs(title = "a1: geom_abline only") +
  theme(aspect.ratio = 1)
a2 <- ggplot(worldbankdata, aes(y = Cooking, x = Electricity)) +
  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

x, y, ymin, ymax,

optional aesthetics

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

See also

geom_stratum, geom_flow, geom_lode

Example

library(ggalluvial)
freq.table <- worldbankdata |>
  select(Country, Region, Year, Income) |>
  filter(Year > 2019) |>
  filter(Region %in% c("East Asia & Pacific", "North America")) |>
  group_by(Region, Year, Income) |>
  summarise(n = n()) |>
  drop_na()
freq.table
# A tibble: 12 × 4
# Groups:   Region, Year [6]
   Region               Year Income     n
   <fct>               <dbl> <fct>  <int>
 1 East Asia & Pacific  2020 LM        14
 2 East Asia & Pacific  2020 UM         8
 3 East Asia & Pacific  2020 H         15
 4 East Asia & Pacific  2021 LM        14
 5 East Asia & Pacific  2021 UM         9
 6 East Asia & Pacific  2021 H         14
 7 East Asia & Pacific  2022 LM        13
 8 East Asia & Pacific  2022 UM         9
 9 East Asia & Pacific  2022 H         15
10 North America        2020 H          3
11 North America        2021 H          3
12 North America        2022 H          3
a1 <- freq.table |>
  ggplot(aes(y = n, axis1 = Region, axis2 = Year)) +
  geom_alluvium(aes(fill = Income), width = 1 / 12) +
  scale_fill_brewer(palette = "Dark2") +
  labs(title = "a1: geom_alluvium only")

a2 <- freq.table |>
  ggplot(aes(y = n, axis1 = Region, axis2 = Year)) +
  geom_alluvium(aes(fill = Income), 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

1.3 geom_arc

Package

ggforce(Pedersen 2022)

Description

Draw a circle or a segment of a circle.

Understandable aesthetic

required aesthetics

x0 - starting coordinate of x-axis , y0 - starting coordinate of x-axis, r - radius, start, end

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

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 - starting coordinate of x-axis , y0 - starting coordinate of x-axis, r - radius, start, end

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

df <- data.frame(
  state = c(
    "A", "B", "C",
    "D", "E"
  ),
  focus = c(0.2, 0, 0, 0, 0),
  start = c(0, 1, 2, 3, 4),
  end = c(1, 2, 3, 4, 2 * pi),
  amount = c(4, 3, 1, 2, 5),
  stringsAsFactors = FALSE
)
ggplot(df) +
  geom_arc_bar(aes(
    x0 = 0, y0 = 0, r0 = 0, r = 1, amount = amount,
    fill = state, explode = focus
  ), stat = "pie") +
  scale_fill_brewer(palette = "Dark2") +
  theme(aspect.ratio = 1)

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)
Loading required package: RJDemetra

Attaching package: 'RJDemetra'
The following objects are masked from 'package:dplyr':

    compute, count

Attaching package: 'ggdemetra'
The following object is masked from 'package:RJDemetra':

    ipi_c_eu
The following object is masked from 'package:base':

    raw
 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)
Warning: Computation failed in `stat_arima()`.
Caused by error in `dataframe2ts()`:
! Error with the frequency: it must be equal to 12, 6, 4 or 2

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

1.10 geom_autohistogram

1.11 geom_autopoint

1.12 geom_axis_hive

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.