具有负和正值R的离散色标的栅格图 [英] Raster map with discrete color scale for negative and positive values R

查看:192
本文介绍了具有负和正值R的离散色标的栅格图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个要映射的数据框. df具有相同的xy坐标,因此我需要一个单个颜色栏,并且对于这两个df,都需要一个可见的离散色标,如下所示.我希望colorkey中的颜色与自定义的中断相匹配.可以在此示例之外应用的更通用的解决方案,我们将不胜感激

I have two dataframes which I will like to map. The dfs have the same xy coordinates and I need a single colorbar with a visible discrete color scale for both dfs like the one shown here. I would like the colors in the colorkey to match the self-defined breaks. a more general solution that can be applied outside this example is much appreciated

我追求的是 RcolorBrewer 包中的RdYIBu调色板.

The RdYIBu color palette from the RcolorBrewer package is what I am after.

到目前为止,我的代码:

My code so far:

library(rasterVis)
ras1 <- raster(nrow=10,ncol=10) 
set.seed(1) 
ras1[] <- rchisq(df=10,n=10*10) 
ras2=ras1*(-1)/2 
s <- stack(ras1,ras2) 
Uniques <- cellStats(s,stat=unique) 
Uniques.max <- max(Uniques)
Uniques.min <- min(Uniques)
my.at <- round(seq(ceiling(Uniques.max), floor(Uniques.min), length.out= 10),0)
myColorkey <- list(at=my.at, labels=list(at=my.at)) 
levelplot(s, at=my.at, colorkey=myColorkey,par.settings=RdBuTheme())

如何如上面的示例图所示,在colorkey中设置值以匹配图上的值?请注意,colorkey中的颜色数应与地图上显示的颜色数相同.

How can I set the values in the colorkey to match values on the map as shown on the sample map above? Note that the number of colors in the colorkey should be the same number shown on the map.

非常感谢您的帮助.您的建议将帮助我开发许多此类地图.

Many thanks for your help. Your suggestions will help me to develop many such maps.

谢谢.

推荐答案

以下内容将助您一臂之力.借助ggplot2文档和许多在线示例,您应该能够调整外观,使其完全符合您的期望,而不会遇到任何麻烦.

The following should get you going. With the ggplot2 documentation and the many online examples,you should be able to tweak the aesthetics to get it to look exactly as you want without any troubles.Cheers.

#Order breaks from lowest to highest
  my_at <- sort(my_at)

#Get desired core colours from brewer
  cols0 <- brewer.pal(n=length(my_at), name="RdYlBu")

#Derive desired break/legend colours from gradient of selected brewer palette
  cols1 <- colorRampPalette(cols0, space="rgb")(length(my_at))

#Convert raster to dataframe
  df <- as.data.frame(s, xy=T)
  names(df) <- c("x", "y", "Epoch1", "Epoch2")

#Melt n-band raster to long format
  dfm <- melt(df, id.vars=c("x", "y"), variable.name="epoch", value.name="value")

#Construct continuous raster plot without legend
  #Note usage of argument `values` in `scale_fill_gradientn` -
  #-since your legend breaks are not equi-spaced!!!
  #Also note usage of coord_equal()
  a  <- ggplot(data=dfm, aes(x=x, y=y)) + geom_raster(aes(fill=value)) + coord_equal()+
        facet_wrap(facets=~epoch, ncol=1) + theme_bw() + 

        scale_x_continuous(expand=c(0,0))+
        scale_y_continuous(expand=c(0,0))+
        scale_fill_gradientn(colours=cols1,
                             values=rescale(my_at),
                             limits=range(dfm$value),
                             breaks=my_at) +
        theme(legend.position="none", panel.grid=element_blank())

#Make dummy plot discrete legend whose colour breaks go along `cols1`
  df_leg <- data.frame(x=1:length(my_at), y=length(my_at):1, value=my_at)
  gg_leg <- ggplot(data=df_leg, aes(x=x, y=y)) + geom_raster(aes(fill=factor(value))) +
            scale_fill_manual(breaks=my_at, values=cols1,
                              guide=guide_legend(title="",
                                                 label.position="bottom")) +
            theme(legend.position="bottom")

#Extract discrete legend from dummy plot
  tmp <- ggplot_gtable(ggplot_build(gg_leg))
  leg <- which(sapply(tmp$grobs, function(x) x$name)=="guide-box")
  legend <- tmp$grobs[[leg]]

#Combine continuous plot of your rasters with the discrete legend
  grid.arrange(a, legend, ncol=1, heights=c(4, 0.8))

这篇关于具有负和正值R的离散色标的栅格图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆