R 带ggmap的世界地图

R 带ggmap的世界地图,r,ggplot2,ggmap,R,Ggplot2,Ggmap,我正在使用ggmap,希望有一张以澳大利亚为中心的世界地图,我可以很容易地绘制地理编码点。与其他一些映射包相比,ggmap似乎更易于使用。然而,当我使用下面的代码打开地图时,会出现错误 gc <- geocode('australia') center <- as.numeric(gc) > map <- get_map(location = center, source="google", maptype="terrain", zoom=0) Error: zoom

我正在使用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

编辑:更新为ggplot2 v 0.9.3

最近我尝试过类似的方法,但收效甚微。但是,有许多方法可以从
map
包中居中放置世界地图:请参阅和。使用后者的代码,这里的示例将世界地图居中于经度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")
库(地图)
图书馆(plyr)
图书馆(GG2)
图书馆(sp)
图书馆(ggmap)
#获取一些点以绘制CRAN镜像
镜像=getCRANmirrors(全部=FALSE,局部.only=FALSE)
镜像$Place=粘贴(镜像$City,“,”,镜像$Country,sep=”“)#耐心等待
tmp=地理编码(镜像$Place)
镜像=cbind(镜像,tmp)
###################################################################################################
#在经度160上重新居中世界地图(并镜像坐标)
###克劳迪娅·恩格尔的代码2012年3月19日,www.stanford.edu/~cengel/blog
###重新居中####

中心我最近也犯了同样的错误,这归结为ggmap不喜欢$pm$80°以外的纬度

然而,我不得不单独下载我的图片,因为它太大,无法下载(使用OSM);这不是你的问题,但我会为未来的读者记录下来

我是这样解决的:

  • 通过单独下载墨卡托投影图像
  • 纬度需要注意:我得到了与您显示的相同的错误,纬度限制在$\pm$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)))
require(“ggmap”)
图书馆(“png”)

缩放查看ggplot的内置坐标图。这可以创建地图,而不需要第三方的分幅集。对于简单的地图来说,它非常棒,可以利用所有的美


我已经成功地基于谷歌地图构建了世界地图。不幸的是,这有点失真,因为我认为ggmap/googlemaps施加了限制(数据长度=409600像素,维度是792的倍数)。尽管如此,以下大小、比例和缩放参数的组合提供了带有ggmap的Google world map

当然,你可以改变
lon
来改变澳大利亚的纵向焦点,如你所愿

library(tidyverse)

your_gmaps_API_key <- ""

get_googlemap(center = c(lon = 0, lat = 0)
          , zoom = 1
          , maptype="roadmap"
          , size = c(512,396) 
          , scale = 2
          , color = "bw"
          , key = your_gmaps_API_key) %>% ggmap(.)
库(tidyverse)
您的\u gmaps\u API\u密钥%ggmap(.)


注意:地图上的点来自我自己的数据集,不是由上面的代码生成的,但是世界地图在这里非常重要。

谢谢。我知道你提到的其他方法,但我希望有一种使用ggmap的方法,因为这样我可以很容易地绘制点!你还没有接受答案,所以你找到了不同的解决方案吗?@maj我没有找到使用ggmap绘制世界地图的解决方案。下面的答案很好,但它们需要从其他地方绘制世界地图,然后在上面进行绘图。如链接中所述,坐标图(方向=c(长、宽、旋转))应该解决这个重新定心的问题,但它没有。默认方向为c(90,0180),将其更改为c(90160180)可以移动中心,但也会使地图显示出错。似乎也没有任何解决办法。@sandy发布的是ggplot2中唯一的解决方案(我可以找到)。
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)))
library(tidyverse)

your_gmaps_API_key <- ""

get_googlemap(center = c(lon = 0, lat = 0)
          , zoom = 1
          , maptype="roadmap"
          , size = c(512,396) 
          , scale = 2
          , color = "bw"
          , key = your_gmaps_API_key) %>% ggmap(.)