plot_timelineA: Creates a time line plot

Description Usage Arguments Value See Also Examples

View source: R/plot_timeline.R

Description

We are exploring different interfaces before release. We are keen to hear feedback from beta-testers like you. Please try these alternative interfaces and let us know which one you prefer. The main difference between them is if and how they allow recoding the values of line_name, which become the labels of the plot legend:

Usage

1
2
3
4
5

Arguments

data

Pre-processed data for the chart, with columns: year, value, line_name.

specs

Dataframe containing order of lines, their labels and colour names from the r2dii_colours palette.

recode

One of the following:

  • A function/lambda to apply to data$line_name.

  • A named vector to recode data$line_name.

  • A logical vector of length 1. TRUE recodes data$line_name to title case. FALSE does no recoding and plots data$line_name as is.

Value

An object of class "ggplot".

See Also

timeline_specs

dplyr::recode().

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
library(ggplot2)
library(dplyr)

data <- sda_target %>%
  filter(sector == "cement", between(year, 2020, 2050)) %>%
  prepare_for_timelineB(extrapolate = TRUE)

# `plot_timelineA()` -------------------------------------------------------

data <- prepare_for_timelineA(sda_target, sector_filter = "cement")
plot_timelineA(data)

# Customize as usual with ggplot2
plot_timelineA(data) +
  scale_colour_manual(values = c("red", "blue", "green", "black")) +
  labs(title = "Timeline plot")

# Customize `line_name` via a data frame passed to `specs`
# styler: off
custom <- tribble(
                ~line_name,                  ~label, ~colour_hex,
               "projected",                 "Proj.",   "#4a5e54",
       "corporate_economy",         "Corp. Economy",   "#a63d57",
             "target_demo",         "Target (demo)",   "#78c4d6",
  "adjusted_scenario_demo",  "Adj. Scenario (demo)",   "#f2e06e",
)
# styler: on

plot_timelineA(data, specs = custom)

# `plot_timelineB()` ------------------------------------------------------

data <- prepare_for_timelineA(sda_target, sector_filter = "aviation")
plot_timelineB(data)

# Recode `line_name` with `dplyr::recode()`
data %>%
  mutate(line_name = recode(line_name,
    "corporate_economy" = "Corp. economy",
    "projected" = "Proj.",
    "target_demo" = "Target (demo)",
    "adjusted_scenario_demo" = "Adj. Scenario (demo)",
  )) %>%
  plot_timelineB()

# `plot_timelineC()` ------------------------------------------------------

data <- prepare_for_timelineA(sda_target, sector_filter = "aviation")
unique(data$line_name)
# Recode to title case
plot_timelineC(data, recode = TRUE)

# Don't recode
plot_timelineC(data, recode = FALSE)

# Recode to title case
unique(data$line_name)
plot_timelineC(data)

# Recode using a function
plot_timelineC(data, recode = toupper)

# Recode using a formula giving a lambda function
plot_timelineC(data, recode = ~ toupper(gsub("_", " ", .x)))

# Recode via a named vector
legend <- c(
  "projected" = "Proj.",
  "corporate_economy" = "Corp. Economy",
  "target_demo" = "Target (demo)",
  "adjusted_scenario_demo" = "Adj. Scenario (demo)"
)
plot_timelineC(data, recode = legend)

2DegreesInvesting/r2dii.plot.static documentation built on Dec. 17, 2021, 6:37 a.m.