这与我之前的 SO 帖子有关。我正在尝试使用不规则间隔的数据绘制轮廓。我尝试了三种方法来覆盖地图(来自各种 SO 帖子)。它们都不适合我。我会很感激帮助。以下是我尝试的三种方法:方法1)
来自谷歌的地图
rwa <- get_map(location = c(lon = 29.75, lat = -2), zoom = 9, maptype = "toner", source = "stamen")
g1 <- ggmap(rwa)
这向我显示了 google url 和下面的行以及服务条款的链接 Google Maps API 服务条款
然后什么也没有发生;
方法二
remfname <- "http://biogeo.ucdavis.edu/data/diva/adm/RWA_adm.zip"
locfname <- "/tmp/RWAadm.gz"
download.file(remfname, locfname)
con <- gzcon(file(locfname, "rb"))
myPoints <- readBin(con, numeric(), n=1e6, size = 4, endian = "little")
close(con)
rwa <- fortify(myPoints)
这给了我以下错误:
int [1:89709] 67324752 20 1446445064 1826437762 49564023 370147328 786432 1464991744 1684102977 1680748653 ...
Error: ggplot2 doesn't know how to deal with data of class integer.
用整数替换 numeric() 会引发类似的错误,而 real() 是未知的
方法三
tmpenv <- new.env()
load("C:/R_Progs/Scripts/RWA_adm0.RData", envir=tmpenv, verbose = TRUE)
rwa <- tmpenv$Data
rwa <- fortify(myPoints)
或者
rwa <- readOGR(dsn = "C:/R_Progs/Scripts", layer = "RWA_adm0")
rwa <- fortify(myPoints)
Error:
Error in readChar(con, 5L, useBytes = TRUE) : cannot open the connection
In addition: Warning message:
In readChar(con, 5L, useBytes = TRUE) :
cannot open compressed file 'C:/R_Progs/rwanda/R.RData', probable reason 'No such file or directory'
Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv) :
Cannot open file
这些是我的绘图功能
g1 + geom_tile(data = df, aes(x = Lon, y = Lat, z = Rain, fill = Rain), alpha = 0.8) + stat_contour(data = df, aes(x = Lon, y = Lat, z = Rain)) +
ggtitle("Rwandan rainfall") + xlab("Longitude") + ylab("Latitude") + scale_fill_continuous(name = "Rain (mm)",
low = "white", high = "blue") + theme_bw() + theme(plot.title = element_text(size = 25, face = "bold"),
legend.title = element_text(size = 15), axis.text = element_text(size = 15), axis.title.x = element_text(size = 20, vjust = -0.5),
axis.title.y = element_text(size = 20, vjust = 0.2),legend.text = element_text(size = 10)) + coord_map()
和
ggplot() +
geom_polygon(data = rwa, aes(x = long, y = lat, group = group),
colour = "black", size = 0.5, fill = "white") + stat_contour(data = df, aes(x = Lon, y = Lat, z = Rain)) +
ggtitle("Rwandan rainfall") + xlab("Longitude") + ylab("Latitude") + scale_fill_continuous(name = "Rain (mm)",
low = "white", high = "blue") + theme_bw() + theme(plot.title = element_text(size = 25, face = "bold"),
legend.title = element_text(size = 15), axis.text = element_text(size = 15), axis.title.x = element_text(size = 20, vjust = -0.5),
axis.title.y = element_text(size = 20, vjust = 0.2),legend.text = element_text(size = 10)) + coord_map()
这些是加载的库;
library(reshape2)
library(ggplot2)
library(ggmap)
library(rgdal)
library(akima)
library(directlabels)
和准备数据:
datr <- read.table("Apr0130precip.txt",header=TRUE,sep="")
datfr <- data.frame(datr)
x <- datfr[,1]
y <- datfr[,2]
z <- datfr[,3]
fld <- with(datfr, interp(x , y, z))
df <- melt(fld$z, na.rm = TRUE)
names(df) <- c("x", "y", "Rain")
df$Lon <- fld$x[df$x]
df$Lat <- fld$y[df$y]
这是数据:
Lon Lat Rain
28.92 -2.47 83.4
29.02 -2.68 144
29.25 -1.67 134.7
29.42 -2.07 174.9
29.55 -1.58 151.5
29.57 -2.48 224.1
29.6 -1.5 254.3
29.72 -2.18 173.9
30.03 -1.95 154.8
30.05 -1.6 152.2
30.13 -1.97 126.2
30.33 -1.3 98.5
30.45 -1.81 145.5
30.5 -2.15 151.3