gGmap与geom_map叠加 [英] ggmap with geom_map superimposed

查看:229
本文介绍了gGmap与geom_map叠加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$($)
库(spdep)
库(ggplot2)
库(ggmap)
库(rgdal)

获取和提取数据:

  nc.sids<  -  readShapePoly(system.file(etc / shapes / sids.shp,package =spdep)[1],ID =FIPSNO,proj4string = CRS + proj = longlat + ellps = clrk66))
nc.sids = spTransform(nc.sids,CRS(+ init = epsg:4326))
pre>

从stamen.com获取背景地图,情节,看起来不错:

  ncmap = get_map(location = as.vector(bbox(nc.sids)),source =stamen,maptype =toner,zoom = 7)
ggmap(ncmap)

在地图上创建一个long,lat,Z和plot的数据框,并显示一个空白图:

  ncP = data.frame(coordinates(nc.sids),runif(nrow(nc.sids)))
colnames(ncP) = c(long,lat,Z)

ggmap(ncmap)+ geom_point(aes(x = long,y = lat,col = Z),data = ncP)
ggplot()+ geom_point(aes(x = long,y = lat,col = Z),data = ncP)

给它一些独特的ids称为id并加强(含维生素和铁?)

  nc.sids@data [,1] = 1:nrow(nc.sids)
名称(nc.sids)[1] =id
ncFort = fortify(nc.sids )

现在,我的地图和我的限制,我想绘制74出生率:

  myMap = geom_map(aes(fill = BIR74,map_id = id),map = ncFort,data = nc.sids @ data)
限制= expand_limits(x = ncFort $ long,y = ncFort $ lat)

空白图可以:

  ggplot()+ myMap +限制

但是在ggmap上我不能:

  ggmap ncmap)+ myMap + Limits 
#eval(expr,envir,enclosure)中的错误:没有找到对象'lon'

某些版本:

 > packageDescription(ggplot2)$ Version 
[1]0.9.0
> packageDescription(ggmap)$ Version
[1]2.0

我可以添加geom_polygon到ggplot或ggmap,它的工作原理。所以有一些事情与geom_map ....

解决方案

错误信息是,我认为继承问题的结果。通常情况下,在后续层中使用不同的数据帧时。



在ggplot2中,每个图层都继承了初始调用 ggplot 中全局设置的默认aes映射。例如, ggplot(data = data,aes(x = x,y = y))全局设置x和y映射,以便所有后续层都期望看到x和y在任何数据帧中分配给它们。如果x和y不存在,则会出现类似于您收到的错误信息。 有关类似的问题,请参阅此处范围的解决方案。



在你的情况下,这不是很明显,因为第一次调用是 ggmap - 你看不到映射也不是如何设置,因为 ggmap 都很好地包装起来。不过,在某个地方, ggamp 调用 ggplot ,所以默认的美学映射必须设置在初始调用中的某个位置 ggmap 。接下来是 ggmap ,后跟 geom_map ,而不考虑继承问题会导致错误。



所以,Kohske在以前的帖子中提出的建议适用于你需要在geom_map中使用不同的数据集时使其失效。不知道已经设置了什么或如何设置它们,通过将 inherit.aes = FALSE 添加到第二层可能是最简单的 - 调用 geom_map



请注意,您不会收到带有 ggplot()+ myMap + Limits 的错误消息,因为没有美观已在ggplot调用中设置。



接下来,我使用R版本2.15.0,ggplot2版本0.9.1和ggmap版本2.1。我几乎完全使用你的代码,除了在$ code> geom_map 的调用中添加 inherit.aes = FALSE 。一个小的变化允许 ggmap geom_map 叠加:



$($)
库(spdep)
库(ggplot2)
库(ggmap)
库(rgdal)

#获取和提取数据:
nc.sids< - readShapePoly(system.file(etc / shapes / sids.shp,package =spdep)[1],ID =FIPSNO,proj4string = CRS(+ proj = longlat + ellps = clrk66))
nc.sids = spTransform(nc.sids,CRS(+ init = epsg:4326))

#从stamen.com获取背景图,绘图,看起来不错:
ncmap = get_map(location = as.vector(bbox(nc.sids)),source =stamen,maptype =墨粉,zoom = 7)
ggmap(ncmap)

#创建一个数据帧,长,纬,Z和绘图在地图上,空白图:
ncP = data.frame(coordinates(nc.sids),runif(nrow(nc.sids)))
colnames(ncP)= c(long,lat,Z)

ggmap(ncmap)+ geom_point(aes(x = long,y = lat,col = Z),data = ncP)
ggplot()+ geom_point(aes(x = long,y = = Z),data = ncP)

#give它有一些独特的ids calle d'id'并加强(含维生素和铁?)
nc.sids@data [,1] = 1:nrow(nc.sids)
名称(nc.sids)[1] = id
ncFort = fortify(nc.sids)

#现在,我的地图和我的限制,我想绘制出生率:
myMap = geom_map(inherit。 aes = FALSE,aes(fill = BIR74,map_id = id),map = ncFort,data = nc.sids @ data)
Limits = expand_limits(x = ncFort $ long,y = ncFort $ lat)

#和一个空白的图我可以:
ggplot()+ myMap +限制

#但在一个ggmap我不能:
ggmap(ncmap)+ myMap + Limits

最后一行代码的结果是:




library(sp)
library(spdep)
library(ggplot2)
library(ggmap)
library(rgdal)

Get and fiddle with data:

nc.sids <- readShapePoly(system.file("etc/shapes/sids.shp", package="spdep")[1],ID="FIPSNO", proj4string=CRS("+proj=longlat +ellps=clrk66"))
nc.sids=spTransform(nc.sids,CRS("+init=epsg:4326"))

Get background map from stamen.com, plot, looks nice:

ncmap = get_map(location=as.vector(bbox(nc.sids)),source="stamen",maptype="toner",zoom=7)
ggmap(ncmap)

Create a data frame with long,lat,Z, and plot over the map and a blank plot:

ncP = data.frame(coordinates(nc.sids),runif(nrow(nc.sids)))
colnames(ncP)=c("long","lat","Z")

ggmap(ncmap)+geom_point(aes(x=long,y=lat,col=Z),data=ncP)
ggplot()+geom_point(aes(x=long,y=lat,col=Z),data=ncP)

give it some unique ids called 'id' and fortify (with vitamins and iron?)

nc.sids@data[,1]=1:nrow(nc.sids)
names(nc.sids)[1]="id"
ncFort = fortify(nc.sids)

Now, my map and my limits, I want to plot the 74 birth rate:

myMap = geom_map(aes(fill=BIR74,map_id=id),map=ncFort,data=nc.sids@data)
Limits = expand_limits(x=ncFort$long,y=ncFort$lat)

and on a blank plot I can:

ggplot() + myMap + Limits

but on a ggmap I can't:

ggmap(ncmap) + myMap + Limits
# Error in eval(expr, envir, enclos) : object 'lon' not found

Some versions:

> packageDescription("ggplot2")$Version
[1] "0.9.0"
> packageDescription("ggmap")$Version
[1] "2.0"

I can add geom_polygon to ggplot or ggmap and it works as expected. So something is up with geom_map....

解决方案

The error message is, I think, the result of an inheritance issue. Typically, it comes about when different data frames are used in subsequent layers.

In ggplot2, every layer inherits default aes mappings set globally in the initial call to ggplot. For instance, ggplot(data = data, aes(x = x, y = y)) sets x and y mappings globally so that all subsequent layers expect to see x and y in whatever data frame has been assigned to them. If x and y are not present, an error message similar to the one you got results. See here for a similar problem and a range of solutions.

In your case, it's not obvious because the first call is to ggmap - you can't see the mappings nor how they are set because ggmap is all nicely wrapped up. Nevertheless, ggamp calls ggplot somewhere, and so default aesthetic mappings must have been set somewhere in the initial call to ggmap. It follows then that ggmap followed by geom_map without taking account of inheritance issues results in the error.

So, Kohske's advice in the earlier post applies - "you need to nullify the lon aes in geom_map when you use a different dataset". Without knowing too much about what has been set or how they've been set, it's probably simplest to globber the lot by adding inherit.aes = FALSE to the second layer - the call to geom_map.

Note that you don't get the error message with ggplot() + myMap + Limits because no aesthetics have been set in the ggplot call.

In what follows, I'm using R version 2.15.0, ggplot2 version 0.9.1, and ggmap version 2.1. I use your code almost exactly, except for the addition of inherit.aes = FALSE in the call to geom_map. That one small change allows ggmap and geom_map to be superimposed:

library(sp)
library(spdep)
library(ggplot2)
library(ggmap)
library(rgdal)

#Get and fiddle with data:
nc.sids <- readShapePoly(system.file("etc/shapes/sids.shp", package="spdep")[1],ID="FIPSNO", proj4string=CRS("+proj=longlat +ellps=clrk66"))
nc.sids=spTransform(nc.sids,CRS("+init=epsg:4326"))

#Get background map from stamen.com, plot, looks nice:
ncmap = get_map(location=as.vector(bbox(nc.sids)),source="stamen",maptype="toner",zoom=7)
ggmap(ncmap)

#Create a data frame with long,lat,Z, and plot over the map and a blank plot:
ncP = data.frame(coordinates(nc.sids),runif(nrow(nc.sids)))
colnames(ncP)=c("long","lat","Z")

ggmap(ncmap)+geom_point(aes(x=long,y=lat,col=Z),data=ncP)
ggplot()+geom_point(aes(x=long,y=lat,col=Z),data=ncP)

#give it some unique ids called 'id' and fortify (with vitamins and iron?)
nc.sids@data[,1]=1:nrow(nc.sids)
names(nc.sids)[1]="id"
ncFort = fortify(nc.sids)

#Now, my map and my limits, I want to plot the 74 birth rate:
myMap = geom_map(inherit.aes = FALSE, aes(fill=BIR74,map_id=id), map=ncFort,data=nc.sids@data)
Limits = expand_limits(x=ncFort$long,y=ncFort$lat)

# and on a blank plot I can:
ggplot() + myMap + Limits

# but on a ggmap I cant:
ggmap(ncmap) + myMap + Limits 

The result from the last line of code is:

这篇关于gGmap与geom_map叠加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆