问题
I'd like to plot a map of chlorophyll concentration, but the values are dispersed in such a way that the legend becomes unreadable (see picture)
So I am trying to control the spacing of the colours in the colour bar/legend. I'd like to get it evenly spaced (while keeping the uneven breaks on the map itself).
The the example below is much simplified and is for a raster plot, but the same goes for image.plot.
library(raster)
r <- raster(ncol=5, nrow=4)
r[] <- 1:20
plot(r, breaks = c(0,1,2,3,5,10,20), col = rainbow(6))
I thought about converting the data to log values, but that doesn't give me a satisfying result. So any help with the legend is much appreciated.
回答1:
It's ugly, but you can use plot
for the map and image.plot
for your legend. You will need to add the labels of your custom breaks to the position of the equally spaced breaks.
Plot map without legend:
library(raster)
r <- raster(ncol=5, nrow=4)
r[] <- 1:20
my_breaks = c(0,1,2,3,5,10,20)
n = 6
my_col = rainbow(n)
plot(r, breaks = my_breaks, col = my_col, legend = FALSE, zlim=c(0,20))
The default breaks will be equally spaced from 0 to 20:
def_breaks = seq(0,20,length.out=(n+1))
Add legend using image.plot
from the fields
package, placing custom break labels at default break positions:
library(fields)
image.plot(r, zlim = c(0,20),
legend.only = TRUE,
col = my_col,
axis.args = list(at = def_breaks, labels = my_breaks))
来源:https://stackoverflow.com/questions/19061492/r-how-to-control-spacing-of-colour-bar-legend