3  geom_a

3.1 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

3.2 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

3.3 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 > 2015) |>
  group_by(Region, Year, Income) |>
  summarise(n = n()) |>
  drop_na()
freq.table
# A tibble: 153 × 4
# Groups:   Region, Year [49]
   Region               Year Income     n
   <fct>               <dbl> <fct>  <int>
 1 East Asia & Pacific  2016 LM        13
 2 East Asia & Pacific  2016 UM        10
 3 East Asia & Pacific  2016 H         14
 4 East Asia & Pacific  2017 LM        13
 5 East Asia & Pacific  2017 UM        10
 6 East Asia & Pacific  2017 H         14
 7 East Asia & Pacific  2018 LM        13
 8 East Asia & Pacific  2018 UM        10
 9 East Asia & Pacific  2018 H         14
10 East Asia & Pacific  2019 LM        12
# ℹ 143 more rows
a1 <- freq.table |>
  ggplot(aes(y = n, axis1 = Region, axis2 = Year)) +
  geom_alluvium(aes(fill = Income), width = 1 / 12) +
  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))) +
  labs(title = "a2: geom_alluvium, \n geom_stratum and geom_label")
a1 / a2

3.4 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)

3.5 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)