# create world map data
world_map <- map_data("world")
# standardized country names
plot_data <- analysis_data |>
mutate(region = case_when(
country == "Bosnia & Herzegovina" ~ "Bosnia and Herzegovina",
country == "Brunei Darussalam" ~ "Brunei",
country == "Myanmar" ~ "Burma",
country == "Ivory Coast" ~ "Cote d'Ivoire",
TRUE ~ country
)) |>
# order income group
mutate(
income_group = factor(income_group,
levels = c("Lower-middle income",
"Upper-middle income",
"High income"))
) |>
# calculate each countries' location
left_join(
world_map %>%
group_by(region) %>%
summarise(
long = mean(long, na.rm = TRUE),
lat = mean(lat, na.rm = TRUE),
.groups = 'drop'
),
by = "region"
) |>
filter(!is.na(long))
# arrange three colors for different income groups
three_income_colors <- c(
"Lower-middle income" = "#4E79A7", # 深蓝色
"Upper-middle income" = "#F28E2B", # 橙色
"High income" = "#E15759" # 红色
)
# visualized by using a world map
p1 <- ggplot() +
geom_polygon(data = world_map,
aes(x = long, y = lat, group = group),
fill = "white", color = "black", linewidth = 0.15) +
# bubble world map
geom_point(data = plot_data,
aes(x = long, y = lat,
size = emission_2024,
color = income_group),
alpha = 0.7) +
# adjust bubble ratio
scale_size_continuous(
name = "CO2 per capita (metric tons)",
range = c(2, 6),
breaks = c(1, 5, 10, 15, 20),
guide = guide_legend(
title.position = "top",
title.hjust = 0.5,
direction = "horizontal",
nrow = 1
)
) +
scale_color_manual(
name = "Income Group (2024)",
values = three_income_colors,
guide = guide_legend(
title.position = "top",
title.hjust = 0.5,
direction = "horizontal",
nrow = 1,
override.aes = list(size = 4)
)
) +
# fixed ratio
coord_fixed(
ratio = 1.3,
xlim = c(-180, 180),
ylim = c(-60, 85)
) +
# add labels
labs(
title = "Global CO2 Emissions per Capita by Income Group (2024)",
subtitle = "Bubble size = CO2 emissions per capita | Color = Income group",
caption = "Data: World Bank"
) +
# theme setting
theme_void() +
theme(
plot.title = element_text(
hjust = 0.5,
face = "bold",
size = 14,
margin = margin(b = 5, t = 5)
),
plot.subtitle = element_text(
hjust = 0.5,
size = 10,
margin = margin(b = 8)
),
plot.caption = element_text(
hjust = 0.5,
size = 8,
margin = margin(t = 5)
),
# legend position at bottom
legend.position = "bottom",
legend.box = "vertical",
legend.box.just = "center",
legend.direction = "horizontal",
# legend margin
legend.box.margin = margin(5, 0, 5, 0),
legend.spacing.x = unit(20, "pt"),
# legend text
legend.title = element_text(size = 10, face = "bold"),
legend.text = element_text(size = 9),
# margin
plot.margin = margin(10, 10, 40, 10)
)
print(p1)