Repository navigation
Expand file tree
/
Copy pathfc_plot.CS.R
More file actions
51 lines (44 loc) · 1.69 KB
/
Copy pathfc_plot.CS.R
File metadata and controls
51 lines (44 loc) · 1.69 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
# fc_plot.CS.R
# Plotting case stability from list generated by CS.adj()
plot.CS <- function(stability_list,
tit = "Centrality stability") {
df_list <- list()
for (i in seq_along(stability_list)) {
# Capturing proportions from element name
raw_name <- names(stability_list)[i]
proportion_value <- as.numeric(gsub("prop_", "", raw_name))
# Building data.frame
metrics_df <- stability_list[[i]]
# Iterating by metric (column)
for (metric_name in colnames(metrics_df)) {
values <- metrics_df[[metric_name]]
mean_val <- mean(values, na.rm = TRUE)
se <- sd(values, na.rm = TRUE) / sqrt(sum(!is.na(values)))
ci <- 1.96 * se
df_list[[length(df_list) + 1]] <- data.frame(
Proportion = proportion_value,
Metric = metric_name,
MeanCorrelation = mean_val,
CI_Lower = mean_val - ci,
CI_Upper = mean_val + ci
)
}
}
# Merging data.frames in a sigle one
df <- do.call(rbind, df_list)
# Plot
ggplot(df, aes(x = Proportion * 100)) +
geom_ribbon(aes(ymin = CI_Lower, ymax = CI_Upper, fill = Metric), alpha = 0.2) +
geom_line(aes(y = MeanCorrelation, color = Metric), linewidth = 1.2) +
geom_point(aes(y = MeanCorrelation, color = Metric), size = 2) +
geom_hline(yintercept = 0.7, linetype = "dashed", color = "gray40", linewidth = 1) +
scale_x_continuous(trans = "reverse", breaks = sort(unique(df$Proportion * 100), decreasing = TRUE)) +
coord_cartesian(ylim = c(0, 1)) +
labs(
title = tit,
x = "Sampled cases (%)",
y = "Average correlation with original sample"
) +
theme_minimal() +
theme(legend.position = "top")
}