ggplot2绘制面积图
2021-01-23 本文已影响0人
R语言数据分析指南
使用流图来说明肯尼亚各县的不同水源,并用肯尼亚国旗的阴影作为颜色
library(tidyverse)
library(janitor)
#devtools::install_github("Shelmith-Kariuki/rKenyaCensus")
library(rKenyaCensus)
# remotes::install_github("davidsjoberg/ggstream")
library(ggstream)
library(paletteer)
library(patchwork)
library(showtext)
kenya_pal <- rev(c("#006300","#257D25", "#35B035",
"#97FC97", "#FFFFFF","#B50000", "#C20000",
"#9C0000", "#F04848", "#FFFFFF", "#D6D6D6",
"#999999","#4D4D4D","#000000"))
map_bck_clr <- "#BAE7F7"
map_neutral <- "grey40"
water <- rKenyaCensus::V4_T2.15
water <- clean_names(water)
water_clean <- water %>%
ungroup() %>%
filter(county != "xxx", admin_area == "County") %>%
select(-not_stated) %>%
pivot_longer(cols = pond:publictap_standpipe,
names_to = "source", values_to = "value") %>%
filter(!is.na(value)) %>%
mutate(
source = case_when(
source == "borehole_tube_well" ~ "Borehole\ntubewell",
source == "bottledwater" ~ "Bottled\n water",
source == "dam_lake" ~ "Dam lake",
source == "pipedintodwelling" ~ "Piped into\ndwelling",
source == "pipedtoyard_plot" ~ "Piped to\nyard/plot",
source == "pond" ~"Pond",
source == "publictap_standpipe" ~"public \ntap/standpipe",
source == "rain_harvestedwater" ~ "Rain harvested\n / water",
TRUE ~ str_replace_all(str_to_title(source), "_", "\n")
)
)
water_prep <- water_clean %>%
arrange(county) %>%
mutate(x_num = as.numeric(as_factor(county)),
x_labels = county)
county_breaks <- water_prep$x_num
county_labels <- water_prep$x_labels
plt <- water_prep %>%
ggplot() +
geom_stream(aes(x_num,value,fill=source,color = source)) +
scale_x_continuous(breaks = county_breaks,
labels = county_labels)+
scale_fill_manual(values = kenya_pal) +
scale_color_manual(values = kenya_pal) +
guides(fill = FALSE,color = FALSE) +
theme_void() +
theme(axis.text.x = element_text(family ="Times",face = "plain",
angle = 90, hjust = 1,size =10),
plot.margin = margin(20,0,30,0))
plt
map = rKenyaCensus::KenyaCounties_SHP %>%
sf::st_as_sf() %>%
clean_names()
water_map <- water_clean %>%
group_by(source) %>%
slice_max(value,n = 10)
source_list <- water_map %>%
select(source) %>%
arrange() %>%
pull() %>% unique()
color_pal <- c()
color_pal[source_list] <- kenya_pal
map_source1 <- map %>%
right_join(filter(water_map, source %in% source_list[1:7]))
map_source2 <- map %>%
right_join(filter(water_map, source %in% source_list[8:14]))
map1_plt <- ggplot() +
geom_sf(data = map, fill = map_bck_clr, size = 0.2,
alpha = 0.3, color = "black") +
geom_sf(data = map_source1, aes(fill = source),
size = 0.3, color = map_neutral) +
scale_fill_manual(values = color_pal) +
facet_wrap(~source, nrow = 1, strip.position="top") +
guides(fill = FALSE) +
theme_void() +
theme(panel.spacing.x = unit(2, "lines"),
strip.text.x = element_text(family = "robot",
size = 18, margin = margin(0,0,5,0)))
map2_plt <- ggplot() +
geom_sf(data = map, fill = map_bck_clr, size = 0.2,
alpha = 0.3,color = "black") +geom_sf(data = map_source2,
aes(fill = source), size = 0.3, color = map_neutral) +
scale_fill_manual(values = color_pal) +
facet_wrap(~source, nrow = 1, strip.position="bottom") +
guides(fill = FALSE) +
theme_void() +
theme(panel.spacing.x = unit(2, "lines"),
strip.text.x = element_text(family = "robot",
size = 18, margin = margin(5,0,5,0)))
final <- map1_plt / plt / map2_plt +
plot_layout(nrow = 3, heights = c(1,3,1)) +
plot_annotation(
caption = "Visualization: Christophe Nicault | Data: rKenyaCensus",
theme = theme(plot.caption = element_text(family = "heebo", size = 16,
color = "black", margin = margin(20,0,0,0)))
)
final