在不规则网格上绘制轮廓 [英] Plotting contours on an irregular grid
问题描述
我已经浏览了R中的页面和等高线图(包括很多关于stackoverflow的提示),但都没有成功。这里是我的等值线数据,包括添加卢旺达地图(数据由14个经度,纬度和雨量值组成,分别为x,y和z):
Lon Lat雨
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
<
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))
已满。 contour(fld)
插值失败.help将会被理解。
ggplot $ c $的一些不同的可能性C>。这两个简单的轮廓图,并生成地图上的地图。
插值
library(akima)
fld < - with(df,interp(x = Lon,y = Lat,z = Rain))
base
使用 filled.contour < code
$ b $ $ $ $ $ $ $ $ $ $ $> fill.contour(x = fld $ x,
y = fld $ y,
z = fld $ z,
color.palette =
colorRampPalette(c(white,blue)),
xlab =Longitude,
ylab =纬度,
main =卢旺达降雨量,
key.title = title(main =Rain(mm),cex.main = 1))
基本 ggplot
使用 geom_tile
和 stat_contour
library(ggplot2)
库(reshape2)
#以长格式准备数据
df < - melt(fld $ z,na.rm = TRUE)
名称(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(卢旺达降雨量 )+
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))
由 ggmap
创建的Google Map上的 ggplot
#抓取地图。 get_map创建一个栅格对象
library(ggmap)
rwanda1< - get_map(location = c(lon = 29.75,lat = -2),
zoom = 9,
maptype =碳粉,
源=雄蕊)
#替代地图
#rwanda2 < - get_map(location = c(lon = 29.75,lat = -2),
#zoom = 9,
#maptype =terrain)
#绘制栅格地图
g1< - ggmap(rwanda1)
g1
#阴谋地图和降雨数据
#使用coord_map默认的mercator投影
g1 +
geom_tile(data = df,aes(x = Lon,y = Lat,z = (卢旺达降雨量)+
ggtitle(卢旺达降雨量)+
stat_contour(data = df,aes(x = Lon,y = Lat,z = Rain)
xlab(Longitude)+
ylab(Latitude)+
scale_fill_continuous(name =Rain(mm),
low =white,high =蓝色)+
主题(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
从shapefile创建的地图上
#因为我没有地图对象,所以我喜欢这样:
#从$ b $获取地图数据b#http://biogeo.ucdavis.edu/data/diva/adm/RWA_adm.zip
#将文件解压缩到名为rwanda的文件夹
#使用rgdal :: readOGR读取shapefile
#只是尝试三个shape文件中的第一个,这似乎工作。
#'dsn'(数据源名称)是shapefile所在的文件夹
#'layer'是没有.shp扩展名的shapefile的名称。
library(rgdal)
rwa< - readOGR(dsn =rwanda,layer =RWA_adm0)
class(rwa)
#[1] SpatialPolygonsDataFrame
#将SpatialPolygonsDataFrame对象转换为data.frame
rwa2< - fortify(rwa)
class(rwa2)
#[1]数据。框架
#绘图和raindata
ggplot()+
geom_polygon(data = rwa2,aes(x = long,y = lat,group = group),
color =black,size = 0.5,fill =white)+
geom_tile(data = df,aes(x = Lon,y = Lat,z = Rain,fill = Rain) 0.8)+
stat_contour(data = df,aes(x = Lon,y = Lat,z = Rain))+
ggtitle(卢旺达降雨量)+
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_tex t(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()
使用 R中空间数据的好工具。考虑我的答案是一个相当快速和简单的开始。
I have gone through pages and pages of contour plots in R (including many hints on stackoverflow) without success. Here is my data to contour, including adding a map of Rwanda (the data consists of 14 values of longitude, latitude and rain as x,y and 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
Here is the code I tried from 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)
The interpolation fails.help will be appreciated.
解决方案 Here are some different possibilites using base
R graphics and ggplot
. Both simple contours plots, and plots on top of maps are generated.
Interpolation
library(akima)
fld <- with(df, interp(x = Lon, y = Lat, z = Rain))
base
R plot using filled.contour
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))
Basic ggplot
alternative using geom_tile
and 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
on a Google Map created by 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()
ggplot
on a map created from shapefile
# 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 interpolation and plotting of your rainfall data could of course be done in a much more sophisticated way, using the nice tools for spatial data in R. Consider my answer a fairly quick and easy start.
这篇关于在不规则网格上绘制轮廓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!