Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot map with values for countries as color in R?

I have following simple example data which I want to plot on a map with gradient color corresponding to value of the given country.

ddf = read.table(text="
country value
USA 10
UK 30
Sweden 50
Japan 70
China 90
Germany 100
France 80
Italy 60
Nepal 40
Nigeria 20
", header=T)

On google search, I found several sites. However, I am looking for code which is small and clear, and should preferably be fast (I found ggplot methods to be relativley slow). The resolution of world map need not be high.

I tried following code:

library(maptools)
data(wrld_simpl)
plot(wrld_simpl)

Specific nations can be colored as given on : Using [R] maps package - colouring in specific nations on a world map Using the command:

plot(wrld_simpl, col = c(gray(.80), "red")[grepl("^U", wrld_simpl@data$NAME) + 1])

But how can I get map with above data in a gradient of colors. Thanks for your help.

like image 308
rnso Avatar asked Jun 10 '14 09:06

rnso


People also ask

How do I create a world map in R?

To create a world map using it we will use the geom_map() function of the ggplot2 package of the R Language. This function returns a ggplot object so all the functions that work on other ggplot plots will be working in geom_map() too. where, data: determines the data be displayed in this layer.

Why are world maps color coded?

In all map-making the colours are chosen to highlight boundaries and make the map look appealing, rather than the sort of colour coding device that you'd find in an info-graphic.


2 Answers

I thought the other answers were slightly complicated, perhaps because this was asked/answered a relatively long time ago? Here's a simple way using ggplot2. I don't know what the benchmark is with respect to whether or not this is "fast".

library(ggplot2)
library(dplyr)

ddf = read.table(text="
country value
USA 10
UK 30
Sweden 50
Japan 70
China 90
Germany 100
France 80
Italy 60
Nepal 40
Nigeria 20
", header=T)

world <- map_data("world")

world %>%
  merge(ddf, by.x = "region", by.y = "country", all.x = T) %>%
  arrange(group, order) %>%
  ggplot(aes(x = long, y = lat, group = group, fill = value)) + geom_polygon()

world map, colored by value

This makes it easy to modify using all the common ggplot2 handles. For example, if we wanted to pretty this up quickly:

library(viridis)

world %>%
  merge(ddf, by.x = "region", by.y = "country", all.x = T) %>%
  arrange(group, order) %>%
  ggplot(aes(x = long, y = lat, group = group, fill = value)) +
  geom_polygon(color = "white", size = 0.2) +
  scale_fill_viridis("", na.value = "gray90") +
  theme_minimal() +
  theme(axis.text = element_blank(),
        axis.title = element_blank(),
        panel.grid = element_blank())

prettier map with viridis color scale, no axis titles, etc.

like image 171
Hendy Avatar answered Sep 18 '22 18:09

Hendy


You could use rworldmap if you wanted less code and a coarser resolution map.

library(rworldmap)

#create a map-shaped window
mapDevice('x11')
#join to a coarse resolution map
spdf <- joinCountryData2Map(ddf, joinCode="NAME", nameJoinColumn="country")

mapCountryData(spdf, nameColumnToPlot="value", catMethod="fixedWidth")

Default categorisation, colours and legends can be altered, see this RJournal paper.

It would be faster with country codes rather than names.

rworldmap map

like image 40
Andy Avatar answered Sep 20 '22 18:09

Andy