Closed ffpinas closed 3 years ago
Just a side note: to read this file using the stars package, you need to use the read_stars()
function.
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).
@ffpinas Could you share the tif file so that I can reproduce it? A cropped version is also fine.
attached is the file that I used for the map natura2000_sgu_nmi18_v01_5m.zip
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.
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 = ""))
I get this map after running the code:
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.
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 = ""))
Yes, this is the solution I was looking for!!! thanks so much!!!
I imported a tiff file (see below) using the raster package and tried to plot this tmap (see attached image below).
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.