2012-06-26 141 views
23

我正在使用ggmap,並希望有一個以澳大利亞爲中心的世界地圖,我可以輕鬆地繪製地理編碼點。與其他一些映射包相比,ggmap似乎更容易使用。然而,當我通過一個使用下面的代碼錯誤的地圖。ggmap的世界地圖

gc <- geocode('australia') 
center <- as.numeric(gc) 
> map <- get_map(location = center, source="google", maptype="terrain", zoom=0) 
Error: zoom must be a whole number between 1 and 21 

從get_map幫助: 「縮放:地圖縮放,從0(全世界)至21(建築)的整數,默認值爲10(市)開放街道地圖限制18的縮放和花蕊地圖上的限制取決於maptype,'auto'會自動確定邊界框規格的縮放比例,默認爲中心/縮放比例爲10。

變焦更改爲一個沒有錯誤get_map但確實爲繪製該地圖

map <- get_map(location = center, source="google", maptype="terrain", zoom=1) 
ggmap(map) 

Warning messages: 
1: In min(x) : no non-missing arguments to min; returning Inf 
2: In max(x) : no non-missing arguments to max; returning -Inf 
3: In min(x) : no non-missing arguments to min; returning Inf 
4: In max(x) : no non-missing arguments to max; returning -Inf 

它看起來像經度不被通過拉。 最後爲2的變焦它不工作,但不會通過地圖全世界

因此帶來,我的問題是如何使用get_map獲得世界地圖?

會話信息:

sessionInfo() ř版本2.15.0(2012-03-30) 平臺:I386-PC-的mingw32/I386(32位)

locale: 
[1] LC_COLLATE=English_United Kingdom.1252 
[2] LC_CTYPE=English_United Kingdom.1252 
[3] LC_MONETARY=English_United Kingdom.1252 
[4] LC_NUMERIC=C       
[5] LC_TIME=English_United Kingdom.1252  

attached base packages: 
[1] stats  graphics grDevices utils  datasets methods base  

other attached packages: 
[1] mapproj_1.1-8.3 maps_2.2-6  rgdal_0.7-12 sp_0.9-99  
[5] ggmap_2.1  ggplot2_0.9.1 

loaded via a namespace (and not attached): 
[1] colorspace_1.1-1 dichromat_1.2-4 digest_0.5.2  grid_2.15.0  
[5] labeling_0.1  lattice_0.20-6  MASS_7.3-17  memoise_0.1  
[9] munsell_0.3  plyr_1.7.1   png_0.1-4   proto_0.3-9.2  
[13] RColorBrewer_1.0-5 reshape2_1.2.1  RgoogleMaps_1.2.0 rjson_0.2.8  
[17] scales_0.2.1  stringr_0.6  tools_2.15.0 
+0

您尚未接受的答案,所以你有沒有發現一個不同的解決方案? – maj

+0

@maj我沒有發現任何解決方案使用ggmap轉危爲安低於世界map.The答案是偉大的,但他們需要從別的地方拉一個世界地圖,然後在其繪製。 – user1414259

回答

13

編輯:更新後GGPLOT2 v 0.9.3

我想類似的東西recenty但一點成功。但是,有多種方法可以將map包中的世界地圖居中:請參閱hereherehere。使用後者中的代碼,這裏是一個以世界地圖爲中心的示例,以160度爲中心,繪製使用ggplot2繪製的世界地圖上的CRAN鏡像位置(使用ggmap包中的geocode()函數獲得的座標),以及新西蘭顏色(使用geom_polygon) 。將地圖定位在經度160上可以使地圖左側的所有非洲地圖和地圖右側的格陵蘭島大部分地圖保持在地圖左側。

library(maps) 
library(plyr) 
library(ggplot2) 
library(sp) 
library(ggmap) 

# Get some points to plot - CRAN Mirrors 
Mirrors = getCRANmirrors(all = FALSE, local.only = FALSE) 

Mirrors$Place = paste(Mirrors$City, ", ", Mirrors$Country, sep = "") # Be patient 
tmp = geocode(Mirrors$Place) 
Mirrors = cbind(Mirrors, tmp) 

################################################################################################### 
# Recentre worldmap (and Mirrors coordinates) on longitude 160 
### Code by Claudia Engel March 19, 2012, www.stanford.edu/~cengel/blog 

### Recenter #### 
center <- 160 # positive values only 

# shift coordinates to recenter CRAN Mirrors 
Mirrors$long.recenter <- ifelse(Mirrors$lon < center - 180 , Mirrors$lon + 360, Mirrors$lon) 

# shift coordinates to recenter worldmap 
worldmap <- map_data ("world") 
worldmap$long.recenter <- ifelse(worldmap$long < center - 180 , worldmap$long + 360, worldmap$long) 

### Function to regroup split lines and polygons 
# Takes dataframe, column with long and unique group variable, returns df with added column named group.regroup 
RegroupElements <- function(df, longcol, idcol){ 
    g <- rep(1, length(df[,longcol])) 
    if (diff(range(df[,longcol])) > 300) { # check if longitude within group differs more than 300 deg, ie if element was split 
    d <- df[,longcol] > mean(range(df[,longcol])) # we use the mean to help us separate the extreme values 
    g[!d] <- 1 # some marker for parts that stay in place (we cheat here a little, as we do not take into account concave polygons) 
    g[d] <- 2 # parts that are moved 
    } 
    g <- paste(df[, idcol], g, sep=".") # attach to id to create unique group variable for the dataset 
    df$group.regroup <- g 
    df 
} 

### Function to close regrouped polygons 
# Takes dataframe, checks if 1st and last longitude value are the same, if not, inserts first as last and reassigns order variable 
ClosePolygons <- function(df, longcol, ordercol){ 
    if (df[1,longcol] != df[nrow(df),longcol]) { 
    tmp <- df[1,] 
    df <- rbind(df,tmp) 
    } 
    o <- c(1: nrow(df)) # rassign the order variable 
    df[,ordercol] <- o 
    df 
} 

# now regroup 
worldmap.rg <- ddply(worldmap, .(group), RegroupElements, "long.recenter", "group") 

# close polys 
worldmap.cp <- ddply(worldmap.rg, .(group.regroup), ClosePolygons, "long.recenter", "order") # use the new grouping var 
############################################################################# 

# Plot worldmap using data from worldmap.cp 
windows(9.2, 4) 
worldmap = ggplot(aes(x = long.recenter, y = lat), data = worldmap.cp) + 
    geom_polygon(aes(group = group.regroup), fill="#f9f9f9", colour = "grey65") + 
    scale_y_continuous(limits = c(-60, 85)) + 
    coord_equal() + theme_bw() + 
    theme(legend.position = "none", 
    panel.grid.major = element_blank(), 
    panel.grid.minor = element_blank(), 
    axis.title.x = element_blank(), 
    axis.title.y = element_blank(), 
    #axis.text.x = element_blank(), 
    axis.text.y = element_blank(), 
    axis.ticks = element_blank(), 
    panel.border = element_rect(colour = "black")) 

# Plot the CRAN Mirrors 
worldmap = worldmap + geom_point(data = Mirrors, aes(long.recenter, lat), 
    colour = "red", pch = 19, size = 3, alpha = .4) 

# Colour New Zealand 
# Take care of variable names in worldmap.cp 
head(worldmap.cp) 
worldmap + geom_polygon(data = subset(worldmap.cp, region == "New Zealand", select = c(long.recenter, lat, group.regroup)), 
      aes(x = long.recenter, y = lat, group = group.regroup), fill = "blue") 

enter image description here

+0

感謝您的。我知道,你剛纔提到的其他方式,但我希望有使用ggmap,因爲這樣我可以積點的方式輕鬆! – user1414259

9

我最近得到了同樣的錯誤,並將其歸結爲ggmap不順心的緯度之外$ \ $下午80℃。

但是,我不得不單獨下載我的圖像,因爲它太大而無法下載(使用OSM);這不是你的問題,但我爲未來的讀者記錄它。

這裏是我如何解決它:

  • 單獨墨卡託的下載VIA BigMap
  • 需要一些護理緯度投影圖像:我告訴你隨緯度的限制同樣的錯誤之外$ \下午$ 80 °,當我預計在85°OSM覆蓋之前一切都應該很好),但是我沒有追蹤它們,因爲我反正不需要非常高的緯度。
  • 0°/ 0°中心是對我的目的(我在歐洲:-)),但你肯定可以減少圖像等。無論是對你有好處,並通過cbind自己包裹。只要確保你知道你的切割的經度。
  • 然後設置你的形象
  • 的邊框並指定相應的類

這是我做的:

require ("ggmap") 
library ("png") 

zoom <- 2 
map <- readPNG (sprintf ("mapquest-world-%i.png", zoom)) 
map <- as.raster(apply(map, 2, rgb)) 

# cut map to what I really need 
pxymin <- LonLat2XY (-180,73,zoom+8)$Y # zoom + 8 gives pixels in the big map 
pxymax <- LonLat2XY (180,-60,zoom+8)$Y # this may or may not work with google 
             # zoom values 
map <- map [pxymin : pxymax,] 

# set bounding box 
attr(map, "bb") <- data.frame (ll.lat = XY2LonLat (0, pxymax + 1, zoom+8)$lat, 
            ll.lon = -180, 
            ur.lat = round (XY2LonLat (0, pxymin, zoom+8)$lat), 
            ur.lon = 180) 
class(map) <- c("ggmap", "raster") 

ggmap (map) + 
    geom_point (data = data.frame (lat = runif (10, min = -60 , max = 73), 
           lon = runif (10, min = -180, max = 180))) 

結果:
ggplot world map

編輯:我演有一點與你的谷歌地圖,但我沒有得到正確的緯度。 :-(

0

退房ggplot的內置coord_map,這可以無需第三方瓦片的集合創建地圖,它的簡單地圖巨大,可以使用所有的ggplot的美。

http://docs.ggplot2.org/current/coord_map.html

+0

如在鏈接所提到的,coord_map(取向= C(LON,LAT,旋轉))是假設爲解決這個重新定心的問題,但它沒有。取向的默認爲c(90,0,180),它改變到c(90160180)可以移動的中心,但它也螺絲了地圖顯示。似乎也沒有任何修復。什麼@sandy貼是唯一的解決辦法(我能找到)在GGPLOT2。 – woshishui