Simplify scale figure with accessible colors

This commit is contained in:
Armin
2026-08-14 12:38:03 +00:00
parent 1909da3720
commit 9f809bad1e
6 changed files with 31 additions and 69 deletions
+27 -48
View File
@@ -49,31 +49,13 @@ trajectory_rows <- lapply(seq_len(nrow(trajectory_spec)), function(i) {
})
trajectories <- do.call(rbind, trajectory_rows)
anchor_spec <- data.frame(
party_id = c(1545, 1375, 1565, 1567, 433, 809),
year = c(2021, 2021, 2019, 1979, 2022, 2020),
label = c(
"The Left 2021", "CDU 2021", "PiS 2019",
"UK Conservatives 1979", "French National Front 2022",
"US Republicans 2020"
)
)
anchors <- merge(anchor_spec, panel, by = c("party_id", "year"), all.x = TRUE, sort = FALSE)
if (anyNA(anchors$economic_lr) || anyNA(anchors$galtan)) {
stop("One or more declared scale anchors are absent from the release panel")
}
anchors$series <- "Standalone anchors"
anchors$display_role <- "anchor"
anchors$is_labelled <- TRUE
plot_columns <- c(
"party_id", "party_name_english", "country", "year", "series",
"display_role", "is_labelled", "label", "economic_lr",
"economic_lr_q025", "economic_lr_q975", "galtan", "galtan_q025",
"galtan_q975", "source_support_class"
)
plot_data <- rbind(trajectories[plot_columns], anchors[plot_columns])
plot_data <- trajectories[plot_columns]
write.csv(
plot_data,
file.path(output_dir, "party_scale_example_plot_data.csv"),
@@ -86,21 +68,15 @@ label_positions <- data.frame(
label = c(
"Fidesz 1990", "Fidesz 2022",
"Danish Social Democrats 2007", "Danish Social Democrats 2019",
"US Democrats 1944", "US Democrats 2020",
"The Left 2021", "CDU 2021", "PiS 2019",
"UK Conservatives 1979", "French National Front 2022",
"US Republicans 2020"
"US Democrats 1944", "US Democrats 2020"
),
plot_label = c(
"Fidesz\n1990", "Fidesz\n2022",
"Danish Social Democrats\n2007", "Danish Social Democrats\n2019",
"US Democrats\n1944", "US Democrats\n2020",
"The Left\n2021", "CDU\n2021", "PiS\n2019",
"UK Conservatives\n1979", "French National Front\n2022",
"US Republicans\n2020"
"US Democrats\n1944", "US Democrats\n2020"
),
label_x = c(.84, .36, .12, .33, .73, .36, .11, .65, .14, .84, .58, .88),
label_y = c(.36, .93, .32, .49, .33, .27, .16, .48, .93, .51, .91, .78)
label_x = c(.84, .36, .12, .33, .73, .36),
label_y = c(.36, .93, .32, .49, .33, .27)
)
labelled <- merge(labelled, label_positions, by = "label", all.x = TRUE, sort = FALSE)
if (anyNA(labelled$label_x) || anyNA(labelled$label_y)) {
@@ -113,39 +89,34 @@ trajectories$series <- factor(trajectories$series, levels = trajectory_levels)
p <- ggplot() +
geom_path(
data = trajectories,
aes(x = economic_lr, y = galtan, group = series, linetype = series),
colour = "grey25", linewidth = .7,
aes(x = economic_lr, y = galtan, group = series, colour = series, linetype = series),
linewidth = .85,
arrow = grid::arrow(type = "closed", length = grid::unit(.065, "inches"))
) +
geom_point(
data = trajectories,
aes(x = economic_lr, y = galtan),
colour = "grey35", size = 1.1
aes(x = economic_lr, y = galtan, colour = series, shape = series),
size = 1.4
) +
geom_errorbar(
data = labelled,
aes(x = economic_lr, ymin = galtan_q025, ymax = galtan_q975),
width = 0, colour = "grey45", alpha = .65
aes(x = economic_lr, ymin = galtan_q025, ymax = galtan_q975, colour = series),
width = 0, alpha = .75
) +
geom_errorbar(
data = labelled,
aes(y = galtan, xmin = economic_lr_q025, xmax = economic_lr_q975),
orientation = "y", width = 0, colour = "grey45", alpha = .65
aes(y = galtan, xmin = economic_lr_q025, xmax = economic_lr_q975, colour = series),
orientation = "y", width = 0, alpha = .75
) +
geom_point(
data = anchors,
aes(x = economic_lr, y = galtan),
shape = 21, fill = "white", colour = "black", size = 2.5, stroke = .7
) +
geom_point(
data = labelled[labelled$display_role == "trajectory", ],
aes(x = economic_lr, y = galtan),
shape = 16, colour = "black", size = 2.1
data = labelled,
aes(x = economic_lr, y = galtan, colour = series, shape = series),
size = 2.4
) +
geom_segment(
data = labelled,
aes(x = economic_lr, y = galtan, xend = label_x, yend = label_y),
colour = "grey55", linewidth = .25
aes(x = economic_lr, y = galtan, xend = label_x, yend = label_y, colour = series),
linewidth = .3, show.legend = FALSE
) +
geom_text(
data = labelled,
@@ -155,12 +126,20 @@ p <- ggplot() +
scale_linetype_manual(
values = c("Fidesz" = "solid", "Danish Social Democrats" = "dashed", "US Democrats" = "dotdash")
) +
scale_colour_manual(
values = c("Fidesz" = "#D55E00", "Danish Social Democrats" = "#009E73", "US Democrats" = "#0072B2")
) +
scale_shape_manual(
values = c("Fidesz" = 16, "Danish Social Democrats" = 17, "US Democrats" = 15)
) +
scale_x_continuous(limits = c(0, 1), breaks = c(0, .25, .5, .75, 1)) +
scale_y_continuous(limits = c(0, 1), breaks = c(0, .25, .5, .75, 1)) +
labs(
x = "Economic: left (0) to right (1)",
y = "Cultural: cosmopolitan (0) to traditionalist (1)",
linetype = "Election-year path"
colour = "Election-year path",
linetype = "Election-year path",
shape = "Election-year path"
) +
theme_minimal(base_size = 9) +
theme(