variable_timesteps: Bar plots with variable time steps

add_timesteps_columnsR Documentation

Bar plots with variable time steps

Description

Utility functions for plotting stacked bars with variable widths for displaying time-series data with variable time steps (like REMIND data).

Usage

add_timesteps_columns(
  data,
  timesteps,
  periods = "period",
  gaps = 0,
  interval_shift = c(-0.5, 0.5),
  timesteps_period = "period",
  timesteps_interval = "year"
)

add_remind_timesteps_columns(data, periods = "period", gaps = 0)

ggplot_bar_vts(
  data,
  timesteps,
  mapping = aes(x = !!sym("period"), y = !!sym("value"), fill = !!sym("variable")),
  gaps = 0.1,
  position_fill = FALSE,
  interval_shift = c(-0.5, 0.5),
  timesteps_period = "period",
  timesteps_interval = "year"
)

ggplot_bar_remind_vts(
  data,
  mapping = aes(x = !!sym("period"), y = !!sym("value"), fill = !!sym("variable")),
  gaps = 0.1,
  position_fill = FALSE
)

Arguments

data

A data frame.

timesteps

A data frame like remind_timesteps with columns period, year, and weight, where weight determines which share of year belongs to period.

periods

The column holding the period information in data (either a string or an object). Defaults to 'period'.

gaps

Gaps between bars as a fraction of the smallest bar width. Defaults to 0.1 (e.g. 0.1 * 5 years = 0.5 years).

interval_shift

numeric of length 2. Shifts added to the interval fix point to obtain the beginning and end of time interval. If the interval for period 1 should be ⁠[0.5, 1.5]⁠, interval_shift should be set to c(-0.5, 0.5) (default). If the interval for period 1 should be ⁠[0, 1]⁠, interval_shift should be set to c(-1, 0).

timesteps_period

character string giving the column name of the period in the timesteps data frame. Defaults to 'period'.

timesteps_interval

character string giving the column name of the time interval in the timesteps data frame. Defaults to 'year'.

mapping

aes() mapping with aesthetics x, y, and optionally fill.

position_fill

If TRUE, stacks bars and standardises each stack to have constant height.

Details

add_timesteps_columns() calculates the x-axis position and width of bars based on the information in timesteps and joins it to data. add_remind_timesteps_columns() uses the remind_timesteps data frame. ggplot_bar_vts() produces a bar plot with bars positioned according to timesteps. ggplot_bar_remind_vts() uses the remind_timesteps data frame.

Value

add_timesteps_columns() and add_remind_timesteps_columns() return a data frame. ggplot_bar_vts() and ggplot_bar_remind_vts() return a ggplot()-like object.

Author(s)

Michaja Pehl

Examples

require(tidyverse)

# some example data
(data <- quitte_example_data %>%
    filter(first(scenario) == scenario,
           last(region) == region,
           first(variable) == variable))

# adding individual timesteps
add_timesteps_columns(data, remind_timesteps)

# adding remind timesteps with gaps
add_remind_timesteps_columns(data, gaps = 0.1)

# plotting individual timesteps without gaps
ggplot_bar_vts(data, remind_timesteps, gaps = 0)

# plotting remind timegaps, using further ggplot2 functions
ggplot_bar_remind_vts(
    data = quitte_example_data %>%
        filter(scenario %in% levels(quitte_example_data$scenario)[1:3],
               last(region) == region,
               grepl('PE\\|', variable),
               2100 >= period)) +
    scale_fill_manual(
        values = mip::plotstyle(grep('^PE\\|',
                                     levels(quitte_example_data$variable),
                                     value = TRUE))) +
    facet_wrap(~ scenario)

# another data set with a different time column
data2 <- data.frame(variable = c('Wind', 'Solar', 'Wind', 'Solar'),
    tau = c(1,1,2,2),
    value = 1:4)

# some timesteps dataframe with hourly data
timesteps <- data.frame(tau = c(rep(1,2),rep(2,4)),
                            hour = 1:6,
                            weight = 1)

# plotting with different timesteps than periods and years
ggplot_bar_vts(data2, timesteps,
              mapping = aes(tau, value, group = variable, fill = variable),
              timesteps_period = 'tau',
              timesteps_interval = 'hour',
              interval_shift = c(-1,0))

pik-piam/quitte documentation built on April 26, 2024, 12:58 a.m.