Skip to content

Commit

Permalink
changed plot range to look nicer and added fix to gtsave() error
Browse files Browse the repository at this point in the history
  • Loading branch information
gaelso committed Oct 29, 2024
1 parent 78f82af commit c3f450e
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 74 deletions.
22 changes: 16 additions & 6 deletions R/fct_forestplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,15 @@ fct_forestplot <- function(
col_ciup <- rlang::enquo(.ciupper)

## data range for the plot
E_min <- eval(substitute(min(.data$.cilower)))
E_max <- eval(substitute(max(.data$.ciupper)))
E_min <- eval(substitute(min(.data$.cilower, 0)))
E_max <- eval(substitute(max(.data$.ciupper, 0)))
E_range <- c(E_min, E_max)

## Extend range to 5% of the data range to make it look nicer
plot_min <- E_min - ((E_max - E_min) * 0.05)
plot_max <- E_max + ((E_max - E_min) * 0.05)
plot_range <- c(plot_min, plot_max)

## Output
gt_out <- .data |>
dplyr::select(!!col_id, !!col_value, !!col_uperc, !!col_cilo, !!col_ciup) |>
Expand Down Expand Up @@ -99,22 +104,27 @@ fct_forestplot <- function(
linewidth = 12
) +
geom_vline(xintercept = 0, linetype = "dotted", linewidth = 8) +
geom_vline(xintercept = E_min, linewidth = 4) +
geom_vline(xintercept = E_max, linewidth = 4) +
geom_vline(xintercept = plot_min, linewidth = 4) +
geom_vline(xintercept = plot_max, linewidth = 4) +
theme_minimal() +
scale_y_discrete(breaks = NULL) +
scale_x_continuous(breaks = NULL) +
theme(axis.text = element_text(size = 120)) +
labs(x = element_blank(), y = element_blank()) +
coord_cartesian(xlim = E_range)
coord_cartesian(xlim = plot_range)

}) |>
ggplot_image(height = px(30), aspect_ratio = 5)
}
)

## Save the table as img is path specified
if (is.character(.filename)) gtsave(gt_out, filename = .filename)
if (is.character(.filename)) {
gtsave(gt_out, filename = .filename)

## Workaround gtsave not working on linux/macos
f <- chromote::default_chromote_object(); f$close()
}

gt_out

Expand Down
111 changes: 43 additions & 68 deletions R/test-functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ gt_trans <- res_trans |>
.ciupper = E_ciupper,
.id_colname = "Land use<br>transition code",
.conflevel = "90%",
.filename = "tests/gt_trans2.png"
.filename = "tests/gt_trans.png"
)

##
Expand Down Expand Up @@ -158,79 +158,54 @@ res_redd <- sim_redd |>
) |>
select(redd_id, E, E_U, E_ME, E_cilower, E_ciupper)

E_min <- min(res_redd$E_cilower)
E_max <- max(res_redd$E_ciupper)
E_range <- c(E_min, E_max)

gt_redd <- res_redd |>
select(-E_ME) |>
mutate(
E_distribution = redd_id,
E_cilower = if_else(E_cilower == 0, NA_integer_, E_cilower),
E_ciupper = if_else(E_ciupper == 0, NA_integer_, E_ciupper)
) |>
gt() |>
cols_label(
redd_id = md("REDD+ activity<br>per time period"),
E = "E (tCO2/y)",
E_U = "U (%)",
E_cilower = "CI (90%)",
E_distribution = ""
) |>
cols_merge(
columns = c(E_cilower, E_ciupper),
pattern = "<<({1}>> - <<{2})>>",
) |>
tab_spanner(
label = "MCS results",
columns = starts_with("E")
) |>
fmt_number(decimals = 0) |>
fmt_percent(columns = "E_U", scale_values = F, decimals = 0) |>
sub_missing(
columns = "E_U",
missing_text = "-"
) |>
text_transform(
locations = cells_body(columns = 'E_distribution'),
fn = function(column) {
map(column, function(x){

## !! FOR TESTING ONLY
# x = "T1_DF_ev_moist_closed"
# column = res_trans$trans_id
## !!

res_redd |>
## NEED UNIQUE ID
filter(redd_id == x) |>
ggplot() +
geom_point(aes(x = E, y = redd_id), size = 40) +
geom_segment(aes(x = E_cilower, xend = E_ciupper, y = redd_id, yend = redd_id), linewidth = 12) +
geom_vline(xintercept = 0, linetype = "dotted", linewidth = 8) +
geom_vline(xintercept = E_min, linewidth = 4) +
geom_vline(xintercept = E_max, linewidth = 4) +
theme_minimal() +
scale_y_discrete(breaks = NULL) +
scale_x_continuous(breaks = NULL) +
theme(axis.text = element_text(size = 120)) +
labs(x = element_blank(), y = element_blank()) +
coord_cartesian(xlim = gg_E_range)

}) |>
ggplot_image(height = px(30), aspect_ratio = 5)
}
)

gtsave(data = gt_redd, filename = "tests/gt_redd.png")
gt_redd <- fct_forestplot(
.data = res_redd,
.id = redd_id,
.value = E,
.uperc = E_U,
.cilower = E_cilower,
.ciupper = E_ciupper,
.id_colname = "REDD+ activity<br>per time period",
.conflevel = "90%",
.filename = "tests/gt_redd.png"
)

gt_redd

## redd+ acti to time period
sim_period <- sim_redd |>
group_by(sim_no, time_period) |>
summarise(E_period = sum(E_redd), .groups = "drop")
summarise(E_sim = sum(E_sim), .groups = "drop")

sim_period |> filter(sim_no == 1)

res_period <- sim_period |>
group_by(time_period) |>
summarise(
E = round(median(E_sim)),
E_ciupper = round(quantile(E_sim, 1 - ci_alpha/2)),
E_cilower = round(quantile(E_sim, ci_alpha/2)),
.groups = "drop"
) |>
mutate(
E_ME = round((E_ciupper - E_cilower) / 2),
E_U = round(E_ME / E * 100),
) |>
select(time_period, E, E_U, E_ME, E_cilower, E_ciupper)

gt_period <- res_period |>
fct_forestplot(
.id = time_period,
.value = E,
.uperc = E_U,
.cilower = E_cilower,
.ciupper = E_ciupper,
.id_colname = "Time period",
.conflevel = "90%",
.filename = "tests/gt_period.png"
)

res_period |> filter(sim_no == 1)
gt_period

## aggregate redd+ periods for the reference level
time_ref <- time |> filter(period_type == "reference")
Expand Down
Binary file added tests/gt_period.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 added tests/gt_redd.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 tests/gt_trans.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit c3f450e

Please sign in to comment.