r - 在不规则网格上绘制轮廓

标签 r ggplot2 contour

我浏览了R中的页面和轮廓图页面(包括关于stackoverflow的许多提示),但均未成功。这是我的轮廓数据,包括添加卢旺达 map (数据包含14个经度,纬度和降雨值,分别为x,y和z):

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

这是我从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)

插值失败。将不胜感激。

最佳答案

这是一些使用base R图形和ggplot的可能性。既生成了简单的轮廓图,又生成了位于 map 顶部的图。

插补

library(akima)
fld <- with(df, interp(x = Lon, y = Lat, z = Rain))
base使用filled.contour的R图
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))

使用ggplotgeom_tile的基本stat_contour替代
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))

ggplot创建的Google map 上的ggmap
# 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()

从shapefile创建的 map 上的ggplot
# 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()

当然,可以使用the nice tools for spatial data in R对降雨量数据进行插值和绘图。考虑一下我的回答,这是一个相当快速和容易的开始。

关于r - 在不规则网格上绘制轮廓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19339296/

相关文章:

r - 寻找替换列表中字符向量元素的方法

r - ggplot `expand_scale()` 轴 - 不一致

opencv - 从颜色形状中检测轮廓点

r - 如何自定义 ggplot2 中 geom_tile 内轮廓的形状?

python - 无法找到和绘制最大轮廓

r - Windows 7,update.packages 问题 : "unable to move temporary installation"?

r - 使用 data.table 基于多列对行进行子集化 - 最快的方式

将列表重复到 R 中的数据框

r - 不同 Y 轴的组合柱图和折线图

r - 堆积面积图未正确显示负值