第 9 章 Multiple plots
::download_file("https://www.dropbox.com/s/7b3nbgfx5bgft8g/drake_annotationmaps.Rdata?dl=1")
xfunload("drake_annotationmaps.Rdata")
<- drake
drake_map ::download_file("https://www.dropbox.com/s/v8rchkn63fzgjj3/drake_facet.Rdata?dl=1")
xfunload("drake_facet.Rdata")
<- drake
drake_facet ::download_file("https://www.dropbox.com/s/apmooy1p0d54d7d/drake_grid.Rdata?dl=1")
xfunload("drake_grid.Rdata")
<- drake drake_grid
9.1 Grid arrangement
- gridExra
install.packages("gridExtra")
library(gridExtra)
9.1.1 grid.arrange
成品
$loadTarget$ggGrid_electionMap()
drake_grid%>% gridExtra::grid.arrange() ggGrid_electionMap
9.1.2 資料整理
將幾個比較小又擠在一起的行政區拆分出來。
$loadTarget$dsf_newTaipeiDPP2()
drake_grid$loadTarget$gg_newTaipei2()
drake_grid gg_newTaipei2
%>%
dsf_newTaipeiDPP filter(
!is.na(dpp)
%>%
) mutate(
shortname=stringr::str_remove(name, "區")
-> dsf_newTaipeiDPP2 )
%>%
dsf_newTaipeiDPP2 ggplot() + geom_sf() +
geom_sf_text(
aes(
label=shortname
size=2
), -> gg_newTaipei
) gg_newTaipei
<- list()
dsf_newTaipeiSplit <-
clusterNames ::str_subset(
stringr$shortname,
dsf_newTaipeiDPP2"[蘆重泰莊永]"
)%>%
dsf_newTaipeiDPP2 filter(
!(shortname %in% clusterNames)
-> dsf_newTaipeiSplit$notClustered
) %>%
dsf_newTaipeiDPP2 filter(
%in% clusterNames
shortname -> dsf_newTaipeiSplit$clustered
)
dsf_newTaipeiSplit
9.1.3 行政區名地圖
%>%
dsf_newTaipeiDPP2 ggplot() + geom_sf(
color="white"
+
) geom_sf_text(
data=dsf_newTaipeiSplit$notClustered,
aes(
label=shortname
size=2
), -> gg_newTaipei2
) gg_newTaipei2
+
gg_newTaipei2 geom_sf_text(
data=dsf_newTaipeiSplit$clustered,
aes(
label=shortname
size=1.5
), +theme_void() -> gg_newTaipeiLabeled
) gg_newTaipeiLabeled
9.1.4 得票率圖編修
+
ggsf_electiontheme(
legend.title = element_blank(),
legend.key.width = unit(5,"points"),
legend.key.height = unit(10,"points"),
legend.position = c(0.85, 0.2)
-> ggsf_electionRefined
) ggsf_electionRefined
9.1.5 併圖
::grid.arrange(
gridExtra
ggsf_electionRefined,
gg_newTaipeiLabeled,nrow=1,
top="民進黨2020總統大選新北市得票率"
-> ggGrid_electionMap )
%>% gridExtra::grid.arrange() ggGrid_electionMap
ggsave(ggGrid_electionMap, file="ggGrid.svg")