使用灰度的光栅绘图:0和1均以白色绘制

时间:2016-12-20 18:04:50

标签: r plot

我有以下示例代码:

library(raster)
library(SpaDES)

m = matrix(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0.1, 0.2, 0.3, 0.4, 0.4, 0.3, 0.2, 0.1, 0,
         0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
         0.4, 0.3, 0.2, 0.1, 0, 0.1, 0.2, 0.3, 0.4, 0.5),nrow=4,
         ncol=10, byrow=TRUE )
r <- raster(m)
Plot(r, cols = grey.colors(10, start=0, end=1), title = "4x10 Raster")

这应生成一个4x10光栅,其中每个0代表一个黑色正方形,1代表一个白色正方形,其他数字代表灰色正方形。不幸的是,一个和零(以及背景)都是白色的。如何将零显示为黑色方块?

Raster with white instead of black

编辑:我正在使用R包&#34; SpaDES&#34;对于生态应用,Plot是该包的功能。非常重要的是,无论绘图查看器的形状或栅格的大小,栅格方块都被绘制为正方形,因为它代表了一个光栅图。

感谢您的帮助。

3 个答案:

答案 0 :(得分:2)

我这样做了:

plot(r, col = grey.colors(10, start=0, end=1), main = "4x10 Raster")

得到了这个:

enter image description here

Plot更改为plot,将cols更改为col,将title更改为main

答案 1 :(得分:0)

来自@Derek Corcoran的纠正。我添加了一些代码来保留栅格的宽高比。

//DialogControl.cs
bool _closed = false;
DialogResult _result;

DialogResult ShowModal()
{
    this.Show();
    while(!_closed) Application.DoEvents();  //Infinite loop
    return _result;
}

public void OkButton_OnClick(EventArgs e, object sender)
{
    _result = DialogResult.OK;
    _closed = true;
}

public void CancelButton_OnClick(EventArgs e, object sender)
{
    _result = DialogResult.Cancel;
    _closed = true;
}

enter image description here

答案 2 :(得分:0)

因此,在使用Plot包而不是SpaDES的{​​{1}}函数时解决问题的方法似乎如下:

plot

raster plot with zeros plotted in black

我仍然不明白为什么更改library(raster) library(SpaDES) m = matrix(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.1, 0.2, 0.3, 0.4, 0.4, 0.3, 0.2, 0.1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0.4, 0.3, 0.2, 0.1, 0, 0.1, 0.2, 0.3, 0.4, 0.5),nrow=4, ncol=10, byrow=TRUE ) r <- raster(m) Plot(r, cols = grey.colors(10, start=0, end=1), title = "4x10 Raster", na.color = "black") 的选项不起作用。以下是来自zero.color的{​​{1}}函数的定义:

Plot