2017-02-16 61 views
0

我想從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)) 

最佳, 埃裏希

+0

這絕對有可能。你能發佈一個你想實現的可重複的例子嗎? –

+0

謝謝Martijn!我添加了一個例子。我想要做的是隻繪製兩個地圖的一個常見圖例,最好將圖例與地圖分開,並在繪圖區域的任何位置單獨繪製,如ggplot2示例中所示。但如果你有更好的解決方案,我很高興。 – Erich

回答

1

我不知道這是你的意思:

data(World) 

tm_shape(World) + 
    tm_polygons(c("economy", "HPI")) + 
    tm_layout(legend.outside = TRUE) 

常見的傳說繪製地圖之外。由於兩個地圖都有不同的圖例,因此只有第一張地圖的圖例被採用。

也有使用grid.layout的方法。在這種情況下,您需要在網格視口中打印tmap地圖(請參閱print.tmap),並在另一箇中打印圖例。

UPDATE:

只是要齊全:通常情況下,應該有辦法用方面做到這一點:

data(World) 
AfAs <- World[[email protected]$continent %in% c('Africa', 'Asia'),] 
tm_shape(AfAs) + 
    tm_fill("HPI",style = 'fixed',breaks = my.breaks) + 
    tm_facets(by = "continent", drop.units = TRUE, free.coords = TRUE) 

如果需要繪製2次TMAP通話,這是最方便的過程圖例作爲一個單獨的地圖,與legend.only=TRUE

africa.map <- tm_shape(africa) + 
    tm_fill("HPI",style = 'fixed',breaks = my.breaks) + 
    tm_layout(legend.show = FALSE) 

asia.map <- tm_shape(asia) + 
    tm_fill("HPI",style = 'fixed',breaks = my.breaks) + 
    tm_layout(legend.show = FALSE) 

legend.map <- tm_shape(africa) + 
    tm_fill("HPI",style = 'fixed',breaks = my.breaks) + 
    tm_layout(legend.only = TRUE) 

grid.newpage() 
page.layout <- grid.layout(nrow = 2, ncol = 2, widths=c(.4,.6), heights=c(.6,.4)) 
pushViewport(viewport(layout = page.layout)) 

print(africa.map, vp=viewport(layout.pos.row = 1:2, layout.pos.col = 1)) 
print(asia.map, vp=viewport(layout.pos.row = 1, layout.pos.col = 2)) 
print(legend.map, vp=viewport(layout.pos.row = 2, layout.pos.col = 2)) 

UPDATE 2

圖例可以用此代碼放大:

legend.map <- tm_shape(africa) + 
    tm_fill("HPI",style = 'fixed',breaks = my.breaks) + 
    tm_layout(legend.only = TRUE, design.mode=TRUE, scale=2, asp=0) 

design.mode=TRUE在設計地圖時非常方便。我必須將asp設置爲0,因爲它佔用了非洲的寬高比:這是一個錯誤,因爲當legend.only = TRUE時縱橫比應該跟隨設備/視口。這在devel版本中是固定的。

+0

非常感謝!這正是我正在尋找的。 – Erich

+0

但是現在你怎麼在世界上讓這個傳說更大?我已經嘗試了本書中的所有可用命令: – Erich

+0

請參閱更新二。 –