在tmap R的图例直方图中添加直方图轴标签

tcbh2hod  于 2023-04-27  发布在  其他
关注(0)|答案(1)|浏览(106)

我想在tmap中的图例直方图中添加轴标签。下面是一个示例代码:

library(tmap)
data(World)

tm_shape(World) +
  tm_polygons("gdp_cap_est", style = "quantile", palette = "RdYlBu",legend.hist=TRUE)

我希望得到这样的东西:

很抱歉,这似乎是一个很简单的问题,但我在网上找不到答案。

hjzp0vay

hjzp0vay1#

尽管tmap不太适合这样的事情,但这里有一个解决方案来完成你想要的:

library(tidyverse)
library(tmap)
library(sf)
data(World)

# create label for x axis
coords <- tibble(x = -100, y = -90, label = "GDP") %>% 
  st_as_sf(coords = c("x", "y"))

# create linestring with y axis label
ls <- tibble(geometry = st_sfc(st_linestring(rbind(c(-180, -90), c(-180, -20)))), 
             label = "Frequency") %>% st_as_sf()

tm_shape(World) +
  tm_polygons("gdp_cap_est",
              style = "quantile", 
              palette = "RdYlBu",
              legend.hist = TRUE) +
  tm_shape(coords) +
  tm_text(text = "label", size = 0.75) +
  tm_shape(ls) +
  tm_lines(col = "white") + # "hide" linestring
  # need to use along.lines = T here to rotate and align the label along the defined linestring
  tm_text(text = "label", size = 0.75, along.lines = T) +
  tm_layout(legend.position = c(0.04, 0.04))

相关问题