Skip to content

Instantly share code, notes, and snippets.

@USMortality
Created November 15, 2024 15:44
Show Gist options
  • Save USMortality/8e0c87548aa3501d45924c2dfda0d816 to your computer and use it in GitHub Desktop.
Save USMortality/8e0c87548aa3501d45924c2dfda0d816 to your computer and use it in GitHub Desktop.
Measles Cases & Vaxx Coverage [World]
library(jsonlite)
library(tibble)
library(dplyr)
library(ggplot2)
library(scales)
sf <- 2
width <- 600 * sf
height <- 335 * sf
options(vsc.dev.args = list(width = width, height = height, res = 72 * sf))
# Cases
df <- as_tibble(fromJSON("https://ghoapi.azureedge.net/api/WHS3_62")$value)
# Prepare the data
plot_data <- df |>
filter(SpatialDimType == "REGION") |>
select(TimeDim, SpatialDim, Value) |>
group_by(TimeDim, SpatialDim) |>
mutate(Value = as.integer(gsub(" ", "", Value))) |>
summarize(Value = sum(Value, na.rm = TRUE))
# Plot using ggplot2
chart <- ggplot(plot_data, aes(x = TimeDim, y = Value, group = SpatialDim)) +
geom_line() +
geom_point() +
facet_wrap(~SpatialDim) +
theme_minimal() +
labs(
title = "Worldwide Measles Cases by Region",
subtitle = "Source: WHO",
x = "Year", y = "Cases"
) +
scale_y_continuous(labels = label_number(scale = 1e-6, suffix = "M")) +
theme(
strip.text = element_text(size = 12), # Facet label size
legend.position = "none" # Hide legend
)
ggplot2::ggsave(
filename = "chart1.png", plot = chart, width = width, height = height,
units = "px", dpi = 72 * sf, device = grDevices::png, type = c("cairo")
)
# Immunization Coverage
df <- as_tibble(fromJSON("https://ghoapi.azureedge.net/api/MCV2")$value)
# Prepare the data
plot_data <- df |>
filter(SpatialDimType == "REGION") |>
select(TimeDim, SpatialDim, Value) |>
group_by(TimeDim, SpatialDim) |>
mutate(Value = as.integer(gsub(" ", "", Value)) / 100) |>
summarize(Value = sum(Value, na.rm = TRUE))
# Plot using ggplot2
chart <- ggplot(plot_data, aes(x = TimeDim, y = Value, group = SpatialDim)) +
geom_line() +
geom_point() +
facet_wrap(~SpatialDim) +
theme_minimal() +
labs(
title = "Measles immunization coverage by the nationally recommended age (%)",
subtitle = "Source: WHO",
x = "Year", y = "Cases"
) +
scale_y_continuous(labels = label_percent()) +
theme(
strip.text = element_text(size = 12), # Facet label size
legend.position = "none" # Hide legend
)
ggplot2::ggsave(
filename = "chart2.png", plot = chart, width = width, height = height,
units = "px", dpi = 72 * sf, device = grDevices::png, type = c("cairo")
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment