你如何在 tmap 中定位标题和图例?
How do you position the title and legend in 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),
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)
问题是 {tmap} 在多边形的边界框内绘制了图例和标题。要腾出更多空间,您必须稍微扩展边界框。
不久前我写了一篇关于这个主题的博客 post,你可能想看看那个 https://www.jla-data.net/eng/adjusting-bounding-box-of-a-tmap-map/
由于您的示例无法完全重现,我将在 {sf} 随附的北卡罗来纳州 shapefile 上演示该技术,因此可以广泛使用。
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'))
# 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'))
我是编程新手,目前正在参加一个使用 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)
问题是 {tmap} 在多边形的边界框内绘制了图例和标题。要腾出更多空间,您必须稍微扩展边界框。
不久前我写了一篇关于这个主题的博客 post,你可能想看看那个 https://www.jla-data.net/eng/adjusting-bounding-box-of-a-tmap-map/
由于您的示例无法完全重现,我将在 {sf} 随附的北卡罗来纳州 shapefile 上演示该技术,因此可以广泛使用。
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'))
# 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'))