我使用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的缩放,并在雄蕊地图的限制取决于地图类型。 “自动”自动确定包围盒规格的变焦,并且默认为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()R版本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
编辑 :更新后GGPLOT2 v 0.9.3
我想类似的东西recenty但收效甚微。 不过,也有许多方法来市中心,距离世界地图map
包:看到这里 , 这里和这里 。 从后者使用代码,下面是集中在经度160的世界地图为例,绘制CRAN镜像地址(使用获得的坐标geocode()
在世界地图上使用GGPLOT2绘出了从该ggmap包功能),和颜色新西兰(使用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")
我最近得到了同样的错误,并将其归结为ggmap不顺心的纬度之外$ \ $下午80℃。
但是,我不得不单独下载我的形象,因为它是一个下载(使用OSM)过大; 这是不是你的问题,但是我记录它为未来的读者。
下面是我如何解决它:
- 墨卡托的单独下载通过投影图像BigMap
- 纬度需要一些护理:我告诉你随纬度的限制同样的错误之外$ \ $下午80℃时,我希望一切都应该罚款,直到85°OSM盖),但我没有跟踪下来,因为我反正不要“T需要非常高纬度地区。
- 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的内置coord_map。 这可以无需第三方瓦片的集合创建地图。 它非常适合简单的地图,可以使用所有这些都是ggplot的美。
http://docs.ggplot2.org/current/coord_map.html
我已成功地建立基于谷歌地图的世界地图。 这是不幸的是稍微扭曲,因为我相信ggmap /谷歌地图强加限制(数据长度= 409600个像素,尺寸是792的倍数)。 然而,以下的规模,范围和缩放参数的组合提供了谷歌的世界地图与ggmap。
Natrally,你可以改变lon
到longitudal对焦点切换到澳洲,如你所愿。
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(.)
注:地图上的点是从我自己的数据集,而不是由上面的代码生成,但世界地图为核心的重要性在这里。