我猜我有一个简单的解决方案,但我遇到了一些麻烦.
我试图转换以下map对象:
require(maps)
usa <- map("state")
Run Code Online (Sandbox Code Playgroud)
SpatialPolygon使用map2SpatialPolygons函数进入对象:
require(maptools)
usa.sp <- map2SpatialPolygons(usa, IDs=usa$names,proj4string=CRS("+proj=longlat"))
Run Code Online (Sandbox Code Playgroud)
我一直收到以下错误:
Error in map2SpatialPolygons(usa, IDs = usa$names, proj4string = CRS("+proj=longlat")) :
map and IDs differ in length
Run Code Online (Sandbox Code Playgroud)
经过一些研究,看起来ID的长度为63,并且map在应用函数后对象的长度为169 .NAmat2xyList(cbind(map$x, map$y))(我找不到源代码).
有人有主意吗?这是usa地图对象的结构:
> str(usa)
List of 4
$ x : num [1:1705] -88.4 -88.1 -88 -87.9 -87.8 ...
$ y : num [1:1705] 30.4 30.4 30.8 30.6 30.3 ...
$ range: num [1:4] -124.7 -67 25.1 49.4
$ names: …Run Code Online (Sandbox Code Playgroud) 我有一个ggplot地图,例如:
library(ggmap)
ggmap(get_map())
Run Code Online (Sandbox Code Playgroud)
我希望轴标签自动标记为NS/WE:在上述情况下,例如,它应该显示95.4°E而不是lon -95.4.
我试图弄乱scales包和使用scale_x_continuous和scale_y_continuous标签和打破选项,但我没有设法让它工作.
拥有一个scale_y_latitude和那将是非常棒的scale_x_longitude.
编辑:感谢@Jaap的回答,我得到了以下内容:
scale_x_longitude <- function(xmin=-180, xmax=180, step=1, ...) {
ewbrks <- seq(xmin,xmax,step)
ewlbls <- unlist(lapply(ewbrks, function(x) ifelse(x < 0, paste(x, "W"), ifelse(x > 0, paste(x, "E"),x))))
return(scale_x_continuous("Longitude", breaks = ewbrks, labels = ewlbls, expand = c(0, 0), ...))
}
scale_y_latitude <- function(ymin=-90, ymax=90, step=0.5, ...) {
nsbrks <- seq(ymin,ymax,step)
nslbls <- unlist(lapply(nsbrks, function(x) ifelse(x < 0, paste(x, "S"), ifelse(x > 0, paste(x, "N"),x))))
return(scale_y_continuous("Latitude", …Run Code Online (Sandbox Code Playgroud) 我已经将此问题作为在不规则网格问题上绘制数据的有效方法的一部分,但一般反馈是将原始问题拆分为更易于管理的块.因此,这个新问题.
我使用组织在不规则二维网格上的卫星数据,其尺寸为扫描线(沿轨道尺寸,即Y轴)和地面像素(跨越轨道尺寸,即X轴).每个中心像素的纬度和经度信息存储在辅助坐标变量中,以及四个角坐标对(纬度和经度坐标在WGS84参考椭球上给出).
让我们构建一个玩具数据集,包括12x10可能不规则的网格和相关的表面温度测量.
library(pracma) # for the meshgrid function
library(ggplot2)
num_sl <- 12 # number of scanlines
num_gp <- 10 # number of ground pixels
l <- meshgrid(seq(from=-20, to=20, length.out = num_gp),
seq(from=30, to=60, length.out = num_sl))
lon <- l[[1]] + l[[2]]/10
lat <- l[[2]] + l[[1]]/10
data <- matrix(seq(from = 30, to = 0, length.out = num_sl*num_gp),
byrow = TRUE, nrow = num_sl, ncol = num_gp) +
matrix(runif(num_gp*num_sl)*6, nrow = num_sl, ncol = …Run Code Online (Sandbox Code Playgroud) 我使用组织在不规则二维网格上的卫星数据,其尺寸为扫描线(沿轨道尺寸)和地面像素(跨越轨道尺寸).每个中心像素的纬度和经度信息存储在辅助坐标变量中,以及四个角坐标对(纬度和经度坐标在WGS84参考椭球上给出).数据存储在netCDF4文件中.
我想要做的是在投影地图上有效地绘制这些文件(可能还有文件的组合 - 下一步!).
我的做法,到目前为止,灵感来自杰里米的Voisey的回答这个问题,一直在打造我的兴趣可变连杆的像素边界的数据帧,并使用ggplot2与geom_polygon用于实际的情节.
让我来说明我的工作流程,并提前为天真的方法道歉:我刚开始用一周或两周的R编码.
注意
要完全重现问题:
1.下载两个数据帧:so2df.Rda(22M)和pixel_corners.Rda(26M)
2.在您的环境中加载它们,例如
so2df <- readRDS(file="so2df.Rda")
pixel_corners <- readRDS(file="pixel_corners.Rda")
Run Code Online (Sandbox Code Playgroud)
我要从我的文件中读取数据和纬度/经度边界.
library(ncdf4)
library(ggplot2)
library(ggmap)
# set path and filename
ncpath <- "/Users/stefano/src/s5p/products/e1dataset/L2__SO2/"
ncname <- "S5P_OFFL_L2__SO2____20171128T234133_20171129T003956_00661_01_022943_00000000T000000"
ncfname <- paste(ncpath, ncname, ".nc", sep="")
nc <- nc_open(ncfname)
# save fill value and multiplication factors
mfactor = ncatt_get(nc, "PRODUCT/sulfurdioxide_total_vertical_column",
"multiplication_factor_to_convert_to_DU")
fillvalue = ncatt_get(nc, "PRODUCT/sulfurdioxide_total_vertical_column",
"_FillValue")
# read the SO2 total column variable
so2tc <- …Run Code Online (Sandbox Code Playgroud)