Skip to content

Commit

Permalink
In plot_timeline(), order legend to match lines (#158)
Browse files Browse the repository at this point in the history
* Order legend to match lines

On master, the order of the legend does not match the order of the
plot lines: https://i.imgur.com/baQlmlW.png

Now they do: https://i.imgur.com/F8CauVj.png

* Refactor to simplify body
  • Loading branch information
Mauro Lepore authored May 17, 2021
1 parent 10ece7a commit ee5060c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 38 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Depends:
R (>= 3.4)
Imports:
dplyr,
forcats,
ggplot2,
ggpubr,
glue,
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

0 comments on commit ee5060c

Please sign in to comment.