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.
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.
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.
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()
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())
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With