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

Fix annotations overlap #221

Merged
merged 7 commits into from
Jun 7, 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
37 changes: 8 additions & 29 deletions R/plot_trajectory.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 +
Expand Down
6 changes: 4 additions & 2 deletions R/prep_trajectory.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 26 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()’.

<!-- end list -->

``` r
market_share
#> # A tibble: 1,170 x 8
Expand All @@ -61,8 +63,11 @@ market_share
#> # … with 1,160 more rows, and 1 more variable: technology_share <dbl>
```

- `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()’.

<!-- end list -->

``` r
data_trajectory <- prep_trajectory(
Expand All @@ -76,7 +81,9 @@ data_trajectory <- prep_trajectory(
)
```

- `plot_trajectoryA()` is an alternative to `plot_trajectoryB()`.
- `plot_trajectoryA()` is an alternative to `plot_trajectoryB()`.

<!-- end list -->

``` r
# `plot_trajectoryA()` takes more arguments
Expand Down Expand Up @@ -107,6 +114,7 @@ plot_trajectoryA(
<img src="man/figures/README-unnamed-chunk-5-1.png" width="100%" style="display: block; margin: auto auto auto 0;" />

``` r

# more elaborate annotations, title and labels

data_trajectory <- prep_trajectory(
Expand All @@ -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
Expand All @@ -148,9 +156,11 @@ plot +

<img src="man/figures/README-unnamed-chunk-5-2.png" width="100%" style="display: block; margin: auto auto auto 0;" />

- `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.

<!-- end list -->

``` r
# Default colours, all data, added title
Expand All @@ -174,6 +184,7 @@ plot +
<img src="man/figures/README-unnamed-chunk-6-1.png" width="100%" style="display: block; margin: auto auto auto 0;" />

``` r

# Custom colours, all data, no title
power_colors_custom <- tibble(
technology = c("coalcap", "oilcap", "gascap", "nuclearcap", "hydrocap", "renewablescap"),
Expand All @@ -190,6 +201,7 @@ plot
<img src="man/figures/README-unnamed-chunk-6-2.png" width="100%" style="display: block; margin: auto auto auto 0;" />

``` r

# Default colours, selected data and labels (metric_type parameters), added title

sector <- "automotive"
Expand Down Expand Up @@ -220,9 +232,11 @@ plot +

<img src="man/figures/README-unnamed-chunk-6-3.png" width="100%" style="display: block; margin: auto auto auto 0;" />

- `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.

<!-- end list -->

``` r
data <- sda %>%
Expand All @@ -241,9 +255,11 @@ plot_timelineA(data) +

<img src="man/figures/README-unnamed-chunk-7-1.png" width="100%" style="display: block; margin: auto auto auto 0;" />

- `timeline_specs()` creates the default specs data frame for
- `timeline_specs()` creates the default specs data frame for
‘plot\_timelinea()’.

<!-- end list -->

``` r
# You may use it as a template to create your custom specs
timeline_specs(data)
Expand Down
Binary file modified man/figures/README-unnamed-chunk-5-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/README-unnamed-chunk-5-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.