-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_limitation_estimates.R
More file actions
188 lines (169 loc) · 6.68 KB
/
plot_limitation_estimates.R
File metadata and controls
188 lines (169 loc) · 6.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# libraries ----
library(ggplot2)
library(readr)
library(dplyr)
library(tidyr)
library(stringr)
library(patchwork)
library(scales)
# Set working directory ----
setwd("/home/sreichl/projects/ResearchAcceleration/")
# input / output ----
plausibility_path <- file.path("data", "anonymized_data_limiting_factors.csv")
output_dir <- file.path("results", "plots")
dir.create(output_dir, recursive = TRUE, showWarnings = FALSE)
# plotting defaults ---------------------------
theme_set(theme_minimal(base_size = 8, base_family = "Arial"))
theme_update(
axis.text.x = element_text(size = 6, colour = "black", family = "Arial"),
axis.text.y = element_text(size = 6, colour = "black", family = "Arial"),
)
# colour palette (same grey for the two neutral halves)
cols <- c(
"Insignificant limit" = "#2980b9",
"Minor limit" = "#3498db",
"Moderate limit L" = "#95a5a6",
"Moderate limit R" = "#95a5a6",
"Major limit" = "#e74c3c",
"Crucial limit" = "#c0392b"
)
height_in <- 3
width_in <- 6
options(repr.plot.width = width_in, repr.plot.height = height_in)
# load data & prepare data for plotting -----------------------------------
raw_df <- read_csv(plausibility_path, show_col_types = FALSE)
long_df <- raw_df |>
pivot_longer(everything(),
names_to = "limit",
values_to = "estimate",
values_drop_na = TRUE) |>
mutate(
estimate = str_to_title(str_squish(estimate)),
estimate = recode(
estimate,
"Insignificant Limit" = "Insignificant limit",
"Minor Limit" = "Minor limit",
"Moderate Limit" = "Moderate limit",
"Major Limit" = "Major limit",
"Crucial Limit" = "Crucial limit"
)
)
# counts per limit & split neutral category
count_df <- long_df |>
count(limit, estimate, name = "n")
# split the neutral answers into left / right halves
neutral_left <- count_df |>
filter(estimate == "Moderate limit") |>
mutate(estimate = "Moderate limit L", n_signed = -n / 2)
neutral_right <- neutral_left |>
mutate(estimate = "Moderate limit R", n_signed = n / 2)
others <- count_df |>
filter(estimate != "Moderate limit") |>
mutate(n_signed = ifelse(estimate %in% c("Insignificant limit","Minor limit"),-n, n))
plot_df <- bind_rows(neutral_left, neutral_right, others)
# fix limit order --------------------------------------------
limit_order <- c('Biological/Physical time limits',
'Resource & infrastructure',
'Input data limitations',
'Human strategic direction',
'Human ethical judgment',
'Human accountability',
'Institutional adaptation',
'Empirical validation',
'Stakeholder coordination',
'Safety & security',
'Scientific community assimilation',
'Data volume management')
plot_df <- plot_df |> mutate(limit = factor(limit,levels = rev(limit_order)))
# stacking order --------------------------------------------
ord <- c("Insignificant limit",
"Minor limit",
"Moderate limit L",
"Crucial limit",
"Major limit",
"Moderate limit R")
plot_df$estimate <- factor(plot_df$estimate, levels = ord)
# set max_cnt for symmetric axis limits --------------------------------------------
max_cnt <- 8
# plot ------------------------------------------------------------------------
p <- ggplot(plot_df, aes(x = limit, y = n_signed, fill = estimate)) +
geom_col(width = 0.8) +
# add response numbers for not-plausible
geom_text(
aes(label = ifelse(estimate %in% c("Moderate limit L", "Moderate limit R"),
"", # full total for neutral
abs(n_signed))), # raw count for all others
position = position_stack(vjust = 0.5),
colour = "white",
size = 3
) +
# add response numbers for plausible
geom_text(
data = plot_df %>% filter(estimate == "Moderate limit L"), # one row per limit
aes(x = limit, y = 0, # centre (x-axis after flip)
label = abs(n_signed) * 2), # full neutral count
colour = "white",
size = 3,
inherit.aes = FALSE
) +
# add total response numbers per limit
geom_text(
data = plot_df %>% # reuse current data-frame
group_by(limit) %>% # one row per limit
summarise(total = sum(abs(n_signed)), .groups = "drop") %>%
mutate(limit = factor(limit, levels = limit_order)),
aes(x = limit, y = -8, label = paste0("N=", total)), # fixed left position
inherit.aes = FALSE,
hjust = 0, vjust = 0.5,
size = 3,
colour="grey50"
) +
coord_flip() +
scale_x_discrete(
labels = function(l)
ifelse(l == "Biological/Physical time limits",
"Biological/physical time limits",
l)
) +
# x-axis
scale_y_continuous(limits = c(-max_cnt, max_cnt),,
breaks = seq(-max_cnt, max_cnt, 1),
labels = abs) +
scale_fill_manual(
values = cols,
# show each category once, collapse the two neutrals to one label
breaks = c("Insignificant limit",
"Minor limit",
"Moderate limit L",
"Major limit",
"Crucial limit"),
labels = c("Insignificant limit",
"Minor limit",
"Moderate limit",
"Major limit",
"Crucial limit"),
name = NULL,
) +
labs(x = "Limiting factors", y = "Less limiting ← Number of responses → More limiting") +
theme(
panel.grid.major.x = element_blank(), # remove vertical major grid-lines
panel.grid.minor.x = element_blank(), # remove vertical minor grid-lines
panel.grid.major.y = element_blank(),
panel.grid.minor = element_blank(),
axis.line.x = element_blank(), # no centre line
axis.text.x = element_blank(),
# LEGEND
legend.position = "bottom",
legend.location = "plot", # use whole plot (not just panel) as anchor
legend.justification = "center", # left-align legend box to plot
legend.box.just = "center", # left-align items inside the box
legend.text = element_text(size = 7), # smaller text
legend.key.size = unit(0.3, "cm"), # smaller colour swatches
legend.spacing.y = unit(0.1, "cm") # tighter rows
)
# show plot -------------------------------------------------------------------
print(p)
# save plot -------------------------------------------------------------------
ggsave(file.path(output_dir, "limitation_estimates_plot.png"),
plot = p, width = width_in, height = height_in,
dpi = 300, bg = "white")