Home > other >  'x' is a list, but does not have components 'x' and 'y' in unsupervise
'x' is a list, but does not have components 'x' and 'y' in unsupervise

Time:10-21

I'm trying to plot the output of a kmeans analysis on a raster stack on r and I got returned the error in the title. This is my output raster:

    unsuperClass results

    *************** Map ******************
    $map
    class      : RasterLayer 
    dimensions : 48219, 90691, 4373029329  (nrow, ncol, ncell)
    resolution : 10, 10  (x, y)
    extent     : 3909190, 4816100, 2404810, 2887000  (xmin, xmax, ymin, ymax)
    crs        :  proj=laea  lat_0=52  lon_0=10  x_0=4321000  y_0=3210000  ellps=GRS80  units=m  no_defs 
    source     : r_tmp_2021-10-19_154600_2028_56752.grd 
    names      : layer 
    values     : 1, 4  (min, max)

As you can see, to do the analysis I used the "unsuperClass" function in RStoolbox. And the code that returns the error:

    plot(unCrasterresult)
    Error in xy.coords(x, y, xlabel, ylabel, log) : 
      'x' is a list, but does not have components 'x' and 'y'

I know I need to call something like

    plot(unCrasterresult$x, unCrasterresult$y)

and set limits but I don't know exactly how to structure the code.

Edit: I've also tried this code but to no avail

    plot(unCrasterresult, xlim=c(3909190, 4816100), ylim=c(2404810, 2887000))

CodePudding user response:

If one searches the web for "unsuperClass RStoolbox" the first hit is:

https://www.rdocumentation.org/packages/RStoolbox/versions/0.2.6/topics/unsuperClass

So we can deduce that you have installed and loaded the RStoolbox package. Looking at the documentation for the function being used we see an example:

library(raster)
input <- brick(system.file("external/rlogo.grd", package="raster"))

## Plot 
olpar <- par(no.readonly = TRUE) # back-up par
par(mfrow=c(1,2))
plotRGB(input)

## Run classification
set.seed(25)
unC <- unsuperClass(input, nSamples = 100, nClasses = 5, nStarts = 5)
unC

## Plots
colors <- rainbow(5)
plot(unC$map, col = colors, legend = FALSE, axes = FALSE, box = FALSE)
legend(1,1, legend = paste0("C",1:5), fill = colors,
       title = "Classes", horiz = TRUE,  bty = "n")

par(olpar) # reset par

So the map element of such an object is an S4 classed object with a class of "RasterLayer":

str(unC$map)
Formal class 'RasterLayer' [package "raster"] with 12 slots
  ..@ file    :Formal class '.RasterFile' [package "raster"] with 13 slots
  .. .. ..@ name        : chr ""
  .. .. ..@ datanotation: chr "FLT4S"
  .. .. ..@ byteorder   : chr "little"
  .. .. ..@ nodatavalue : num -Inf
  #--- abbreviated the console output, note that this is an S4 object

This shows you might have success with:

plot(unCrasterresult$map)

There must be a base plot method defined for an S4 object of class "RasterLayer" in either the RStoolbox or raster package. My comment regarding the character of the package being new or obscure was based on my not finding any prior SO mention of that class, but the package has been around since 2015 and is being actively maintained. If you had posted on the SE site for GIS questions you might have gotten an immediate answer from a current user who knew that function and package. See: https://gis.stackexchange.com/search?q=unsuperClass . There is also a R-SIG spatial mailing list: r-sig-geo.

CodePudding user response:

Please include a self-contained reproducible example when you ask a R question. That makes is much easier to see what goes wrong, and to provide help.

You need to load the raster package to be able to plot a RasterLayer. So something like this:

library(raster)
plot(unsuperClasresult$map)
  • Related