【问题标题】:why when we use rev to reverse the colour of legend we get something different?为什么当我们使用 rev 来反转图例的颜色时,我们会得到不同的东西?
【发布时间】:2013-01-30 15:02:54
【问题描述】:

我正在尝试绘制此光栅文件的图像。图例的颜色是这样的:低值用红色,高值用绿色。

但通常红色与高值相关联。这就是为什么我然后使用rev 来反转图例的颜色。但是我得到了一些不同的东西。是否还有其他功能可以反转图例的颜色并保持颜色不变。

require(raster)
require(fields)
r = raster(y)
extent(r) = extent(c(xmn=-180,xmx=180,ymn=-90,ymx=90))
plot(r, col = rainbow(20, s = 1, v = 1, start = 0, 
     end = 1),lab.breaks=seq(0,0.6,0.05),
     breaks=seq(0,0.6,0.05), zlim=c(0.0,0.6),horizontal = TRUE, 
     xlab="Longitude", ylab="Latitude",legend.shrink = 0.9,
     legend.width = 1.2)

然后我用这个给了我:

plot(r, col = rev(rainbow(20, s = 1, v = 1, start = 0, end = 1)),
     lab.breaks=seq(0,0.6,0.05),breaks=seq(0,0.6,0.05),
     zlim=c(0.0,0.6),horizontal = TRUE, xlab="Longitude", 
     ylab="Latitude",legend.shrink = 0.9,legend.width = 1.2)

【问题讨论】:

    标签: r plot raster


    【解决方案1】:

    显然你没有检查颜色。

    > length(rainbow(20, s = 1, v = 1, start = 0, end = 1))
    [1] 20
    

    但不是全部用了 20 个,你只用了其中的 12 个,所以:

    plot(r, col = rev(rainbow(20, s = 1, v = 1, start = 0, end = 1)[1:12]),
         lab.breaks=seq(0,0.6,0.05),breaks=seq(0,0.6,0.05),
         zlim=c(0.0,0.6),horizontal = TRUE, xlab="Longitude", 
         ylab="Latitude",legend.shrink = 0.9,legend.width = 1.2)
    

    应该给你你想要的情节。毕竟,你有 13 个中断值,所以只有 12 个类别。这意味着你可以做到:

    plot(r, col = rev(rainbow(12, s = 1, v = 1, start = 0, end = 1)),
         lab.breaks=seq(0,0.6,0.05),breaks=seq(0,0.6,0.05),
         zlim=c(0.0,0.6),horizontal = TRUE, xlab="Longitude", 
         ylab="Latitude",legend.shrink = 0.9,legend.width = 1.2)
    

    【讨论】:

      猜你喜欢
      • 2019-06-22
      • 2011-11-11
      • 2013-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-28
      • 2021-08-11
      相关资源
      最近更新 更多