0

I would like to plot a world map with countries colored based on their values. I found here Plot map with values for countries as color in R? the second answer can be a good solution for me. Especially, I want to reproduce the second plot in the second answer with a continuous color legend (see below), but I didn't succeed. In the plotme function I do not know where to add the following code:

gg <- gg + scale_fill_gradient2(low = pal[1],
                                mid = pal[palSz/2],
                                high = pal[palSz],
                                midpoint = (max(ddf$value) + min(ddf$value)) / 2,
                                name="value")

I tried to replace the following code with the above one but got error "Discrete value supplied to continuous scale":

gg <- gg + scale_fill_manual(values=colorRampPalette(brewer.pal(9, 'Reds'))(length(ddf$value)), 
                               name="Country")

Can anyone help to reproduce the plot?

enter image description here

yliueagle
  • 1,191
  • 1
  • 7
  • 22

1 Answers1

1

You add it instead of the previous fill scale (scale_fill_manual). But in addition you need to replace the statement fill=brk with fill=value. Then it should work. Altogether the new function will look like this (mostly copied the linked answer):

library(RColorBrewer)
library(maptools)
library(ggplot2)

data(wrld_simpl)

ddf = read.table(text="
                 country value
                 'United States' 10
                 'United Kingdom' 30
                 'Sweden' 50
                 'Japan' 70
                 'China' 90
                 'Germany' 100
                 'France' 80
                 'Italy' 60
                 'Nepal' 40
                 'Nigeria' 20", header=TRUE)

# Pascal had a #spiffy solution that is generally faster

plotPascal <- function() {

  pal <- colorRampPalette(brewer.pal(9, 'Reds'))(length(ddf$value))
  pal <- pal[with(ddf, findInterval(value, sort(unique(value))))]

  col <- rep(grey(0.8), length(wrld_simpl@data$NAME))
  col[match(ddf$country, wrld_simpl@data$NAME)] <- pal

  plot(wrld_simpl, col = col)

}

plotme <- function() {

  # this lets us use the contry name vs 3-letter ISO
  wrld_simpl@data$id <- wrld_simpl@data$NAME

  wrld <- fortify(wrld_simpl, region="id")
  wrld <- subset(wrld, id != "Antarctica") # we don't rly need Antarctica

  gg <- ggplot()

  # setup base map
  gg <- gg + geom_map(data=wrld, map=wrld, aes(map_id=id, x=long, y=lat), fill="white", color="#7f7f7f", size=0.25)

  # add our colored regions
  gg <- gg + geom_map(data=ddf, map=wrld, aes(map_id=country, fill=value),  color="white", size=0.25)

  # this sets the scale and, hence, the legend
  pal <- colorRampPalette(brewer.pal(9, 'Reds'))(length(ddf$value))
  palSz <- 10 # not sure what you really want/need for this range  
  gg <- gg + scale_fill_gradient2(low = pal[1],
                                  mid = pal[palSz/2],
                                  high = pal[palSz],
                                  midpoint = (max(ddf$value) + min(ddf$value)) / 2,
                                  name="value")

  # this gives us proper coords. mercator proj is default
  gg <- gg + coord_map()
  gg <- gg + labs(x="", y="")
  gg <- gg + theme(plot.background = element_rect(fill = "transparent", colour = NA),
                   panel.border = element_blank(),
                   panel.background = element_rect(fill = "transparent", colour = NA),
                   panel.grid = element_blank(),
                   axis.text = element_blank(),
                   axis.ticks = element_blank(),
                   legend.position = "right")
  gg

}

plotme()
shadow
  • 21,823
  • 4
  • 63
  • 77