質問

ピクセル値をプロットに追加する方法は?click()を使用して値を取得できますが、プロットに表示させてください。

library(raster)
r <- raster(nrow=3, ncol=3)
r[] <- 1:ncell(r)
plot(r)
click(r) 
.

Enter Image説明

役に立ちましたか?

解決

次のことを試してください。 getMethod("click", signature="Raster")

myClick <- function(x, n = Inf, id = FALSE, xy = FALSE, cell = FALSE, 
                    type = "n", show = TRUE, ...) {
    i <- 0
    n <- max(n, 1)
    while (i < n) {
        i <- i + 1
        loc <- locator(1, type, ...)
        xyCoords <- cbind(x = loc$x, y = loc$y)
        cells <- na.omit(cellFromXY(x, xyCoords))
        if (length(cells) == 0)
              break
        value <- extract(x, cells)
        text(xyCoords, labels = value)
    }
}

## Try it out
myClick(r, n=4)
.

他のヒント

textメソッドを使用できるすべての値を表示する場合:

library(raster)
r <- raster(nrow=3, ncol=3, vals=1:9)
plot(r)
text(r)
.

サブセットの場合は、次のようなことができます。

z <- rasterToPoints(r, function(x) x > 6 )
plot(r)
text(z[,1], z[,2], z[,3])
.

私はこの質問がすでに答えとしてマークされていますが、JoshのソリューションとEddieのフォローアップの質問に基づいています。):

r <- raster(nrow=3, ncol=3)
r[] <- runif(ncell(r))

plot(r)

for(i in 1:ncell(r)){
 xycoords <- xyFromCell(r, cell = i)
 value <- extract(r, xycoords)
 text(xycoords, labels = round(value))
}
.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top