【问题标题】:Raster map with discrete color scale for negative and positive values R带有离散色标的光栅图,用于负值和正值 R
【发布时间】:2016-02-18 08:20:50
【问题描述】:

我有两个要映射的数据框。 dfs 具有相同的 xy 坐标,我需要一个 single colorbar 和一个可见的 discrete color scale 用于两个 dfs,就像这里显示的那样。我希望颜色键中的颜色与自定义中断相匹配。非常感谢可以在此示例之外应用的更通用的解决方案

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

到目前为止我的代码:

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())

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

非常感谢您的帮助。您的建议将帮助我开发许多这样的地图。

谢谢。

【问题讨论】:

  • @OscarPerpiñán 来自常见问题解答ras1 &lt;- raster(nrow=10,ncol=10) set.seed(1) ras1[] &lt;- rchisq(df=10,n=10*10) ras2=ras1*(-1)/2 s &lt;- stack(ras1,ras2) Uniques &lt;- cellStats(s,stat=unique) Uniques.max &lt;- max(Uniques),Uniques.min &lt;- min(Uniques) my.at &lt;- round(seq(ceiling(Uniques.max), floor(Uniques.min), length.out= 10),0); myColorkey &lt;- list(at=my.at, labels=list(at=my.at)) levelplot(s, at=my.at, colorkey=myColorkey,par.settings=RdBuTheme()),但您可以看到 0 不是我上面显示的示例地图所示的位置。我该如何解决这个问题?非常感谢。
  • 在 cmets 中,您可以在语句之间使用分号 (;),以便您的代码可以运行。
  • @RobertH 感谢您的有用编辑。这个问题的一些答案可以在这里找到 [stackoverflow.com/questions/33750235/…
  • ggplot2 更适合这个(使用 facet_wrap 或 facet_grid),有兴趣吗?

标签: r colors maps raster levelplot


【解决方案1】:

以下内容应该可以帮助您前进。借助 ggplot2 文档和许多在线示例,您应该能够调整美学以使其看起来完全符合您的要求,而不会遇到任何麻烦。干杯。

#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))

【讨论】:

  • 您提供的非常有见地的答案。非常感谢。我想应该可以将 0 放在 7 的位置?在典型的发散调色板中,0 应该将红色与蓝色分开。
  • 我上面的回答仅涵盖原始问题的范围;-)。至于发散的调色板,您似乎已经问过这个here 并且得到了不止一个好的答案。
猜你喜欢
  • 2014-09-24
  • 1970-01-01
  • 1970-01-01
  • 2020-09-24
  • 1970-01-01
  • 1970-01-01
  • 2021-06-27
  • 1970-01-01
  • 2016-05-10
相关资源
最近更新 更多