rrworldmap

Assigning a Specifically Defined Color Scale to a World Map


I have a list of 7 countries with a specific score for each one respectively. Using this code, the color scale starts at the lowest score (0.1367) and ends at the highest score (0.3760). I would like the scale to start at a lower value such as 0.08 and stop at a higher value such as 0.4, since e.g. 0.1367 is still a fairly good score but now displays blue since it is the lowest score in the data-set.

How can I adjust the color scale to start at a lower value that is not included in my data-set?

This is my code:

library(rworldmap)
library(RColorBrewer)

d = read.table(text="
country score
Italy 0.3760
Belgium 0.1431
France 0.2028
Netherlands 0.1431
Australia 0.1411
Germany 0.1431
'United States' 0.1367
", header=T)

numCats <- 100
palette = colorRampPalette(c('blue','lightblue','green', 'yellow', 'red')) 
(numCats)
dt <- joinCountryData2Map(d, joinCode="NAME", nameJoinColumn="country")

map18 = mapCountryData(dt, nameColumnToPlot="score", 
catMethod="fixedWidth",numCats = numCats,colourPalette = palette, mapTitle = 
"AHP Score Obtained",missingCountryCol = 'grey',addLegend=FALSE)


do.call(addMapLegend
        ,c(map18
           ,horizontal=TRUE
           ,legendWidth=0.5))

worldmap_image


Solution

  • You have to manually set the range at which you want values to be specified beforehand. After doing that, split your range into an equal number of classes (in your case you have 100, meaning you want 101 numbers or labels) to make a continuous sequence.

    limits <- range(0.08, 0.40)  # custom range
    legend.limits <- seq(min(limits), max(limits), len = 101)  # splitting to make continuous variable
    

    We then manually modify catMethod in the mapCountryData function to reflect our custom range:

    map18 <- mapCountryData(dt, nameColumnToPlot="score",
                            catMethod= legend.limits,  # custom range for legend
                            numCats = numCats,
                            colourPalette = "rainbow",  # using default colors
                            mapTitle = "AHP Score Obtained",
                            missingCountryCol = 'grey',
                            addLegend=FALSE)
    
    do.call(addMapLegend, c(map18, horizontal = TRUE,
                            legendWidth = 0.5,
                            legendLabels = "limits"))
    

    enter image description here

    Note that I also changed the colourPalette argument to a default rainbow scale. You originally specified the palette variable which specifies 5 colors, but because we now have 100+ classes, colourPalette will need to use one of the defaults.