R 您如何在tmap中定位标题和图例?

R 您如何在tmap中定位标题和图例?,r,tmap,R,Tmap,我是编程新手,目前正在学习一门介绍性的空间分析课程,该课程使用R。下面的代码生成下面包含的TMAP。 如何将每个tmap的标题居中,并将图例放置在右上角,而不将其置于地图上方 非常感谢你的帮助 ga1 = tm_shape(a2georgia) + tm_polygons('PctBlack', style='quantile', breaks=c(4.98,11.75, 22.35,27.64, 32.55, 40.06, 48.18, 79.64),

我是编程新手,目前正在学习一门介绍性的空间分析课程,该课程使用R。下面的代码生成下面包含的TMAP。 如何将每个tmap的标题居中,并将图例放置在右上角,而不将其置于地图上方

非常感谢你的帮助

  ga1 = tm_shape(a2georgia) +
  tm_polygons('PctBlack', style='quantile', breaks=c(4.98,11.75, 22.35,27.64, 32.55, 40.06,   48.18, 79.64),
              n=8, palette=c('lightblue','khaki1', 'red3'), title='Quantiles(8)',
              border.col='grey27', alpha=.9) +
  tm_layout(legend.position = c("right", "top"), title= '% of Population of Black Race', title.position = c('right', 'top'))


ga_cartogram <- cartogram_cont(a2georgia, "PctBlack", itermax=5)

  ga2 = tm_shape(ga_cartogram) + 
  tm_polygons("PctBlack", style='quantile', breaks=c(4.98,11.75, 22.35,27.64, 32.55, 40.06, 48.18, 79.64), 
              n=8, palette=c('lightblue','khaki1', 'red3'), title='Quantiles(8)',
              border.col='grey27', alpha=.9) +
  tm_layout(legend.position = c("right", "top"), title= '% of Population of Black Race',  title.position = c('right', 'top'))


tmap_arrange(ga1,ga2)
ga1=tm_形(a2)+
tm_多边形('PctBlack',style='quantile',breaks=c(4.98,11.75,22.35,27.64,32.55,40.06,48.18,79.64),
n=8,调色板=c('lightblue','khaki1','red3'),title='分位数(8)',
border.col='grey27',alpha=.9)+
tm_布局(legend.position=c(“右”、“顶”),title=%的黑人人口,title.position=c(“右”、“顶”)

gau cartogram问题在于{tmap}在多边形的边界框内绘制图例和标题。要腾出更多空间,必须稍微扩展边界框

不久前,我写了一篇关于这个话题的博客文章,你可能想看看

由于您的示例并不完全可复制,因此我将在随{sf}一起提供的北卡罗来纳州形状文件上演示该技术,并因此广泛使用

library(sf)
library(tmap)

# NC counties - a shapefile shipped with the sf package
nc <- st_read(system.file("shape/nc.shp", package ="sf"))


# bad, bad map...
tm_shape(nc) + tm_polygons("NWBIR74", style='quantile', 
                           breaks=c(4.98,11.75, 22.35,27.64, 32.55, 40.06, 48.18, 79.64),
                           n=8, palette=c('lightblue','khaki1', 'red3'), 
                           title='Quantiles(8)',
                           border.col='grey27', alpha=.9) +
  tm_layout(legend.position = c("right", "top"), 
            title= '% of Population of Black Race', 
            title.position = c('right', 'top'))
库(sf)
图书馆(tmap)
#NC县-sf包附带的形状文件

nc文档似乎有
main.title.position
和可能的值
center
。你研究过了吗?试过了,图例的位置变得更高,但仍然略高于地图。主标题没有受到影响。太棒了,谢谢你的回复。我可以请你澄清一下吗?你是说需要一个sf几何体吗?bbox_new%#拿边界框。。。st#U as#U sfc()#。。。并使其成为sf多边形sf::st_as_sfc是必需的,因为tmap::tm_shape的bbox参数需要一个几何体,而sf::st_bbox返回一个向量;请查看{sf}文档以获得更详细的解释
# make some bbox magic
bbox_new <- st_bbox(nc) # current bounding box

xrange <- bbox_new$xmax - bbox_new$xmin # range of x values
yrange <- bbox_new$ymax - bbox_new$ymin # range of y values

# bbox_new[1] <- bbox_new[1] - (0.25 * xrange) # xmin - left
 bbox_new[3] <- bbox_new[3] + (0.25 * xrange) # xmax - right
# bbox_new[2] <- bbox_new[2] - (0.25 * yrange) # ymin - bottom
bbox_new[4] <- bbox_new[4] + (0.2 * yrange) # ymax - top

bbox_new <- bbox_new %>%  # take the bounding box ...
  st_as_sfc() # ... and make it a sf polygon

# looks better, does it?
tm_shape(nc, bbox = bbox_new) + tm_polygons("NWBIR74", style='quantile', 
                           breaks=c(4.98,11.75, 22.35,27.64, 32.55, 40.06, 48.18, 79.64),
                           n=8, palette=c('lightblue','khaki1', 'red3'), 
                           title='Quantiles(8)',
                           border.col='grey27', alpha=.9) +
  tm_layout(legend.position = c("right", "top"), 
            title= '% of Population of Black Race', 
            title.position = c('right', 'top'))