r-tmap / tmap

R package for thematic maps
https://r-tmap.github.io/tmap
GNU General Public License v3.0
856 stars 119 forks source link

does not display correct legend, creates more legend -- when importing and plotting categorical raster #535

Closed ffpinas closed 3 years ago

ffpinas commented 3 years ago

I imported a tiff file (see below) using the raster package and tried to plot this tmap (see attached image below).

each_tif class : RasterLayer dimensions : 7878, 11558, 91053924 (nrow, ncol, ncell) resolution : 5, 5 (x, y) extent : 622655, 680445, 6204975, 6244365 (xmin, xmax, ymin, ymax) crs : +proj=utm +zone=33 +ellps=WGS84 +units=m +no_defs source : M:/marin/nkp16/nmi18/arbetskataloger/habmod/r-project/data/outputs/predictions/classifications/natura2000/nmi18/v11/5m/natura2000_sgu_nmi18_v01_5m.tif names : natura2000_sgu_nmi18_v01_5m values : 1, 3 (min, max) attributes : ID category 0
1 1 2 2 3 3

The problem with my plot is that I get 4 categories instead of just three. – Looking at the attributes in my file, I see that there are four values in the “ID” (i.e., 0,1,2,3) while there are only three in the “Category”. Why is this?

Plotting them using tmap using a labels list of three because there are only three classes (below) would also result in in one extra category (a repeat of the first one) and I get a warning message: “number of legend labels should be 4

I tried to import using the stars package, unfortunately st_read does not support .tif files.

Any thoughts on these would be welcome. Thanks.

problem with legend

Nowosad commented 3 years ago

Just a side note: to read this file using the stars package, you need to use the read_stars() function.

ffpinas commented 3 years ago

Yes, thanks for this... importing using the stars package also led to the same issue as the picture above, i.e., extra legend is added (same as the first one).

mtennekes commented 3 years ago

@ffpinas Could you share the tif file so that I can reproduce it? A cropped version is also fine.

ffpinas commented 3 years ago

attached is the file that I used for the map natura2000_sgu_nmi18_v01_5m.zip

mtennekes commented 3 years ago

Thanks. I do not see the categories when reading the tif file. I've tried both stars::read_stars and raster::raster. Either I don't have the correct drivers, or there are some auxiliary files missing.

ffpinas commented 3 years ago

yes, the categories came from the dbf file also attached (i.e., natura2000_sgu_nmi18_v01_5m.tif.vat.dbf) which I exported using the code below; rat <- read.dbf("M:/marin/nkp16/nmi18/arbetskataloger/habmod/r-project/data/outputs/predictions/classifications/natura2000/nmi18/v11/5m/natura2000_sgu_nmi18_v01_5m.tif.vat.dbf").

this is the code I used to plot the map in tmap;

tm_shape(each_tif)+ tm_raster(n = NROW(rat), title=paste("Natura2000", "Classes"), labels = paste(as.vector(rat$Class_Name), " ", as.numeric(rat$Class_perc), "%", sep = ""))

mtennekes commented 3 years ago

I get this map after running the code:

image

ffpinas commented 3 years ago

I also had a look at the files that was sent--- I think I made a mistake with the dbf file that I sent, it was for another raster file. the correct one is below; I am sorry for that. natura2000_sgu_nmi18_v01_5m.tif.vat.zip using plot() to plot the raster file would show that there are only three classes

This mistake made me realize that when plotting with tmap, using a list of legend that has more classes than what is specified in the raster file would still output a map with the specified legends, which in this case was ten class even if the raster had only three class (this would show if it was plotted using plot()). If using a legend list that has less the number of classes than what is specified in the raster file, tmap would plot the raster and add a legend to compensate for the number of classes.

mtennekes commented 3 years ago

Hope this helps:

tm_shape(each_tif)+
    tm_raster(n = NROW(rat), title=paste("Natura2000", "Classes"), labels = paste(as.vector(rat$Class_Name), " ", as.numeric(rat$Class_perc), "%", sep = ""))

# uses the same color classes as:
tm_shape(each_tif)+
    tm_raster(n = 3)

# using categories instead of the default color classes:
tm_shape(each_tif)+
    tm_raster(n = 3, style = "cat")

# with meta
tm_shape(each_tif)+
    tm_raster(n = NROW(rat), style = "cat", title=paste("Natura2000", "Classes"), labels = paste(as.vector(rat$Class_Name), " ", as.numeric(rat$Class_perc), "%", sep = ""))
ffpinas commented 3 years ago

Yes, this is the solution I was looking for!!! thanks so much!!!