Search code examples
rrasterlevelplotrastervis

Plotting raster with just 2 pixels using levelplot function in "rasterVis" package in R


I am trying to plot the following raster using the levelplot function in the "rasterVis) package, but all this gives me is a straight line. I tried to give different breaks to the legend using the "at"command but to no avail. I tried whatever I could in the documentation but couldnt find an answer. Also, the raster is plotting fine with the "plot()" and "image()" command in R. Kindly help.

library(raster)
library(rasterVis)
sr <- "+proj=utm +zone=12 +datum=NAD83 +units=m +no_defs +ellps=GRS80 +towgs84=0,0,0"
r <- raster(resolution =c(24600, 28900), crs = sr, nrows = 1, ncols = 2, ext = extent(c(573800, 623000, 3508713, 3537613)))
r[] <- c(1,2)
levelplot(r, margin = FALSE)    

plot of the output from levelplot

Edit 1 When I define the raster with no extent and projection, then the raster plots fine with levelplot. I am unable to find the reason.

r2 <- raster(nrows =1, ncols =2)
r2[] <- c(1, 2)
levelplot(r2, margin = FALSE)

plot of raster r2


Solution

  • If resolution is used, arguments ncols and nrows are ignored. Remove resolution and nrow to plot it with levelplot.

    library(raster)
    library(rasterVis)
    sr <- "+proj=utm +zone=12 +datum=NAD83 +units=m +no_defs +ellps=GRS80 +towgs84=0,0,0"
    r <- raster(ncol = 2, crs = sr, ext = extent(c(573800, 623000, 3508713, 3537613)))
    r[] <- c(1, 2)
    levelplot(r, margin = FALSE)
    

    enter image description here