4

I am creating a map using tmap with a quantitative variable that I wish to use for setting the polygon colours. When I set the colour ramp using a standard RColorBrewer palette or with a custom vector of colours, it is only using half of the values in the colour ramp (but I want them all!). For example, with the "RdYlGn" palette, it is only plotting from yellow to green without any reds.

Let's take the example of the meuse dataset (I am taking the start of this example from this answer (Mapping several maps with the same colorscheme in tmap))

library(sp)
library(tmap)
library(rgeos) # for the gBuffer function in this example

data(meuse, meuse.area)       

coordinates(meuse) <- ~x + y 

meuse.area <- SpatialPolygons(list(Polygons(list(Polygon(meuse.area)),'1')))

proj4string(meuse)<- CRS("+init=epsg:28992")
proj4string(meuse.area)<- CRS("+init=epsg:28992")

#turn it into a spatial polygon for my purposes

meuse.poly=gBuffer(meuse, width=100, byid=TRUE)

tm_shape(meuse.poly)+
    tm_polygons("dist", title="dist",
         palette="RdYlGn", n=6)

Here's the map with this example:

enter image description here

But I don't get the full spectrum of colours:

enter image description here

6

You need auto.palette.mapping=FALSE in tm_polygon().

tm_shape(meuse.poly) +
tm_polygons("dist", title="dist", palette="RdYlGn",                 
            auto.palette.mapping=FALSE, n=6)

plot

See the help page (argument auto.palette.mapping):

auto.palette.mapping

When diverging colour palettes are used (i.e. "RdBu") this method automatically maps colors to values such that the middle colors (mostly white or yellow) are assigned to values of 0, and the two sides of the color palette are assigned to negative respectively positive values. When categorical color palettes are used, this method stretches the palette if there are more levels than colors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.