R语言tmap包中如何增大色棒(colourbar)尺寸?
解决tmap中色棒(colourbar)尺寸过小的问题
针对你的需求,调整色棒尺寸需要在对应图层函数(tm_dots)中设置专属参数,而非仅依赖tm_layout。以下是具体修改方案:
关键修改逻辑
- 色棒作为连续型颜色图例,其尺寸需在图层函数(
tm_dots)中通过legend.width和legend.height参数直接控制,这两个参数的取值是相对于绘图区域的比例(0-1之间) - 可配合
tm_legend的text.size参数调整色棒旁的刻度文字大小,保证视觉协调
修改后的完整代码
# read in london boundary data boros <- st_read('boros.geojson') %>% st_transform(., 27700) # read in road data edges <- st_read('london_all.gpkg', layer = "edges") %>% st_transform(., 27700) # entropy data london_entropy_iso <- st_read('london_entropy_iso_500.geojson') %>% st_transform(27700) #plot the entropy in London london_entropy_map <- tm_shape(boros) + tm_polygons(col = NA, alpha = 0.5) + tm_shape(edges) + tm_lines(col = "black", size = 0.5) + tm_shape(london_entropy_iso) + tm_dots(col = 'entropy', palette = 'viridis', size = 0.02, style = 'cont', title = 'Entropy', # 新增:控制色棒的宽高比例 legend.width = 0.2, legend.height = 1) + tm_compass(type = "arrow", position = c('left', 'bottom'), size = 2) + tm_scale_bar(text.size = 0.9, position = c("left", "bottom")) + tm_layout(legend.position = c("right", "bottom"), main.title= "Amenity Diversity Entropy within a 500m Isodistance", main.title.position = c('center', 'top'), main.title.size = 3, frame = FALSE, # Remove the box surrounding the map inner.margins = 0.1) # 调整图例标题和刻度文字大小 london_entropy_map <- london_entropy_map + tm_legend(title.size = 1.5, text.size = 1) # save map tmap_save(london_entropy_map, filename = "london_entropy_iso_500_map.png", width = 16, height = 20, dpi = 300)
补充说明
- 你可以根据输出效果灵活调整
legend.width和legend.height的数值,比如增大legend.width让色棒更宽,或调整legend.height改变色棒长度 tm_layout中的legend.width/height参数控制的是整个图例容器的大小,而非色棒本身,这也是你之前设置无效的原因
内容的提问来源于stack exchange,提问作者Phil
相关产品推荐
相关产品推荐

