在相同的空间比例下使用geom_sf制作的小型多张地图 [英] Small multiple maps with geom_sf at the same spatial scale
问题描述
我想使用ggplot2::geom_sf
绘制带有多个小地图的图形.这里的挑战是如何做到这一点,以使所有地图在图像中居中并保持相同的空间比例.这是问题所在(下面的可重现示例的数据):
I would like to plot a figure with small multiple maps using ggplot2::geom_sf
. The challenge here is how to do this keeping all maps centered in the image and at the same spatial scale. Here is the problem (data for reproducible example below):
使用facet_wrap
的简单地图将所有多边形置于相同的空间比例,但它们并未居中.
A simple map using facet_wrap
put all polygons at the same spatial scale, but they are not centered.
ggplot(states6) +
geom_sf() +
facet_wrap(~name_state)
这是 SO问题
使用cowplot
.在这种情况下,多边形居中,但它们的空间比例不同
Here is a solution from this SO question
that uses cowplot
. In this case, polygons are centered but they come at different spatial scales
g <- purrr::map(unique(states6$name_state),
function(x) {
# subset data
temp_sf <- subset(states6, name_state == x)
ggplot() +
geom_sf(data = temp_sf, fill='black') +
guides(fill = FALSE) +
ggtitle(x) +
ggsn::scalebar(temp_sf, dist = 100, st.size=2,
height=0.01, model = 'WGS84',
transform = T, dist_unit='km')
})
g2 <- cowplot::plot_grid(plotlist = g)
g2
我在tmap
库中发现了同样的问题.
I've found the same problem using the tmap
library.
tm_shape(states6) +
tm_borders(col='black') +
tm_fill(col='black') +
tm_facets(by = "name_state ", ncol=3) +
tm_scale_bar(breaks = c(0, 50, 100), text.size = 3)
所需的输出
我想要获得的输出与此类似:
Desired output
The output I would like to get is something similar to this:
library(sf)
library(geobr)
library(mapview)
library(ggplot2)
library(ggsn)
library(cowplot)
library(purrr)
library(tmap)
# Read all Brazilian states
states <- geobr::read_state(code_state = 'all', year=2015)
# Select six states
states6 <- subset(states, code_state %in% c(35,33,53,29,31,23))
推荐答案
这不是理想的选择,但您可以通过编程以相同的框大小绘制多个图,然后然后使用:: gridExtra将它们放在一起.要获取每个框的中心,请使用每个几何的质心.
It´s not ideal but you can make several plots programmatically with the same box size and then put them together using ::gridExtra. To get the center of each box, use the centroid of each geometry.
library(sf)
library(geobr)
library(mapview)
library(ggplot2)
library(gridExtra)
阅读所有巴西州:
states <- geobr::read_state(code_state = 'all', year=2015)
选择六个状态:
states6 <- subset(states, code_state %in% c(35,33,53,29,31,23))
形心,供ggplot波纹管中参考(我必须设置投影,如有需要,请在此处进行更改):
centroids, for reference in the ggplot bellow (I had to set the projection, make changes here if needed):
states6$centroid <-
sf::st_transform(states6, 29101) %>%
sf::st_centroid() %>%
sf::st_transform(., '+proj=longlat +ellps=GRS80 +no_defs') %>%
sf::st_geometry()
设置填充:
padding <-7
绘图功能:
graph <- function(x){
ggplot2::ggplot(states6[x,]) +
geom_sf() +
coord_sf(xlim = c(states6$centroid[[x]][1]-padding ,
states6$centroid[[x]][1]+padding),
ylim = c(states6$centroid[[x]][2]-padding ,
states6$centroid[[x]][2]+padding),
expand = FALSE)
}
创建一堆情节:
plot_list <- lapply(X = 1:nrow(states6), FUN = graph)
将它们网格在一起:
g <- cowplot::plot_grid(plotlist = plot_list, ncol = 3)
g
这篇关于在相同的空间比例下使用geom_sf制作的小型多张地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!