使用视口对齐晶格和tmap输出

时间:2018-01-16 16:47:56

标签: r r-grid tmap

我正在尝试对齐几个面板(2个贴图)和包装结构中的晶格输出,并使用网格显示一些解释性文本:

enter image description here

但是,我希望网格输出的左边距与左上角地图的左边缘对齐,文本与顶部地图的左边距对齐。

理想情况下,我想得到这个:

enter image description here

我使用的代码是:

library(tmap)
library(tmaptools)
library(openair)
library(grid)
library(gridExtra)


london_map <- qtm(read_osm("London"))
london_terrain <- qtm(read_osm("London", type = "stamen-terrain"))

pp_plot <- polarPlot(mydata, type="season")
top.vp <- 
  viewport(layout=grid.layout(3, 11,
                              heights=unit(c(0.5, 3.5 ,4), "inches"),
                              widths=unit(c(1,1,1,1,1,1,1,1,1,1), "inches")))


png(filename = "test.png", width = 11, height = 8, units ="in", res=72)

grid.newpage()
pushViewport(top.vp)
print(london_map, vp = viewport(layout.pos.row = 2, layout.pos.col=1:5, just="left"))
print(london_terrain, vp = viewport(layout.pos.row = 2, layout.pos.col=6:11, just="left"))

pushViewport(viewport(layout.pos.row = 3, layout.pos.col=1:7))
plot(pp_plot$plot, newpage=FALSE)
popViewport()     
pushViewport(viewport(layout.pos.row = 3, layout.pos.col=8))
grid.text("Notes: \n1. quo durius te exerces, eo facilius certabis\n2. Ursis fidimus\n3. audaces fortuna juvat \n", just=0)

dev.off()

我已经厌倦了正义的论点,但无济于事。

1 个答案:

答案 0 :(得分:0)

基于以上所述,这实现了我所需要的。我需要使用负面立场:

png(filename = "test.png", width = 11, height = 8, units ="in", res=72)

grid.newpage()
pushViewport(top.vp)
print(london_map, vp = viewport(layout.pos.row = 2, layout.pos.col=1:5, just=c("left","top")))
print(london_terrain, vp = viewport(layout.pos.row = 2, layout.pos.col=6:11, just="left"))

pushViewport(viewport(layout.pos.row = 3, layout.pos.col=1:7))
plot(pp_plot$plot, newpage=FALSE, position=c(-0.18,0,0.82,1))
popViewport()     
pushViewport(viewport(layout.pos.row = 3, layout.pos.col=6))
grid.text("Notes: \n1. quo durius te exerces, eo facilius certabis\n2. Ursis fidimus\n3. audaces fortuna juvat \n", 
          x = 0.85, just = "left")

dev.off()