Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specifying correct raster labels #607

Closed
Nowosad opened this issue Oct 20, 2021 · 3 comments
Closed

Specifying correct raster labels #607

Nowosad opened this issue Oct 20, 2021 · 3 comments

Comments

@Nowosad
Copy link
Member

Nowosad commented Oct 20, 2021

Often, when you crop existing categorical raster data, the new object does not contain all of the original categories. This could be a problem when plotting the raster using some custom palettes and labels. Custom palettes still could be used in this situation by setting the raster values as names of the palette vector. However, the same operation does not work for labels. You can see some examples below, for data with values between 1 and 7 with 5 missing.

library(stars)
library(tmap)
tf = tempfile(fileext = ".zip")
td = tempdir()
download.file("https://github.com/r-tmap/tmap/files/7380213/tmc.zip", tf)
unzip(tf, exdir = td)
lc = read_stars(paste0(td, "/lc.tif"))
lc_palette_df = read.csv(paste0(td, "/legend.csv"))

# wrong colors for 6 and 7
tm_shape(lc) +
  tm_raster(style = "cat", palette = lc_palette_df$color)

# correct colors
my_color = lc_palette_df$color
names(my_color) = lc_palette_df$value
tm_shape(lc) +
  tm_raster(style = "cat", palette = my_color)

# wrong labels for 6 and 7
tm_shape(lc) +
  tm_raster(style = "cat", palette = lc_palette_df$color,
            labels = lc_palette_df$label)

# the fix for colors does not work for labels
my_label = lc_palette_df$label
names(my_label) = lc_palette_df$value
tm_shape(lc) +
  tm_raster(style = "cat", palette = my_color,
            labels = my_label)

tmc.zip

CC: @blakos1

@mtennekes
Copy link
Member

It should work now. Have to check if this occurs in tmap4 as well.

@mtennekes mtennekes mentioned this issue Oct 21, 2021
43 tasks
@Nowosad
Copy link
Member Author

Nowosad commented Oct 22, 2021

Thanks!

@Nowosad
Copy link
Member Author

Nowosad commented Oct 22, 2021

@blakos1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants