Search code examples
rgeospatialrasterterra

How should I solve misaligned colorbar ticks in the Raster package?


I'm trying to use the raster package to visualize GeoTIFF files. I have to say that this is a good choice because I like its colorbar style better than the terra package. But at the same time, I also discovered a problem: in its output results, the ticks and breaks (or color blocks) of the colorbar are not aligned.I sincerely hope that this question can be answered, and I also try to I looked for help in the help documentation and search engines, but with little success.

This is my output:Rater plot

When we look carefully at the colorbar:misaligned colorbar ticks

This is the code I used:

library(raster)

set.seed(123)

# Define a color palette
mycolorbar <- colorRampPalette(c(
  "#fb6203",
  "#ef9c35",
  "#f4d095",
  "#ffffff",
  "#78c6ec",
  "#349dfb",
  "#035ba8"
))

dummy_raster <- raster(matrix(nrow = 10,ncol = 10))
values(dummy_raster) <- runif(100, min = -1, max = 1)


# Plot and save as PDF
pdf(file = "mytest01082.pdf", width = 5, height = 6)

plot(dummy_raster,
     breaks = seq(-1, 1, 0.1),
     col = mycolorbar(20),
     horizontal = TRUE,
     legend.width = 1,
     legend.shrink = 1,
     axis.args = list(
       at = seq(-1, 1, 0.5),
       tcl = 0.3
     ))

dev.off()

I'm wondering if some simple parameter settings can solve this problem. These misaligned ticks can cause some misunderstandings. In addition, I also know that the terra package will soon replace the raster package in the future, and I also really hope to get some help in using the terra package to achieve the colorbar style I want.

My sincerest thanks for your help!


Solution

  • "raster" shows a continuous legend even if you have classes because you use breaks. I believe "terra" behaves better by using an "interval" legend. But you can override that default.

    Your example data (with fewer breaks):

    library(terra)   
    set.seed(123)
    x <- rast(matrix(runif(100, min=-1, max=1), nrow=10, ncol=10))
    mycolorbar <- colorRampPalette(c("#fb6203", "#ef9c35", "#f4d095", "#ffffff", "#78c6ec", "#349dfb", "#035ba8"))
    brks <- seq(-1, 1, 0.5)  
    cols <- mycolorbar(length(brks)-1)
    

    The default "interval" legend

    plot(x, breaks=brks, col=cols, mar=c(2,2,2,6), plg=list(cex=1.1))
    

    enter image description here

    You can put the legend below the map

    plot(x, breaks=brks, col=cols, mar=c(6,2,2,2),
        plg=list(x=0, y=-1, horiz = TRUE, cex=1.2))
    

    enter image description here

    But you can also use a continuous legend, put it below the map, and set the legend tics.

    plot(x, type="continuous", breaks=brks, col=cols, mar=c(6,2,2,2),
        plg=list(x="bottom"))
    

    enter image description here

    If you want to specify the labels you want, for example because you have more breaks and want specific labels, you can do this:

    brks2 <- seq(-1, 1, 0.1)     
    cols2 <- mycolorbar(length(brks2)-1)
    plot(x, type="continuous", breaks=brks2, col=cols2, mar=c(6,2,2,2),
        plg=list(at=brks, labels=brks, x="bottom"))
    

    enter image description here

    With terra 1.7-69 you can change the default tic-mark style to "through", "in", "out", or "none". For example:

    plot(x, type="continuous", breaks=brks2, col=cols2, mar=c(6,2,2,2),
        plg=list(at=brks, labels=brks, x="bottom", tic="out"))
    

    terra 1.7-69 is currently the development version. You can install it with

    install.packages('terra', repos='https://rspatial.r-universe.dev')