1

先上代码:

library(tmap)
library(tmaptools)

data("Europe")

# subset just three countries for illustration
threeC <- Europe[Europe$sovereignt %in% c("Germany", "Poland", "Ukraine"), ]

tm_shape(threeC) +
  tm_polygons() +
  tm_shape(threeC) +
  tm_borders() +
  tm_fill("pop_est",
          palette = get_brewer_pal("YlGnBu"),
          legend.show = F,
          style = "order") +
  tm_text("sovereignt", size = .8) +
  tm_shape(threeC) +
  tm_text("pop_est", size = .8, auto.placement = .1)

结果是: 3 个带有文字标签的国家

问题 1:是否可以通过使用 tm_text() 函数中的某些公式(即,不在数据框,然后使用它而不是“pop_est”)?

问题 2:显示国家名称的文本颜色是黑色或白色(这里“德国”以白色打印,其他两个国家名称以黑色打印)取决于填充颜色,这极大地有助于在大地图中的可见性。但显示“pop_est”数字的附加文本的颜色始终为黑色。如何根据填充颜色使其变为黑色或白色以提高可见度?

4

1 回答 1

0

答案 1:经过一些实验,如果不事先创建自定义列,似乎没有办法做到这一点:

threeC$pop_est.m <- (threeC$pop_est/1e6) %>% sprintf("%.2f", .) %>% paste0("m")

答案 2:使用tm_fill具有所有先前选项的另一个功能,并通过 .addtionally 使其透明alpha = 0

因此,修改后的代码将是:

library(tmap)
library(tmaptools)

data("Europe")

# subset just three countries for illustration
threeC <- Europe[Europe$sovereignt %in% c("Germany", "Poland", "Ukraine"), ]
threeC$pop_est.m <- (threeC$pop_est/1e6) %>% sprintf("%.2f", .) %>% paste0("m")

tm_shape(threeC) +
  tm_polygons() +
  tm_shape(threeC) +
  tm_borders() +
  tm_fill("pop_est",
          palette = get_brewer_pal("YlGnBu"),
          legend.show = F,
          style = "order") +
  tm_text("sovereignt", size = .8) +
  tm_shape(threeC) +
  tm_fill("pop_est",
          palette = get_brewer_pal("YlGnBu"),
          legend.show = F,
          style = "order",
          alpha = 0) + # make it transparent
  tm_text("pop_est.m", size = .8, auto.placement = .1)

这将给出所需的结果: 自定义地图

于 2017-10-01T05:33:25.333 回答