我想從tmap包中使用tm_shape()和tm_layout()在一個頁面中使用grid-package中的grid.layout()繪製多個映射。我想繪製只有一個共同的圖例這裏顯示所有地圖,類似的例子:用一個常見的圖例繪製類tmap的多個對象
ggplot separate legend and plot
不幸的是,TMAP不提供ggplot對象。有人知道如何做tmaps相同嗎?這裏是一個重複的例子:
data(World, rivers, metro)
# creating two separate maps
africa <- World[[email protected]$continent=='Africa',]
asia <- World[[email protected]$continent=='Asia',]
my.breaks <- seq(0,80,20)
africa.map <- tm_shape(africa) +
tm_fill("HPI",style = 'fixed',breaks = my.breaks) +
tm_layout(bg.color = "white", legend.text.size = 1.3, legend.width = 0.6,
legend.outside=TRUE, legend.outside.position = 'top',
legend.outside.size = .1, legend.position = c(0.8, 0.2))
asia.map <- tm_shape(asia) +
tm_fill("HPI",style = 'fixed',breaks = my.breaks) +
tm_layout(bg.color = "white", legend.text.size = 1.3, legend.width = 0.6,
legend.outside=TRUE, legend.outside.position = 'top',
legend.outside.size = .1, legend.position = c(0.8, 0.2))
page.layout <- grid.layout(nrow = 8, ncol = 5,
widths = unit(c(1), "null"),
heights = unit(c(1), "null"),
default.units = "null",
respect = FALSE,
just = "centre")
grid.newpage()
pushViewport(viewport(layout = page.layout))
grid.text(paste('Happy Planet Index'),
vp = viewport(layout.pos.row = 1, layout.pos.col = 1:5),gp=gpar(fontsize=20))
grid.text('Africa', vp = viewport(layout.pos.row = 2, layout.pos.col = 1:2),gp=gpar(fontsize=20))
print(africa.map, vp=viewport(layout.pos.row = 3:6, layout.pos.col = 1:2))
grid.text('Asia', vp = viewport(layout.pos.row = 2, layout.pos.col = 3:5),gp=gpar(fontsize=20))
print(asia.map, vp=viewport(layout.pos.row = 3:6, layout.pos.col = 3:5))
最佳, 埃裏希
這絕對有可能。你能發佈一個你想實現的可重複的例子嗎? –
謝謝Martijn!我添加了一個例子。我想要做的是隻繪製兩個地圖的一個常見圖例,最好將圖例與地圖分開,並在繪圖區域的任何位置單獨繪製,如ggplot2示例中所示。但如果你有更好的解決方案,我很高興。 – Erich