Let's say I want to plot the World map with countries colored by population estimate, but I want the color ramp to start and stop at specified values (for instance, maybe I want to use one legend for several maps).
library(tmap)
data(World)
# custom color ramp
colorRamp <- colorRampPalette(c('blue', 'yellow', 'red'))
# I can plot the full range of values with my custom palette
tm_shape(World) + tm_fill('pop_est', palette = colorRamp(100))
Now let's say I want to specify custom lower and upper bounds:
customRange <- c(200, 700)
breaks <- seq(from = customRange[1], to = customRange[2], length.out = 100)
tm_shape(World) + tm_fill('pop_est', palette = colorRamp(100), breaks = breaks)
This clearly didn't work, and the legend also has problems. I get the following warnings:
Warning messages:
1: Values have found that are less than the lowest break
2: Values have found that are higher than the highest break
Any suggestions as to the proper way to do this? Thanks!
EDIT:
Further elaboration: In this example, the data I am plotting ranges from ~ 140 million to 1.4 billion. But let's say I have a 2nd map that ranges from 500 million to 5 billion. If I want the two maps to have the same color scheme, and have a single legend for both, then I would want the color palette for BOTH maps to span 140 million to 5 billion.
So, how can I specify the min and max value that the color palette will span?