tmap布局问题:原生布局右侧空白过多与plot_grid组合时图例重叠的解决方案求助
tmap布局问题:原生布局右侧空白过多与plot_grid组合时图例重叠的解决方案求助
大家好,我正在尝试用tmap搭建一个标准化的地图布局,需求是左侧显示地图,右侧放置图例、可选的简短文本说明以及项目logo,但遇到了两个棘手的布局问题,希望能得到社区的帮助:
问题1:原生tmap布局右侧存在大量多余空白
我已经通过tmap的布局参数对齐了所有需要的组件(地图、图例、标题文本、logo、指北针、比例尺),但图例右侧始终有大片空白区域。开启tm_design_mode()后能看到这部分是浅绿色的外部单元格空间,我希望把右侧的所有元素(图例、文本、logo)整体右移,给地图区域分配更多的空间。
对应代码:
library(tmap) library(cowplot) library(grid) library(png) library(RCurl) # 准备数据 countries <- World[World$name=="Germany" | World$name=="Poland",] # 构建tmap对象 tm_c <- tm_shape(countries) + tm_fill("name", fill.scale=tm_scale(values="rainbow_pu_br"), fill.legend = tm_legend(title="countries", frame=F, text.size=0.5, title.size = 0.5, position = tm_pos_out(pos.h = 0, pos.v = 0.75))) + tm_compass(type="arrow", position=tm_pos_in("LEFT","TOP"), size=1) + tm_scalebar(breaks=c(0,50,100,200), position=tm_pos_in("RIGHT","BOTTOM")) + tm_title( text=c("data description:\nline a\nline b\nline c\nline d\n \n"), size=0.5, position = tm_pos_out(pos.h = 0, pos.v = 0.9)) + tm_title( text=c("Institute for Mapmaking\nLayout Section"), size=0.3, position=tm_pos_out(pos.h = 0, pos.v = 0.1)) + tm_logo("https://www.r-project.org/logo/Rlogo.png", position = tm_pos_out(pos.h = 0, pos.v = 0.16), height=1) + tm_layout(frame=F, asp=0, legend.outside.size=0.11, outer.margins = c(0,0,0,0), inner.margins = c(0.01,0.01,0.01,0.01)) # 绘图与保存 print(tm_c) tmap_save( tm=tm_c, filename="tm_c_blank.png", width=15, height=10, units="cm", dpi=300, asp=0, outer.margins = c(0,0,0,0) )
问题截图:


问题2:用cowplot::plot_grid组合元素时图例与地图重叠
为了更灵活地控制布局,我尝试用plot_grid将地图、文本说明、图例、logo拆分成独立组件后再组合,但出现了图例与地图重叠的问题。我希望图例能被放置在指定的浅蓝色矩形区域内,不与左侧的地图产生重叠。
对应代码:
# 构建各独立组件 map <- grobTree(rectGrob(gp=gpar(col="white")), tmap_grob(tm_shape(countries) + tm_fill("name", fill.scale=tm_scale(values="rainbow_pu_br"), fill.legend = tm_legend(title="countries", frame=F, text.size=0.5, title.size = 0.5, position = tm_pos_out(pos.h = 0, pos.v = 0.75))) + tm_compass(type="arrow", position=tm_pos_in("LEFT","TOP"), size=1) + tm_scalebar(breaks=c(0,50,100,200), position=tm_pos_in("RIGHT","BOTTOM")) + tm_layout(frame=F, asp=0, legend.outside.size=0.11, outer.margins = c(0,0,0,0), inner.margins = c(0.01,0.01,0.01,0.01), legend.show=F)) ) header <- grobTree(rectGrob(gp=gpar(fill="orange", col="white")), textGrob("line1\nline2\nline3\nline4", x=0.15, y=0.5, gp = gpar(col = "black", fontsize = 8))) r_logo <- readPNG(getURLContent("https://www.r-project.org/logo/Rlogo.png"), FALSE) credit <- grobTree(rectGrob(gp=gpar(fill="green", col="white")), rasterGrob(r_logo, x=0.25, y=0.9, width=0.3, just=c("center","top")), textGrob("Institute of Map making\nLayout Section", x=0.1, y=0.1, just=c("left","bottom"), gp = gpar(col = "black", fontsize = 8))) leg <- grobTree(rectGrob(gp=gpar(fill="lightblue", col="white")), tmap_grob(tm_c + tm_layout(text.size=0.8, legend.only= T))) # 组合右侧元素与最终布局 right_side <- plot_grid(header, leg, credit, ncol=1, rel_heights=c(1,2,1)) # 保存最终图 png("tm_c_grid.png", width = 15, height = 10, units="cm", res=300, pointsize=10) plot_grid(map, right_side, ncol=2, rel_widths=c(3, 1), scale=0.97) dev.off()
问题截图:

求助需求
我想知道:
- 如何调整
tmap原生布局的参数,消除图例右侧的多余空白,让右侧元素整体右移,给地图更多空间? - 如果用
cowplot::plot_grid的方式组合组件,需要怎么调整参数才能避免图例与地图重叠,实现紧凑的左图右元素的布局?
感谢大家的帮助!
内容来源于stack exchange
相关产品推荐
相关产品推荐

