tmap_arrange R markdown 中的输出质量

tmap_arrange output quality in R markdown

使用tmap_arrange时有什么方法可以控制输出分辨率吗?

示例使用来自 cbs api 的荷兰地区的形状文件并分配人口数据(为简单起见,随机分配)。每张地图本身的质量都很好。但是,如果我使用 tmap_arrange 它的分辨率要低得多,我正在努力寻找改进它的方法。

如果我 tmap_save 作为 png,我可以指定分辨率并重新导入以改善问题,但它增加了很多步骤。

示例是 R markdown 脚本。

```
---
title: "tmaps of Netherlands regions"
date: "`r Sys.Date()`"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(warning = FALSE, message = FALSE, error=FALSE) 

library(knitr)
library(markdown)
library(cbsodataR)
library(tidyverse)
library(httr)
library(sf)
library(tmap)

## download geometry

list_of_geos <- c("landsdeel", "provincie", "coropgebied", "gemeente")

## choose year and geometry

for (geo_nam in list_of_geos){
  ## define a year
  year <- "2021"
  ## define url
  url <- parse_url("https://geodata.nationaalgeoregister.nl/cbsgebiedsindelingen/wfs")
  url$query <- list(service = "WFS",
                    version = "2.0.0",
                    request = "GetFeature",
                    typename = paste0("cbsgebiedsindelingen:cbs_", geo_nam, "_", year, "_gegeneraliseerd"),
                    outputFormat = "application/json")
  request <- build_url(url)
  
## import shapes
  geo_sf <- st_read(request, quiet = TRUE)
  ## assign environment name
  assign(geo_nam, geo_sf)
  
}

## define a palette
me_pal <- c("#0000b3", "#0000eb", "#1d00ff", "#4a00ff", "#7600ff", "#a211ee", "#cf2ed1", "#fb4ab5", 
            "#ff6798", "#ff837c", "#ff9f60", "#ffbc43", "#ffd827", "#fff50a")

## make up population data
landsdeel$population <- as.numeric(sample(1000000:9000000, size = nrow(landsdeel)))

## define breaks
bks <- seq(1000000,9000000, length.out = 25)

## generate tmap
tm1 <- tm_shape(landsdeel) +
  tm_polygons('population', palette = me_pal, border.col = 'black', breaks = bks, title = "Population Landsdeel")+
  tm_layout(legend.outside = TRUE, frame = FALSE, legend.width = 2)+
  tm_text("statnaam", size = 1/4) + tm_layout(frame = FALSE)

tm1

coropgebied$population <- as.numeric(sample(0:1000000, size = nrow(coropgebied)))

bks <- seq(0,1000000, length.out = 25)

tm2 <- tm_shape(coropgebied) +
  tm_polygons('population', palette = me_pal, border.col = 'black', breaks = bks, title = "Population Coropgebied")+
  tm_layout(legend.outside = TRUE, frame = FALSE, legend.width = 2)

tm2

provincie$population <- as.numeric(sample(100000:4000000, size = nrow(provincie)))

bks <- seq(100000,4000000, length.out = 25)

tm3 <- tm_shape(provincie) +
  tm_polygons('population', palette = me_pal, border.col = 'black', breaks = bks, title = "Population Provincie")+
  tm_layout(legend.outside = TRUE, frame = FALSE, legend.width = 2)+
  tm_text("statnaam", size = 1/4) + tm_layout(frame = FALSE)

tm3

gemeente$population <- as.numeric(sample(0:900000, size = nrow(gemeente)))

bks <- seq(0,900000, length.out = 25)

tm4 <- tm_shape(gemeente) +
  tm_polygons('population', palette = me_pal, border.col = 'black', breaks = bks, title = "Population Gemeente")+
  tm_layout(legend.outside = TRUE, frame = FALSE, legend.width = 2)

tm4

## arrange 2 by 2
tmap_arrange(tm1, tm2, tm3, tm4, ncol = 2, nrow = 2)

```

只需将您排列好的地图放入一个新变量中并通过tmap_save保存:

tm_all <- tmap_arrange(tm1, tm2, tm3, tm4, ncol = 2, nrow = 2)
tmap_save(tm_all, "test_map.png", dpi = 600)

或直接:

tmap_arrange(tm1, tm2, tm3, tm4, ncol = 2, nrow = 2) %>% tmap_save("test_map.png", dpi = 600)

分辨率通过 dpi 设置。

在你的 markdown 文档中你还可以调整相应块的分辨率,例如通过 {r setup, include=FALSE, dpi = 600}.