在用ggplot2制作的地图上绘制大圆圈

时间:2015-11-24 20:06:39

标签: r ggplot2

我正在尝试添加一系列大圆圈(使用gcIntermediate from library(geosphere))到使用此tutorial制作的地图。

以下是作者提出的代码(经过修改以使问题更清晰):

library(raster)
library(rgdal)
library(ggplot2)
library(reshape2)
library(plyr)
library(geosphere)

#  is all downloaded from [http://www.naturalearthdata.com/downloads/][2]

nat.earth <- stack('./Maps/NE2_50M_SR_W/NE2_50M_SR_W.tif')

domain <- c(113, 154, -44, -11)

nat.crop <- crop(nat.earth, y=extent(domain))

rast.table <- data.frame(xyFromCell(nat.crop, 1:ncell(nat.crop)),
                         getValues(nat.crop/255))

rast.table$rgb <- with(rast.table, rgb(NE2_50M_SR_W.1,
                                       NE2_50M_SR_W.2,
                                       NE2_50M_SR_W.3,
                                      1))
# et voila!

ggplot(data = rast.table, aes(x = x, y = y)) +
  geom_tile(fill = rast.table$rgb) +
  scale_alpha_discrete(range=c(1,0)) +
  scale_x_continuous(expand=c(0,0)) +
  scale_y_continuous(expand=c(0,0)) +
  xlab('') + ylab('')

给出这个: enter image description here

现在,我想在这张地图上加上由它们的纬度坐标定义的一串线,如下:

edges <- read.csv("./csvLines.csv", header=TRUE)

for (j in 1:length(edges[,1])) {
    inter <- gcIntermediate(c(edges[j,]$long1, edges[j,]$lat1), c(edges[j,]$long2, edges[j,]$lat2), n=100, addStartEnd=TRUE)      
    lines(inter, col= colors[colindex], alpha = 1, lwd=1.5)
}

csv文件如下所示:

head(edges)

        lat1    long1      lat2    long2           w
13 -34.92743 138.5999 -33.03811 137.5801 0.007407407
14 -34.92743 138.5999 -33.91509 136.5747 0.007407407
26 -32.85384 135.1541 -33.64998 134.8903 0.007407407
45 -33.68415 136.9237 -34.37630 136.1034 0.007407407
62 -33.03811 137.5801 -33.64998 134.8903 0.007407407
41 -34.37630 136.1034 -34.11956 136.3507 0.011111111

这给了我以下错误消息:

Error in plot.xy(xy.coords(x, y), type = type, ...) :    plot.new has not been called yet

在ggplot地图上绘制线条的正确方法是什么

0 个答案:

没有答案