Skip to contents

Creates a ggplot2 streamgraph based on ggstream::geom_stream() using the specified period as time resolution.

Usage

ggplot_streamgraph(
  data,
  by,
  period = c("week", "month", "quarter", "year", "decade", "century"),
  stacking = c("mirror", "ridge", "proportional"),
  bandwidth = 0.75,
  y_lim = NULL,
  color_palette = viridisLite::turbo,
  prune_legend = TRUE
)

Arguments

data

RDB referendum data as returned by rfrnds(). A data frame that at minimum contains the column specified in period or the column date (to compute the period column), plus the one(s) specified via by (if any).

by

data column to group by before counting number of referendums.

period

Type of period to count referendums by. One of "week", "month", "quarter", "year", "decade" or "century".

stacking

Stacking type. One of

  • "mirror" to stack absolute values symmetrically around the zero line on the x-axis.

  • "ridge" to stack absolute values from the zero line on the x-axis upwards.

  • "proportional" to stack relative values that add up to 100 %.

bandwidth

Kernel density estimation bandwidth. A numeric scalar.

y_lim

Optional Y axis range limit. Only relevant if stacking is one of "mirror" or "ridge". The limit applies to the upper side if stacking = "ridge" and to both sides if stacking = "mirror". A numeric scalar equal or greater than zero.

color_palette

Color palette function that when called with a single integer argument returns that many color codes.

prune_legend

Whether or not to drop by factor levels which don't occur in data from the legend. Only has an effect if by is of type factor.

Value

A ggplot2 object.

See also

Examples

data_rdb <- rdb::rfrnds(quiet = TRUE)

rdb::ggplot_streamgraph(data = data_rdb,
                        by = topics_tier_1,
                        period = "year")


# you can specify a different color palette
rdb::ggplot_streamgraph(data = data_rdb,
                        by = topics_tier_1,
                        period = "year",
                        color_palette = viridisLite::viridis)


# by default, only factor levels which occur in data are included in the legend
data_rdb |>
  dplyr::filter(country_code == "AT") |>
  rdb::ggplot_streamgraph(by = topics_tier_1,
                          period = "decade")


# but you can include *all* factor levels in the legend if you want to
data_rdb |>
  dplyr::filter(country_code == "AT") |>
  rdb::ggplot_streamgraph(by = topics_tier_1,
                          period = "decade",
                          prune_legend = FALSE)