diff --git a/R/plot_trajectory.R b/R/plot_trajectory.R index c3a546b1..95ea8197 100644 --- a/R/plot_trajectory.R +++ b/R/plot_trajectory.R @@ -129,35 +129,14 @@ plot_trajectoryA <- function(data, # annotate trajectory and scenario lines last_year <- max(data$year) value_span <- max(data_scenarios$value) - min(data_scenarios$value_low) - - p_trajectory <- p_trajectory + - ggrepel::geom_text_repel( - data = data_lines %>% + data_lines_end <- data_lines %>% filter( - .data$year == last_year, - .data$metric_type != "scenario" - ), - aes( - x = .data$year, - y = .data$value, - label = .data$label - ), - direction = "y", - size = 3.5, - nudge_x = 0.15, - nudge_y = 0.01 * value_span, - hjust = 0, - segment.size = 0, - xlim = c(min(data$year), last_year + 3) - ) + .data$year == last_year + ) p_trajectory <- p_trajectory + ggrepel::geom_text_repel( - data = data_lines %>% - filter( - .data$year == last_year, - .data$metric_type == "scenario" - ), + data = data_lines_end, aes( x = .data$year, y = .data$value, @@ -168,15 +147,15 @@ plot_trajectoryA <- function(data, color = "black", size = 3.5, alpha = 1, - nudge_x = 0.6, + nudge_x = if_else(data_lines_end$metric_type == "scenario", 0.6, 0.1), nudge_y = 0.01 * value_span, hjust = 0, - segment.size = 0.3, - xlim = c(min(data$year), last_year + 5) + segment.size = if_else(data_lines_end$metric_type == "scenario", 0.4, 0), + xlim = c(min(data$year), last_year + 6) ) + scale_fill_manual( aesthetics = "segment.color", - values = scenario_specs_lines$colour + values = line_colours ) p_trajectory <- p_trajectory + diff --git a/R/prep_trajectory.R b/R/prep_trajectory.R index 08ee4a42..8ffbd8f1 100644 --- a/R/prep_trajectory.R +++ b/R/prep_trajectory.R @@ -48,7 +48,8 @@ prep_trajectory <- function(data, filter(.data$scenario_source == .env$scenario_source_filter) %>% filter(.data$year >= .env$year_start_projected) %>% filter(.data$year <= .env$end_year_filter) %>% - mutate(value = .data[[value]]) + mutate(value = .data[[value]]) %>% + select(.data$metric, .data$metric_type, .data$year, .data$technology, .data$value) if (normalize) { data_filtered <- left_join(data_filtered, @@ -59,7 +60,8 @@ prep_trajectory <- function(data, value = .data$value.x / .data$value.y, year = .data$year.x, technology = .data$technology.x - ) + ) %>% + select(.data$metric, .data$metric_type, .data$year, .data$technology, .data$value) } data_filtered diff --git a/README.Rmd b/README.Rmd index a2ef7695..c42ef174 100644 --- a/README.Rmd +++ b/README.Rmd @@ -141,7 +141,7 @@ scenario_specs <- tibble( "Current Policies Scenario") ) -plot <- plot_trajectory(data_trajectory, +plot <- plot_trajectoryA(data_trajectory, scenario_specs_good_to_bad = scenario_specs, main_line_metric = main_line_metric, additional_line_metrics = additional_line_metrics diff --git a/README.md b/README.md index edb7507a..53b35109 100644 --- a/README.md +++ b/README.md @@ -40,9 +40,11 @@ library(ggplot2, warn.conflicts = FALSE) library(r2dii.plot) ``` -- `market_share` dataset imitating the output of + - `market_share` dataset imitating the output of ‘r2dii.analysis::target\_market\_share()’. + + ``` r market_share #> # A tibble: 1,170 x 8 @@ -61,8 +63,11 @@ market_share #> # … with 1,160 more rows, and 1 more variable: technology_share ``` -- `prep_trajectory()`: prepare the output of - ‘r2dii.analysis::target\_market\_share()’ for ‘plot\_trajectory()’. + - `prep_trajectory()`: prepare the output of + ‘r2dii.analysis::target\_market\_share()’ for + ‘plot\_trajectory()’. + + ``` r data_trajectory <- prep_trajectory( @@ -76,7 +81,9 @@ data_trajectory <- prep_trajectory( ) ``` -- `plot_trajectoryA()` is an alternative to `plot_trajectoryB()`. + - `plot_trajectoryA()` is an alternative to `plot_trajectoryB()`. + + ``` r # `plot_trajectoryA()` takes more arguments @@ -107,6 +114,7 @@ plot_trajectoryA( ``` r + # more elaborate annotations, title and labels data_trajectory <- prep_trajectory( @@ -128,7 +136,7 @@ scenario_specs <- tibble( "Current Policies Scenario") ) -plot <- plot_trajectory(data_trajectory, +plot <- plot_trajectoryA(data_trajectory, scenario_specs_good_to_bad = scenario_specs, main_line_metric = main_line_metric, additional_line_metrics = additional_line_metrics @@ -148,9 +156,11 @@ plot + -- `prep_techmix()` prepare the output of + - `prep_techmix()` prepare the output of ‘r2dii.analysis::target\_market\_share()’ for ‘plot\_techmix()’. -- `plot_techmix()` create a techmix chart in a ggplot object. + - `plot_techmix()` create a techmix chart in a ggplot object. + + ``` r # Default colours, all data, added title @@ -174,6 +184,7 @@ plot + ``` r + # Custom colours, all data, no title power_colors_custom <- tibble( technology = c("coalcap", "oilcap", "gascap", "nuclearcap", "hydrocap", "renewablescap"), @@ -190,6 +201,7 @@ plot ``` r + # Default colours, selected data and labels (metric_type parameters), added title sector <- "automotive" @@ -220,9 +232,11 @@ plot + -- `prep_timeline()` prepare the output of + - `prep_timeline()` prepare the output of ‘r2dii.analysis::target\_sda()’ for ‘plot\_timeline()’. -- `plot_timelineA()` creates a time line plot. + - `plot_timelineA()` creates a time line plot. + + ``` r data <- sda %>% @@ -241,9 +255,11 @@ plot_timelineA(data) + -- `timeline_specs()` creates the default specs data frame for + - `timeline_specs()` creates the default specs data frame for ‘plot\_timelinea()’. + + ``` r # You may use it as a template to create your custom specs timeline_specs(data) diff --git a/man/figures/README-unnamed-chunk-5-1.png b/man/figures/README-unnamed-chunk-5-1.png index 5b9dc94d..cd7c244f 100644 Binary files a/man/figures/README-unnamed-chunk-5-1.png and b/man/figures/README-unnamed-chunk-5-1.png differ diff --git a/man/figures/README-unnamed-chunk-5-2.png b/man/figures/README-unnamed-chunk-5-2.png index 9989bd3c..996988ec 100644 Binary files a/man/figures/README-unnamed-chunk-5-2.png and b/man/figures/README-unnamed-chunk-5-2.png differ