Home > Mobile >  Plot raster by nrow and ncol (dimensions) in R
Plot raster by nrow and ncol (dimensions) in R

Time:01-19

I'm having trouble finding documentation on how to change the x and y axes from coordinates (extent) to nrow and ncol (dimension). When I plot a raster:

r1 <- raster(ncol=10, nrow=10, crs="EPSG:4326")
values(r1) <- sample(80:100, 100, replace=TRUE)
plot(r1)

enter image description here

The x and y axes are pulling from crs. I want x and y to be based on the number of rows and columns:

enter image description here

Seems like there should be an obvious solution and I'm just not finding it. I checked enter image description here

r2 <- raster(xmn=0, xmx=10, ymn=0, ymx=10, ncol=10, nrow=10, crs="EPSG:4326")
values(r2) <- sample(80:100, 100, replace=TRUE)
plot(r2) ##Desired Extent of xmn = 0, xmx=10, ymn = 0, ymx = 10 

enter image description here

CodePudding user response:

If you just need the pixel position you could convert your raster to a matrix and plot it using image or rasterVis::levelplot

library(raster)
#> Loading required package: sp
r1 <- raster(ncol=10, nrow=10, crs="EPSG:4326")
values(r1) <- sample(80:100, 100, replace=TRUE)

m <- as.matrix(r1)
image(1:ncol(m), 1:nrow(m), m)

rasterVis::levelplot(m)

Created on 2022-01-18 by the reprex package (v2.0.0)

  •  Tags:  
  • Related