ggplot2和maps:geom_point和annotation_raster位置不匹配 [英] ggplot2 and maps: geom_point and annotation_raster position mismatch

查看:206
本文介绍了ggplot2和maps:geom_point和annotation_raster位置不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



使用下面的代码,我可以使用 ggmap 从Google成功检索光栅,使用 ggplot2 绘制 annotation_raster ,并将网站地点绘制为栅格图层顶部的红点。在情节上,职位不太匹配(他们应该跟随海岸线)。我知道我的网站的职位是正确的,因为当我将数据作为KML文件上传到Google地球时,他们会绘制哪些地方。



建议将不胜感激。 p>

此代码将以...运行。请注意,您需要开发版本的 ggplot2 ,可在github上使用。要安装:

 #install.packages(devtools)
库(devtools)
install_github ggplot2)

代码:




库(ggmap)
库(grDevices)
theme_set(theme_bw())

#点的坐标,一些积:
siteLat = C(-22.94414,-22.67119,-29.25241,-30.31181,-32.80670,-33.01054,-32.75833, - 33.36068,-31.81708,-32.09185,-32.31667 - 34.13667,-34.05016,-33.91847,-34.13525,-34.12811,-34.10399,-34.16342,-34.41459,-34.58786,-34.83353,-34.37150,-34.40278,-34.17091,-34.08565,-34.04896,-33.98066,-34.02448, -34.20667,-34.05889,-33.97362,-33.99125,-33.28611,-33.02407,-33.01798,-32.99316,-31.09704,-31.05000,-30.91622,-30.70735,-30.28722,-30.27389,-29.86476,-29.54501,-29.49660 ,-29.28056,-28.80467,-27.42472)
siteLon = C(14.50175,14.52134,16.86710,17.26951,17.88522,17.95063,18.02778,18.15731,18.23065, 18.30262,18.32222,18.32674,18.34971,18.38217,18.43592,18.45077,18.48364,18.85908,19.25493,19.33971,20.00439,21.43518,21.73972,22.12749,23.05532,23.37925,23.64567,23.89933,24.77944,25.58889,25.64724,25.67788,27.48889,27.91626, 27.92182,27.95036,30.18395,30.21666,30.32982,30.48474,30.76026,30.83556,31.04479,31.21662,31.24665,31.44403,32.07567,32.73333)
SITENAME = C(SEQ(1:长度(siteLon)))
网站< - as.data.frame(cbind(siteLat,siteLon,siteName))

#指定栅格的近似坐标:
lats = c(-35,-20)
lons = c(10,35)

SAMap< - GetMap.bbox(lons,lats,maptype =satellite)

#提取真实光栅:
lonr< - c(SAMap $ BBOX $ ll [2],SAMap $ BBOX $ ur [2])
latr< - c(SAMap $ BBOX $ ll [1] $ BBOX $ ur [1])$ ​​b
$ b#提取栅格填充数据:
h_raster< - as.raster(SAMap $ myTile)

#plot using annotation_raster :
g< - ggplot(sites,aes(siteLon,siteLat))
g + annotation_raster(h_raster,lonr [1],lonr [2],latr [1],latr [2])+
geom_point(aes(x = siteLon,y = siteLat),color =红色,data = sites)+
scale_x_continuous(limits = lonr)+
scale_y_continuous(limits = latr)

(对不起,我不能在这里发布图片)。

解决方案

由于David Kahle,问题已经解决了。看到这篇文章:



https://groups.google.com/forum/?hl=zh-CN&fromgroups#!topic/ggplot2/ABffHL3WTpY



AJ


Good day everyone,

Using the code below I can successfully retrieve a raster from Google using ggmap, plot an annotation_raster using ggplot2, and plot site localities as red dots on top of the raster layer. On the plot the positions don't quite match (they should follow the coastline). I know my sites' positions are correct because they plot where they should be when I upload the data onto Google Earth as a KML file.

Suggestions will be appreciated.

This code will run as is... Note that you need a development version of ggplot2, which is available on github. To install:

# install.packages("devtools")
library(devtools)
install_github("ggplot2")

and for the code:

library(ggplot2)
library(ggmap)
library(grDevices)
theme_set(theme_bw())

# Some coordinates of points to plot:
siteLat = c(-22.94414, -22.67119, -29.25241, -30.31181, -32.80670, -33.01054, -32.75833, -        33.36068, -31.81708, -32.09185, -32.31667, -34.13667, -34.05016, -33.91847, -34.13525, -34.12811, -34.10399, -34.16342, -34.41459, -34.58786, -34.83353, -34.37150, -34.40278, -34.17091, -34.08565, -34.04896, -33.98066, -34.02448, -34.20667, -34.05889, -33.97362, -33.99125, -33.28611, -33.02407, -33.01798, -32.99316, -31.09704, -31.05000, -30.91622, -30.70735, -30.28722, -30.27389, -29.86476, -29.54501, -29.49660, -29.28056, -28.80467, -27.42472)
siteLon = c(14.50175, 14.52134, 16.86710, 17.26951, 17.88522, 17.95063, 18.02778, 18.15731, 18.23065, 18.30262, 18.32222, 18.32674, 18.34971, 18.38217, 18.43592, 18.45077, 18.48364, 18.85908, 19.25493, 19.33971, 20.00439, 21.43518, 21.73972, 22.12749, 23.05532, 23.37925, 23.64567, 23.89933, 24.77944, 25.58889, 25.64724, 25.67788, 27.48889, 27.91626, 27.92182, 27.95036, 30.18395, 30.21666, 30.32982, 30.48474, 30.76026, 30.83556, 31.04479, 31.21662, 31.24665, 31.44403, 32.07567, 32.73333)
siteName = c(seq(1:length(siteLon)))
sites <- as.data.frame(cbind(siteLat, siteLon, siteName))

# specify raster's approximate coordinates:
lats = c(-35, -20)
lons = c(10, 35)

SAMap <- GetMap.bbox(lons, lats, maptype = "satellite")

# extract "real" coords of raster:
lonr <- c(SAMap$BBOX$ll[2], SAMap$BBOX$ur[2])
latr <- c(SAMap$BBOX$ll[1], SAMap$BBOX$ur[1])

# extract raster fill data:
h_raster <- as.raster(SAMap$myTile)

# plot using annotation_raster:
g <- ggplot(sites, aes(siteLon, siteLat))
g + annotation_raster(h_raster, lonr[1], lonr[2], latr[1], latr[2]) +
  geom_point(aes(x = siteLon, y = siteLat), colour = "red", data = sites) +
  scale_x_continuous(limits = lonr) +
  scale_y_continuous(limits = latr)

(Sorry, I cannot post an image as I am new here).

解决方案

Okay, the problem has been resolved thanks to David Kahle. See this post:

https://groups.google.com/forum/?hl=en&fromgroups#!topic/ggplot2/ABffHL3WTpY

AJ

这篇关于ggplot2和maps:geom_point和annotation_raster位置不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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