Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In plot_timeline(), order legend to match lines #158

Merged
merged 2 commits into from
May 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ Depends:
R (>= 3.4)
Imports:
dplyr,
forcats,
ggplot2,
ggpubr,
glue,
lubridate,
magrittr,
r2dii.data,
rlang,
scales,
ggpubr,
glue
scales
Suggests:
covr,
r2dii.analysis,
Expand Down
45 changes: 15 additions & 30 deletions R/plot_timeline.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,39 +47,24 @@
#' labs(title = "Emission intensity trend for Cement")
plot_timeline <- function(data, specs = timeline_specs(data)) {
check_specs(specs, data)
data <- left_join(data, specs, by = "line_name")

measured <- filter(data, !.data$extrapolated)
plot <- ggplot() +
timeline_line(measured, specs) +
ggplot() +
geom_line(
data = data, aes(
x = .data$year,
y = .data$value,
colour = forcats::fct_reorder2(.data$label, .data$year, .data$value),
linetype = .data$extrapolated
)) +
expand_limits(y = 0) +
scale_x_date(expand = expansion(mult = c(0, 0.1))) +
scale_y_continuous(expand = expansion(mult = c(0, 0.1))) +
expand_limits(y = 0) +
scale_colour_manual(
values = specs$colour_hex,
labels = specs$label
)

if (any(data$extrapolated)) {
extrapolated <- filter(data, .data$extrapolated)
plot <- plot +
timeline_line(extrapolated, specs, linetype = .data$extrapolated) +
scale_linetype_manual(values = "dashed") +
guides(linetype = FALSE)
}

plot + theme_2dii()
}

timeline_line <- function(data, specs, ...) {
geom_line(
data = data,
aes(
x = .data$year,
y = .data$value,
colour = factor(.data$line_name, levels = specs$line_name),
...
)
)
scale_colour_manual(values = unique(data$colour_hex)) +
scale_linetype_manual(
values = if (any(data$extrapolated)) c("solid", "dashed") else "solid") +
guides(linetype = FALSE) +
theme_2dii()
}

check_specs <- function(specs, data) {
Expand Down
10 changes: 2 additions & 8 deletions tests/testthat/_snaps/plot_timeline.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

$layers
$layers[[1]]
mapping: x = ~.data$year, y = ~.data$value, colour = ~factor(.data$line_name, levels = specs$line_name)
mapping: x = ~.data$year, y = ~.data$value, colour = ~forcats::fct_reorder2(.data$label, .data$year, .data$value), linetype = ~.data$extrapolated
geom_line: na.rm = FALSE, orientation = NA
stat_identity: na.rm = FALSE
position_identity
Expand All @@ -21,12 +21,6 @@
stat_identity: na.rm = FALSE
position_identity

$layers[[3]]
mapping: x = ~.data$year, y = ~.data$value, colour = ~factor(.data$line_name, levels = specs$line_name), linetype = ~.data$extrapolated
geom_line: na.rm = FALSE, orientation = NA
stat_identity: na.rm = FALSE
position_identity


$scales
<ggproto object: Class ScalesList, gg>
Expand Down Expand Up @@ -515,7 +509,7 @@
[1] "value"

$labels$colour
[1] "factor(line_name, levels = specs$line_name)"
[1] "forcats::fct_reorder2(label, year, value)"

$labels$linetype
[1] "extrapolated"
Expand Down