本文介绍了如何设置使用ggplot2映射栅格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用R Studio与下面的类似(在Arc Map中创建)进行绘图.

I would like to make a plot using R studio similar to the one below (created in Arc Map)

我尝试了以下代码:

# data processing
library(ggplot2)
# spatial
library(raster)
library(rasterVis)
library(rgdal)

#
test <- raster(paste(datafold,'oregon_masked_tmean_2013_12.tif',sep="")) # read the temperature raster
OR<-readOGR(dsn=ORpath, layer="Oregon_10N") # read the Oregon state boundary shapefile

gplot(test) +  
  geom_tile(aes(fill=factor(value),alpha=0.8)) + 
  geom_polygon(data=OR, aes(x=long, y=lat, group=group), 
               fill=NA,color="grey50", size=1)+
  coord_equal()

该代码的输出如下所示:

The output of that code looks like this:

需要注意的几件事.首先,R版本缺少分水岭的shapefile.那样就好.

A couple of things to note. First, the watershed shapefiles are missing from the R version. that is fine.

第二,R图中较深的灰色背景为无数据"值.在Arc中,不显示它们,但是在R中,它们显示为gplot.当我使用光栅包中的绘图"时,它们不会显示:

Second, The darker gray background in the R plot is No Data values. In Arc, they do not display, but in R they display with gplot. They do not display when I use 'plot' from the raster package:

plot(test)

我的问题如下:

  1. 如何摆脱"gplot"示例中的深灰色NoData填充?
  2. 如何设置图例(颜色栏)合理(例如在ArcMap和栅格绘图"图例中?)
  3. 如何控制颜色图?

请注意,我尝试了

scale_fill_brewer
scale_fill_manual
scale_fill_gradient

依此类推,但例如,我得到了错误

and so on and so forth but I get errors, for example

br <- seq(minValue(test), maxValue(test), len=8)

gplot(test)+
geom_tile(aes(fill=factor(value),alpha=0.8)) +

scale_fill_gradient(breaks = br,labels=sprintf("%.02f", br)) +

geom_polygon(data=OR, aes(x=long, y=lat, group=group), 
             fill=NA,color="grey50", size=1)+
coord_equal()

Regions defined for each Polygons
Error: Discrete value supplied to continuous scale

最后,一旦有了绘制这些贴图之一的解决方案,我想在一个图形上绘制多个贴图,并为整个面板创建一个颜色条(即,所有地图都使用一个颜色条),我想能够控制颜色条的位置和颜色条的大小.这是我可以使用grid.arrange进行操作的示例,但是我不知道如何设置单个颜色条:

Finally, once I have a solution for plotting one of these maps, I would like to plot multiple maps on one figure and create a single colorbar for the entire panel (i.e. one colorbar for all the maps) and I would like to be able to control where the colorbar is located and the size of the colorbar. Here is an example of what I can do with grid.arrange, but I cannot figure out how to set a single colorbar:

r1 <- test
r2 <- test
r3 <- test
r4 <- test

colr <- colorRampPalette(rev(brewer.pal(11, 'RdBu')))

l1 <- levelplot(r1, 
          margin=FALSE,                       
          colorkey=list(
             space='bottom',                   
             labels=list(at=-5:5, font=4),
             axis.line=list(col='black')       
          ),    
          par.settings=list(
             axis.line=list(col='transparent') 
          ),
          scales=list(draw=FALSE),            
          col.regions=viridis,                   
          at=seq(-5, 5, len=101)) +           
   layer(sp.polygons(oregon, lwd=3))

l2 <- levelplot(r2, 
                margin=FALSE,                       
                colorkey=list(
                   space='bottom',                   
                   labels=list(at=-5:5, font=4),
                   axis.line=list(col='black')       
                ),    
                par.settings=list(
                   axis.line=list(col='transparent') 
                ),
                scales=list(draw=FALSE),            
                col.regions=viridis,                   
                at=seq(-5, 5, len=101)) +           
   layer(sp.polygons(oregon, lwd=3))

l3 <- levelplot(r3, 
                margin=FALSE,                       
                colorkey=list(
                   space='bottom',                   
                   labels=list(at=-5:5, font=4),
                   axis.line=list(col='black')       
                ),    
                par.settings=list(
                   axis.line=list(col='transparent') 
                ),
                scales=list(draw=FALSE),            
                col.regions=viridis,                   
                at=seq(-5, 5, len=101)) +           
   layer(sp.polygons(oregon, lwd=3))

l4 <- levelplot(r4, 
                margin=FALSE,                       
                colorkey=list(
                   space='bottom',                   
                   labels=list(at=-5:5, font=4),
                   axis.line=list(col='black')       
                ),    
                par.settings=list(
                   axis.line=list(col='transparent') 
                ),
                scales=list(draw=FALSE),            
                col.regions=viridis,                   
                at=seq(-5, 5, len=101)) +           
   layer(sp.polygons(oregon, lwd=3))

grid.arrange(l1, l2, l3, l4,nrow=2,ncol=2) #use package gridExtra   

输出是这样的:

shapefile和栅格文件可通过以下链接获得:

The shapefile and raster file are available at the following link:

https://drive.google.com/open?id=0B5PPm9lBBGbDTjBjeFNzMHZYWEU

非常感谢.

devtools :: session_info()会话信息------------------------------------------------ -------------------------------------------------- ------------------- 设定值
版本R版本3.1.1(2014-07-10) 系统x86_64,darwin10.8.0
ui RStudio(0.98.1103)
语言(EN)
整理en_US.UTF-8
tz America/Los_Angeles

devtools::session_info()Session info --------------------------------------------------------------------------------------------------------------------- setting value
version R version 3.1.1 (2014-07-10) system x86_64, darwin10.8.0
ui RStudio (0.98.1103)
language (EN)
collate en_US.UTF-8
tz America/Los_Angeles

包装---------------------------------------------- -------------------------------------------------- ------------------------- 包*版本日期来源
bitops 1.0-6 2013-08-17 CRAN(R 3.1.0) 色彩空间1.2-6 2015-03-11 CRAN(R 3.1.3) devtools 1.8.0 2015-05-09 CRAN(R 3.1.3) 摘要0.6.4 2013-12-03 CRAN(R 3.1.0) ggplot2 * 1.0.1 2015-03-17 CRAN(R 3.1.3) ggthemes * 2.1.2 2015-03-02 CRAN(R 3.1.3) git2r 0.10.1 2015-05-07 CRAN(R 3.1.3) gridExtra 0.9.1 2012-08-09 CRAN(R 3.1.0) gtable 0.1.2 2012-12-05 CRAN(R 3.1.0) hexbin * 1.26.3 2013-12-10 CRAN(R 3.1.0) 晶格* 0.20-29 2014-04-04 CRAN(R 3.1.1) mosaicExtra * 0.6-26 2013-08-15 CRAN(R 3.1.0) magrittr 1.5 2014-11-22 CRAN(R 3.1.2) MASS 7.3-33 2014-05-05 CRAN(R 3.1.1) 备忘录0.2.1 2014-04-22 CRAN(R 3.1.0) munsell 0.4.2 2013-07-11 CRAN(R 3.1.0) plyr 1.8.2 2015-04-21 CRAN(R 3.1.3) 原型0.3-10 2012-12-22 CRAN(R 3.1.0) 栅格* 2.2-31 2014-03-07 CRAN(R 3.1.0) rasterVis * 0.28 2014-03-25 CRAN(R 3.1.0) RColorBrewer * 1.0-5 2011-06-17 CRAN(R 3.1.0) Rcpp 0.11.2 2014-06-08 CRAN(R 3.1.0) RCurl 1.95-4.6 2015-04-24 CRAN(R 3.1.3) reshape2 1.4.1 2014-12-06 CRAN(R 3.1.2) rgdal * 0.8-16 2014-02-07 CRAN(R 3.1.0) rversions 1.0.0 2015-04-22 CRAN(R 3.1.3) 秤* 0.2.4 2014-04-22 CRAN(R 3.1.0) sp * 1.0-15 2014-04-09 CRAN(R 3.1.0) 弦0.4-1 2014-12-14 CRAN(R 3.1.2) 纵梁1.0.0 2015-04-30 CRAN(R 3.1.3) viridis * 0.3.1 2015-10-11 CRAN(R 3.2.0) XML 3.98-1.1 2013-06-20 CRAN(R 3.1.0) 动物园1.7-11 2014-02-27 CRAN(R 3.1.0)

Packages ------------------------------------------------------------------------------------------------------------------------- package * version date source
bitops 1.0-6 2013-08-17 CRAN (R 3.1.0) colorspace 1.2-6 2015-03-11 CRAN (R 3.1.3) devtools 1.8.0 2015-05-09 CRAN (R 3.1.3) digest 0.6.4 2013-12-03 CRAN (R 3.1.0) ggplot2 * 1.0.1 2015-03-17 CRAN (R 3.1.3) ggthemes * 2.1.2 2015-03-02 CRAN (R 3.1.3) git2r 0.10.1 2015-05-07 CRAN (R 3.1.3) gridExtra 0.9.1 2012-08-09 CRAN (R 3.1.0) gtable 0.1.2 2012-12-05 CRAN (R 3.1.0) hexbin * 1.26.3 2013-12-10 CRAN (R 3.1.0) lattice * 0.20-29 2014-04-04 CRAN (R 3.1.1) latticeExtra * 0.6-26 2013-08-15 CRAN (R 3.1.0) magrittr 1.5 2014-11-22 CRAN (R 3.1.2) MASS 7.3-33 2014-05-05 CRAN (R 3.1.1) memoise 0.2.1 2014-04-22 CRAN (R 3.1.0) munsell 0.4.2 2013-07-11 CRAN (R 3.1.0) plyr 1.8.2 2015-04-21 CRAN (R 3.1.3) proto 0.3-10 2012-12-22 CRAN (R 3.1.0) raster * 2.2-31 2014-03-07 CRAN (R 3.1.0) rasterVis * 0.28 2014-03-25 CRAN (R 3.1.0) RColorBrewer * 1.0-5 2011-06-17 CRAN (R 3.1.0) Rcpp 0.11.2 2014-06-08 CRAN (R 3.1.0) RCurl 1.95-4.6 2015-04-24 CRAN (R 3.1.3) reshape2 1.4.1 2014-12-06 CRAN (R 3.1.2) rgdal * 0.8-16 2014-02-07 CRAN (R 3.1.0) rversions 1.0.0 2015-04-22 CRAN (R 3.1.3) scales * 0.2.4 2014-04-22 CRAN (R 3.1.0) sp * 1.0-15 2014-04-09 CRAN (R 3.1.0) stringi 0.4-1 2014-12-14 CRAN (R 3.1.2) stringr 1.0.0 2015-04-30 CRAN (R 3.1.3) viridis * 0.3.1 2015-10-11 CRAN (R 3.2.0) XML 3.98-1.1 2013-06-20 CRAN (R 3.1.0) zoo 1.7-11 2014-02-27 CRAN (R 3.1.0)

推荐答案

使用rasterVis::levelplot的方法如下:

加载东西:

library(rgdal)
library(rasterVis)
library(RColorBrewer)

阅读内容:

oregon <- readOGR('.', 'Oregon_10N')
r <- raster('oregon_masked_tmean_2013_12.tif')

定义一个色带调色板(或长度为1的颜色矢量,该颜色比下面的at参数定义的色带的中断数短).

Define a colour ramp palette (or a vector of colours with length 1 shorter than the number of breaks for the colour ramp defined with the at argument below).

colr <- colorRampPalette(brewer.pal(11, 'RdYlBu'))

绘制事物:

levelplot(r, 
          margin=FALSE,                       # suppress marginal graphics
          colorkey=list(
            space='bottom',                   # plot legend at bottom
            labels=list(at=-5:5, font=4)      # legend ticks and labels 
          ),    
          par.settings=list(
            axis.line=list(col='transparent') # suppress axes and legend outline
          ),
          scales=list(draw=FALSE),            # suppress axis labels
          col.regions=colr,                   # colour ramp
          at=seq(-5, 5, len=101)) +           # colour ramp breaks
  layer(sp.polygons(oregon, lwd=3))           # add oregon SPDF with latticeExtra::layer

您实际上可能希望绘制图例轮廓(包括其刻度线),在这种情况下,请将axis.line=list(col='black')添加到colorkey args列表中.这是覆盖由par.settings=list(axis.line=list(col='transparent'))引起的对框的一般抑制所必需的:

You might actually want the legend outline (including its ticks) plotted, in which case add axis.line=list(col='black') to the list of colorkey args. This is required to override the general suppression of boxes caused by par.settings=list(axis.line=list(col='transparent')):

levelplot(r, 
          margin=FALSE,                       
          colorkey=list(
            space='bottom',                   
            labels=list(at=-5:5, font=4),
            axis.line=list(col='black')       
          ),    
          par.settings=list(
            axis.line=list(col='transparent') 
          ),
          scales=list(draw=FALSE),            
          col.regions=colr,                   
          at=seq(-5, 5, len=101)) +           
  layer(sp.polygons(oregon, lwd=3))                  

我同意@hrbrmstr,认为viridis通常是更好地使用,尽管在​​我看来有点难看.与诸如ColorBrewer的RdYlBu之类的东西相比,其主要优点是,去饱和时颜色仍然清晰,并且色差更好地反映了值的差异.我相信RdYlBu可以完美地用于Deuteranopia/Protanopia/Tritanopia色盲.

I agree with @hrbrmstr that viridis is often a better ramp to use, despite being--in my opinion--a bit ugly. The main benefits over something like ColorBrewer's RdYlBu are that colours are still distinct when desaturated, and colour differences better reflect the differences in values. I believe that RdYlBu is perfectly accessible for Deuteranopia/Protanopia/Tritanopia colour-blindness, though.

这是viridis版本:

Here's the viridis version:

library(viridis)
levelplot(r, 
          margin=FALSE,                       
          colorkey=list(
            space='bottom',                   
            labels=list(at=-5:5, font=4),
            axis.line=list(col='black')       
          ),    
          par.settings=list(
            axis.line=list(col='transparent') 
          ),
          scales=list(draw=FALSE),            
          col.regions=viridis,                   
          at=seq(-5, 5, len=101)) +           
  layer(sp.polygons(oregon, lwd=3))

编辑

为回答OP的其他问题,这是如何根据要求绘制多个栅格.

In response to OP's additional question, here is how to plot multiple rasters as requested.

假定所有栅格具有相同的范围,分辨率,投影等,则可以将它们堆叠到RasterStack中,然后在堆栈上使用levelplot.您可以将width作为传递给colorkey的列表的元素进行传递,以控制图例的高度(宽度"有点违反直觉,但默认情况下,图例是垂直的).如果要在每个面板上方禁止显示条形标签(如我在下面所做的-默认情况下,它们用堆栈的层名称标记[请参见names(s)]),则可以在传递的列表中添加strip.borderstrip.backgroundpar.settings.

Assuming all rasters have the same extent, resolution, projections, etc., you can stack them into a RasterStack, and then use levelplot on the stack. You can pass width as an element of the list passed to colorkey to control the legend's height ("width" is a little counter-intuitive, but by default legends are vertical). If you want to suppress strip labels above each panel (as I've done below - by default they are labelled with the stack's layer names [see names(s)]), you can add strip.border and strip.background to the list passed to par.settings.

s <- stack(r, r*0.8, r*0.6, r*0.4)
levelplot(s, 
          margin=FALSE,                       
          colorkey=list(
            space='bottom',                   
            labels=list(at=-5:5, font=4),
            axis.line=list(col='black'),
            width=0.75
          ),    
          par.settings=list(
            strip.border=list(col='transparent'),
            strip.background=list(col='transparent'),
            axis.line=list(col='transparent')
          ),
          scales=list(draw=FALSE),            
          col.regions=viridis,                   
          at=seq(-5, 5, len=101),
          names.attr=rep('', nlayers(s))) +           
  layer(sp.polygons(oregon, lwd=3))

这篇关于如何设置使用ggplot2映射栅格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 15:02