tmap facetmap 不适用于 tm_fill 但适用于 tm_symbols

tmap facetmap not working for tm_fill but does for tm_symbols

我正在尝试使用 tmap 为全球数据集制作一些交互式图表。文件 map.temp 属于 类 sfdata.frame。它有四列 - FPUvalueyeargeometry。我想为年份制作分面,这是一个具有三个唯一值的字符变量 - 2005、2050 和 2080。

head(map.temp) returns

Simple feature collection with 6 features and 3 fields
geometry type:  MULTIPOLYGON
dimension:      XY
bbox:           xmin: -179.1667 ymin: 39.66666 xmax: 179.8333 ymax: 71.41666
epsg (SRID):    4326
proj4string:    +proj=longlat +datum=WGS84 +no_defs
      FPU year                       geometry value
1 ALB_ALB 2005 MULTIPOLYGON (((20.08333 39...  2494
2 ALB_ALB 2050 MULTIPOLYGON (((20.08333 39...  2565
3 ALB_ALB 2080 MULTIPOLYGON (((20.08333 39...  1906
4 ALK_USA 2005 MULTIPOLYGON (((-179 51.166...    NA
5 ALK_USA 2050 MULTIPOLYGON (((-179 51.166...    NA
6 ALK_USA 2080 MULTIPOLYGON (((-179 51.166...    NA

下面的 tm_shape 代码有三行被注释掉; tm_polygonstm_filltm_symbols 各一个。如果这些未注释掉并且代码 运行 分别生成三个图表,每年一个。我希望 tm_fill 和 tm_polygons 用颜色填充 FPU 多边形,但它们只在 year = 2080 时这样做。如果 tm_symbols 行未注释,它会产生结果(大小为相对于价值)每年。 test.RDS 文件可用 here。我的主要问题是如何使用 tm_fill.

为 2005 年和 2050 年的方面着色
library(tmap)
map.temp <- readRDS("test.RDS")
title <- "Maize rainfed yield <br> (mt/ha)"
legend_title <- "(mt/ha)"
breaks <- c(1.0, 2139.2, 4277.5, 6415.8, 8554)
tmap_mode("view")
tm_shape(map.temp) + 
 #   tm_polygons(col = "value", breaks = breaks, title = legend_title) +
 #   tm_fill(col = "value", title = legend_title)  +
    tm_borders() +
 #   tm_symbols(col = "black", border.col = "black", size = "value", title.col = legend_title) +
    tm_facets(by = "year",  nrow = 3, free.coords = FALSE, sync = TRUE) + 
    tm_layout(title = title) +
    tm_view(view.legend.position  = c("left", "bottom"))

干得好。我不是tmap的行家,但是你试过把free.coords设置成TRUE而不是tm_facets中的FALSE吗?它对我有用。

我还认为 tm_borderstm_filltm_polygons 在这种情况下有些多余,所以我只保留了 tm_polygons

以下是我对您的代码所做的修改,应该可以解决您的问题:

tmap_mode("view")
tm_shape(map.temp) + 
  tm_polygons(col = "value", breaks = breaks, title = legend_title) +
  tm_symbols(col = "black", border.col = "black", size = "value", title.col = legend_title) +
  tm_facets(by = "year",  nrow = 3, free.coords = TRUE, sync = TRUE) + # set free.coords to TRUE
  tm_layout(title = title) +
  tm_view(view.legend.position  = c("left", "bottom"))