Description Usage Arguments Value See Also Examples
View source: R/plot_timeline.R
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:
plot_timelineA()
defaults to recoding line_name
to title case, and
allows custom recoding via a data frame passed to the argument specs
.
plot_timelineB()
plots line_name
"as is". You may recode line_name
before passing the data
with, for example, dplyr::recode()
.
plot_timelineC()
defaults to plotting a title case version of line_name
values, and allows none or other recoding via the argument recode
(see
Arguments).
1 2 3 4 5 | plot_timelineA(data, specs = timeline_specs(data))
plot_timelineB(data)
plot_timelineC(data, recode = TRUE)
|
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:
|
An object of class "ggplot".
timeline_specs
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)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.