简体   繁体   English

用离散色标绘制填充轮廓图

[英]Plotting filled contour map with discrete color scale

Thanks to this answer I've been able to plot maps from NetCDF files with the correct projection and overlay map. 多亏了这个答案,我才能够使用正确的投影和覆盖图绘制NetCDF文件中的图。 However, when plotting filled contour maps using ggplot, I encounter an error. 但是,当使用ggplot绘制填充的轮廓图时,遇到错误。 (see the linked answer for the input file) (请参阅输入文件的链接答案)

#See https://gis.stackexchange.com/questions/120900/plotting-netcdf-file-using-lat-and-lon-contained-in-variables

# First, estimate the extent.
# We start with the lat and long, then project it to the Lambert Conformal projection
library(raster)
inputfile <- "file.nc"

# Grab the lat and lon from the data
lat <- raster(inputfile, varname="xlat")
lon <- raster(inputfile, varname="xlon")

# Convert to points and match the lat and lons
plat <- rasterToPoints(lat)
plon <- rasterToPoints(lon)
lonlat <- cbind(plon[,3], plat[,3])

# Specify the lonlat as spatial points with projection as long/lat
lonlat <- SpatialPoints(lonlat, proj4string = CRS("+proj=longlat +datum=WGS84"))

# Need the rgdal package to project it to the original coordinate system
library("rgdal")

# My best guess at the proj4 string from the information given
mycrs <- CRS("+proj=lcc +lat_1=35.00 +lat_2=51.00 +lat_0=39.00 +lon_0=14.00 +x_0=-6000. +y_0=-6000. +ellps=sphere +a=6371229. +b=6371229. +units=m +no_defs")
plonlat <- spTransform(lonlat, CRSobj = mycrs)
# Take a look
plonlat
extent(plonlat)

# Yay! Now we can properly set the coordinate information for the raster
pr <- raster(inputfile, varname="pr")
# Fix the projection and extent
projection(pr) <- mycrs
extent(pr) <- extent(plonlat)
# Take a look
pr
plot(pr)

# Project to long lat grid
r <- projectRaster(pr, crs=CRS("+proj=longlat +datum=WGS84"))

library(rasterVis)
#Plot with ggplot:
#Add an overlay map
library(maps)
world    <-data.frame(map(plot=FALSE)[c("x","y")])
gplot(r*86400) + 
  geom_tile(aes(fill=value)) + 
  scale_fill_discrete() + 
  geom_path(aes(x,y), data=world) + 
  coord_equal(xlim=c(xmin(r), xmax(r)), ylim=c(ymin(r), ymax(r)))

Error: Continuous value supplied to discrete scale
This is because I want to plot a discrete contour map, with discrete colours, not with a continuous colour scale. 这是因为我要绘制具有离散颜色而不是连续色标的离散轮廓图。 Of course it works if using scale_fill_continuous. 当然,如果使用scale_fill_continuous,它就可以工作。
I cannot use fill=factor(value) beacuse it will take ages (and GBs of memory) and then return me with a color entry for each single different value in the dataset. 我不能使用fill=factor(value)因为它将花费一定的时间(和GB的内存),然后为数据集中的每个不同值返回一个带有颜色条目的颜色。 I could manually create some "bins" and modify the dataset to fit into those, but I feel this would be a workaround for a much easier solution. 我可以手动创建一些“ bin”并修改数据集以适合其中的那些,但是我觉得这将是一种更简单的解决方案。

What is the simple elegant solution I'm missing? 我缺少的简单优雅的解决方案是什么? Shouldn't I use geom_tile (or geom_raster)? 我不应该使用geom_tile(或geom_raster)吗?

EDIT: Thsi is a pdf example of what I'd like to get: https://copy.com/yMDzEt4i1ELMxpca That plot is created with GrADS. 编辑:这是我想要得到的pdf示例: https ://copy.com/yMDzEt4i1ELMxpca该图是用GrADS创建的。 The presence or absence of a color legend is not important, sometimes I use it sometimes I don't. 颜色图例的存在与否并不重要,有时我会使用,有时则不使用。

EDIT2: cut(value) could do but as stated I'd like a ggplot solution and additionally... EDIT2: cut(value)可以,但是如上所述,我想要一个ggplot解决方案,另外...

...is an example of what it produces. ...是它产生的一个例子。 I'd like the labels to be between the colors, not above, like this: 我希望标签位于颜色之间,而不是上方,如下所示:

Based on your edits I would do the following: 根据您的修改,我将执行以下操作:

r$value.bin <- cut(r$value, c(0:4, 2 * (3:5)))
gplot(r*86400) + 
    geom_tile(aes(fill = value.bin)) + 
    scale_fill_manual(
       values = colorRampPalette(brewer.pal(9, "Greens"))(nlevels(r$value.bin))) + 
    geom_path(aes(x,y), data=world) + 
    coord_equal(xlim=c(xmin(r), xmax(r)), ylim=c(ymin(r), ymax(r)))

Maybe you have to play with the colours in scale_fill_manual and the cutting a bit such that there are spots with an NA color. 也许您必须使用scale_fill_manual的颜色并进行一些裁剪,以使某些斑点具有NA颜色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM