-
Notifications
You must be signed in to change notification settings - Fork 0
/
05_vasstacked.R
109 lines (88 loc) · 4.15 KB
/
05_vasstacked.R
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
#load csv of priority countries
country_regions <- read.csv("country_selection.csv")
country_regions <- country_regions %>%
dplyr::select(.id, region, CountryName) %>%
dplyr::filter(.id!="")
# combine list
combined <- bind_rows(ir)
combined <- merge(combined, country_regions, by="CountryName")
combined <- combined %>%
filter(youngest_1259m == 1) %>%
select(CountryName, youngest_1259m, zd, vas6, dwormed, region, v005)
#analytical weights
counts <- combined %>%
group_by(CountryName) %>%
summarise(
zd0_vas61 = sum((zd == 0 & vas6 == 1) * v005) / sum(v005) * 100,
zd0_vas60 = sum((zd == 0 & vas6 == 0) * v005) / sum(v005) * 100,
zd1_vas61 = sum((zd == 1 & vas6 == 1) * v005) / sum(v005) * 100,
zd1_vas60 = sum((zd == 1 & vas6 == 0) * v005) / sum(v005) * 100
)
# Add combined length column and sort by it
counts <- counts %>%
mutate(combined_length = zd1_vas60 + zd1_vas61) %>%
arrange(combined_length)
# Reshape the data from wide to long format
counts_long <- counts %>%
select(-combined_length) %>%
gather(key = "Scenario", value = "Percentage", -CountryName)
# Reorder CountryName factor levels based on sorted combined_length values
counts_long$CountryName <- factor(counts_long$CountryName, levels = counts$CountryName)
# Reorder the fill colors for consistent stacking
counts_long$Scenario <- factor(counts_long$Scenario, levels = c("zd0_vas61", "zd0_vas60", "zd1_vas61", "zd1_vas60"))
# Define custom colors for each scenario
custom_colors <- c("zd0_vas61" = "grey", "zd0_vas60" = "skyblue", "zd1_vas61" = "pink", "zd1_vas60" = "red")
# Plot the data
ggplot(counts_long, aes(x = CountryName, y = Percentage, fill = Scenario)) +
geom_bar(stat = "identity", position = "stack", width = 0.8) +
labs(x = "Country", y = "Percentage (%)") +
ggtitle("Zero Dose and VAS Scenarios by Country (age=12-59m)") +
scale_fill_manual(values = custom_colors) +
theme_minimal() +
theme(
axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1),
axis.line = element_line(colour = "black"),
plot.title = element_text(hjust = 0.5),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank()
)
write.csv(counts_long, file="ZD_VAS_stacked_longformat.csv")
##region grouping
counts <- combined %>%
group_by(CountryName, region) %>%
summarise(
zd0_vas61 = sum((zd == 0 & vas6 == 1) * v005) / sum(v005) * 100,
zd0_vas60 = sum((zd == 0 & vas6 == 0) * v005) / sum(v005) * 100,
zd1_vas61 = sum((zd == 1 & vas6 == 1) * v005) / sum(v005) * 100,
zd1_vas60 = sum((zd == 1 & vas6 == 0) * v005) / sum(v005) * 100
)
# Add combined length column and sort within each region by it
counts <- counts %>%
mutate(combined_length = zd1_vas60 + zd1_vas61) %>%
arrange(region, combined_length)
# Reshape the data from wide to long format
counts_long <- counts %>%
select(-combined_length) %>%
gather(key = "Scenario", value = "Percentage", -CountryName, -region)
# Reorder CountryName factor levels based on sorted combined_length values within each region
counts_long$CountryName <- factor(counts_long$CountryName, levels = counts$CountryName)
# Reorder the fill colors for consistent stacking
counts_long$Scenario <- factor(counts_long$Scenario, levels = c("zd0_vas61", "zd0_vas60", "zd1_vas61", "zd1_vas60"))
# Define custom colors for each scenario
custom_colors <- c("zd0_vas61" = "grey", "zd0_vas60" = "skyblue", "zd1_vas61" = "pink", "zd1_vas60" = "red")
# Plot the data
ggplot(counts_long, aes(x = CountryName, y = Percentage, fill = Scenario)) +
geom_bar(stat = "identity", position = "stack") +
labs(x = "Country", y = "Percentage (%)") +
ggtitle("Zero Dose and VAS Scenarios by Country (age=12-23m)") +
scale_fill_manual(values = custom_colors) +
theme_minimal() +
theme(
axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1),
axis.line = element_line(colour = "black"),
plot.title = element_text(hjust = 0.5),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank()
) +
facet_wrap(~ region, scales = "free_x", nrow = 1) +
theme(legend.position = "bottom")