R 在不规则网格上绘制等高线

R 在不规则网格上绘制等高线,r,ggplot2,contour,R,Ggplot2,Contour,我在R中翻阅了一页又一页的等高线图(包括许多关于stackoverflow的提示),但没有成功。这是我的等高线数据,包括添加卢旺达地图(数据包括14个经度、纬度和降雨值,如x、y和z): 以下是我在stackoverflow中尝试的代码: datr <- read.table("Apr0130precip.txt",header=TRUE,sep=",") x <- datr$x y <- datr$y z <- datr$z require(akima) fld &

我在R中翻阅了一页又一页的等高线图(包括许多关于stackoverflow的提示),但没有成功。这是我的等高线数据,包括添加卢旺达地图(数据包括14个经度、纬度和降雨值,如x、y和z):

以下是我在stackoverflow中尝试的代码:

datr <- read.table("Apr0130precip.txt",header=TRUE,sep=",")
x <- datr$x
y <- datr$y
z <- datr$z

require(akima)

fld <- interp(x,y,z)

par(mar=c(5,5,1,1))
filled.contour(fld)

datr这里有一些使用
base
R图形和
ggplot
的不同可能性。生成简单等高线图和地图顶部的图


插值


基本
ggplot
使用
geom\u tile
stat\u等高线的替代方案


ggplot
在从shapefile创建的地图上
#因为我没有你的地图对象,所以我喜欢这样:
#从中获取地图数据
# http://biogeo.ucdavis.edu/data/diva/adm/RWA_adm.zip
#将文件解压缩到名为“卢旺达”的文件夹
#使用rgdal::readOGR读取shapefile
#只需尝试三个形状文件中的第一个,这似乎很有效。
#“dsn”(数据源名称)是shapefile所在的文件夹
#“layer”是不带.shp扩展名的shapefile的名称。
图书馆(rgdal)
rwa
datr <- read.table("Apr0130precip.txt",header=TRUE,sep=",")
x <- datr$x
y <- datr$y
z <- datr$z

require(akima)

fld <- interp(x,y,z)

par(mar=c(5,5,1,1))
filled.contour(fld)
library(akima)
fld <- with(df, interp(x = Lon, y = Lat, z = Rain))
filled.contour(x = fld$x,
               y = fld$y,
               z = fld$z,
               color.palette =
                 colorRampPalette(c("white", "blue")),
               xlab = "Longitude",
               ylab = "Latitude",
               main = "Rwandan rainfall",
               key.title = title(main = "Rain (mm)", cex.main = 1))
library(ggplot2)
library(reshape2)

# prepare data in long format
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]

ggplot(data = df, aes(x = Lon, y = Lat, z = Rain)) +
  geom_tile(aes(fill = Rain)) +
  stat_contour() +
  ggtitle("Rwandan rainfall") +
  xlab("Longitude") +
  ylab("Latitude") +
  scale_fill_continuous(name = "Rain (mm)",
                        low = "white", high = "blue") +
  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))
# grab a map. get_map creates a raster object
library(ggmap)
rwanda1 <- get_map(location = c(lon = 29.75, lat = -2),
                  zoom = 9,
                  maptype = "toner",
                  source = "stamen")
# alternative map
# rwanda2 <- get_map(location = c(lon = 29.75, lat = -2),
#                   zoom = 9,
#                   maptype = "terrain")

# plot the raster map
g1 <- ggmap(rwanda1)
g1

# plot map and rain data
# use coord_map with default mercator projection
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(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()
# Since I don't have your map object, I do like this instead:
# get map data from
# http://biogeo.ucdavis.edu/data/diva/adm/RWA_adm.zip
# unzip files to folder named "rwanda"

# read shapefile with rgdal::readOGR
# just try the first out of three shapefiles, which seemed to work.
# 'dsn' (data source name) is the folder where the shapefile is located
# 'layer' is the name of the shapefile without the .shp extension.

library(rgdal)
rwa <- readOGR(dsn = "rwanda", layer = "RWA_adm0")
class(rwa)
# [1] "SpatialPolygonsDataFrame"

# convert SpatialPolygonsDataFrame object to data.frame
rwa2 <- fortify(rwa)
class(rwa2)
# [1] "data.frame"

# plot map and raindata  
ggplot() + 
  geom_polygon(data = rwa2, aes(x = long, y = lat, group = group),
               colour = "black", size = 0.5, fill = "white") +
  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()