2014-02-05 34 views
6

我想通過谷歌的灰色地圖繪製各個城市的數據點。由於這些城市距離彼此有些距離,我以爲我會用一個多面的情節。創建ggmap有點,facet和每個方面適當放大?

創建地圖非常簡單;請參閱下面的圖片和代碼。但是,每個方面都顯示相同的區域 - 在本例中爲大倫敦 - 其結果是其他城市的積分未顯示。

理想情況下,我想每個方面都顯示每個城市的相關點覆蓋。因此,'加的夫'這個方面會顯示卡迪夫的縮放地圖及其數據點,'伯明翰'會顯示伯明翰及其點數等。我試過改變各種參數,如zoomcenter,但我沒有成功。

如何顯示不同的城市和每個方面的相關點?

enter image description here

require(ggmap) 
require(reshape) 

# create fake data 
sites <- data.frame(site = 1:6, 
        name = c(
         "Royal Albert Hall", 
         "Tower of London", 
         "Wales Millenium Centre", 
         "Cardiff Bay Barrage", 
         "Birmingham Bullring", 
         "Birmingham New Street Station" 
         ), 
        coords = c(
         "51.501076,-0.177265", 
         "51.508075,-0.07605", 
         "51.465211,-3.163208", 
         "51.44609,-3.166652", 
         "52.477644,-1.894158", 
         "52.477487,-1.898836"), 
        subzone = rep(c('London','Cardiff','Birmingham'), each = 2) 
        ) 

# use function from reshape to split/add column 
sites = transform(sites, 
      new = colsplit(coords, split = ",", names = c('lat', 'lon'))) 
names(sites) <- c(names(sites)[1:4], 'lat','lon') 


ggmap(get_googlemap(center = "London", # omitting this doesn't help 
        scale = 2, 
        zoom = 11, # fiddling with zoom doesn't work 
        color = 'bw', 
        maptype = 'roadmap', 
        extent = 'panel', 
        format = "png8", 
        filename = "facet_map_test", 
        )) + 
    facet_wrap(~ subzone, ncol = 1) + 
    geom_point(data = sites, 
       aes(x = lon, y = lat), 
       fill = "red", 
       size = 3, 
       colour = "black", 
       shape = 21, 
       alpha = 1) + 
    theme(legend.position = "none") + 
    theme() 
+0

望着'ggmap()'代碼,我覺得這簡直是不支持:所有方面都會有共享相同的地圖。考慮創建三個獨立的地塊(即使用「facet_wrap」來獲取標題)並以某種方式組合它們。 – krlmlr

回答

6

使用gridExtra包可能是最好的一段路要走。代碼:

# getting the maps 
londonmap <- get_map(location = c(lon = -0.1266575, lat = 51.504575), zoom = 12) 
cardiffmap <- get_map(location = c(lon = -3.16493, lat = 51.45565), zoom = 13) 
birminghammap <- get_map(location = c(lon = -1.896497, lat = 52.477565), zoom = 14) 

# plotting the maps 
p1 <- ggmap(londonmap) + 
    geom_point(data = sites[sites$subzone=="London",], 
      aes(x = lon, y = lat, fill = "red", alpha = 0.8, size = 3), 
      shape = 21) + 
    ggtitle("London") + 
    theme(axis.title = element_blank(), legend.position = "none", plot.margin = unit(c(0,0,0,0), "lines")) 

p2 <- ggmap(cardiffmap) + 
    geom_point(data = sites[sites$subzone=="Cardiff",], 
      aes(x = lon, y = lat, fill = "red", alpha = 0.8, size = 3), 
      shape = 21) + 
    ggtitle("Cardiff") + 
    theme(axis.title = element_blank(), legend.position = "none", plot.margin = unit(c(0,0,0,0), "lines")) 

p3 <- ggmap(birminghammap) + 
    geom_point(data = sites[sites$subzone=="Birmingham",], 
      aes(x = lon, y = lat, fill = "red", alpha = 0.8, size = 3), 
      shape = 21) + 
    ggtitle("Birmingham") + 
    theme(axis.title = element_blank(), legend.position = "none", plot.margin = unit(c(0,0,0,0), "lines")) 

# grouping the plots together in one plot  
grid.arrange(p1,p2,p3,ncol=1) 

結果:

enter image description here

+0

非常感謝,+1抽出時間做到這一點。我是'grid.arrange'的長期用戶,並且基於上述問題已經有了一個解決方案。然而,爲了這個問題的目的,我特別有興趣確定是否可能(或不是)分面解決方案。 – SlowLearner

+0

我很確定一個方面的解決方案是不可能的。使用構面時,facetplots使用相同的背景。在你的情況下,這意味着相同的地圖被用作背景(如你在問題中的圖像中所示)。看一下'ggmap'包中的函數,我無法發現一個支持這種特殊用法的函數。因此,在我看來,只有一種情況是分面解決方案可行:當你需要在同一張地圖上繪製不同組的點時。 – Jaap

相關問題