Plot colour coded world map using ggplot2
Asked Answered
M

1

5

I am trying to generate a generate a plot of a world map where the colour of each country corresponds to a particular value stored in a data frame.

> aggregated_country_data
   country num_responses                                     region
1       AL             1                                    Albania
2       AM             1                                    Armenia
3       AR            32                                  Argentina
...
75      ZW             3                                   Zimbabwe

This is what I have tried

library(rworldmap)
library(ggplot2)
map.world <- map_data(map="world")

gg <- ggplot()
gg <- gg + theme(legend.position="none")
gg <- gg + geom_map(data=map.world, map=map.world, aes(map_id=region, x=long, y=lat), fill="white", colour="black", size=0.25)
gg

That plots the world map just fine, so next I want to add colour to each country in proportion to the value 'num_responses' in aggregated_country_data

gg <- gg + geom_map(data=aggregated_country_data, map=map.world, aes(map_id=region, fill=num_responses), color="white", size=0.25)
gg

But now it's colour coding each of the colours as they correspond to the country code rather than the value that's in the column num_responses in aggregated_country_data.

It's clear that there's something about ggplot2 that I'm not getting, but I can't figure out what that is.

I would appreciate any input, Brad


I figured out what the problem was, and it has nothing to do with ggplot2 or anything else that I was doing. The aggregated_country_data data frame has different names for 'region' than in map.world. My input data (aggregated_country_data) uses a two letter country code by default that I converted into country name (called 'region' in the data frame) using the countrycode R package, but it is using a different naming convention for the names than exists in map.world. So that's a totally different problem.

Meagre answered 27/4, 2015 at 22:25 Comment(5)
are you sure? it looks OK on my machineHarri
Maybe this post can be useful [link] (https://mcmap.net/q/980351/-r-ggplot2-merge-with-shapefile-and-csv-data-to-fill-polygons/709777)Zackzackariah
@Harri It plots a map in colour, but the colour codes for the countries don't match the numbers in the column 'num_responses', they seem to be matched to the country code. But I'll try again.Meagre
maybe use fill=factor(num_responses) otherwise the colors can be too similarHarri
I tried that, it's not displaying the colours in relation to the num_responses vector at all. I'm totally confused.Meagre
A
9
library(rworldmap)
library(ggplot2)
map.world <- map_data(map="world")

#Add the data you want to map countries by to map.world
#In this example, I add lengths of country names plus some offset
map.world$name_len <- nchar(map.world$region) + sample(nrow(map.world))

gg <- ggplot()
gg <- gg + theme(legend.position="none")
gg <- gg + geom_map(data=map.world, map=map.world, aes(map_id=region, x=long, y=lat, fill=name_len))

gg <- gg + scale_fill_gradient(low = "green", high = "brown3", guide = "colourbar")
gg <- gg + coord_equal()
gg

enter image description here

Amuse answered 27/4, 2015 at 23:49 Comment(8)
Perhaps remove Antarctica and at least use coord_map()?Bulrush
@Bulrush My initial approach was to use projection="mercator" in map_data() then do coord_map(). But setting the projection argument to "mercator" in map_data() gives an error: "Error in names[df$group, 1] : subscript out of bounds"..Any idea what's amiss? cheersAmuse
The col vector is unused, right? Can you please remove it?Rauscher
be careful that country names in the map.world object are not standard (e.g. Russian Federation shows up as Russia) which makes difficult to connect it to other data sources (74 out of 214 names i tried were non-standard)Cosh
@Cosh Hi, you should use the country ISO codes to relate map.world data to other sources, perform joins etc, not the country names ;-).Amuse
Obviously. But i dit not see the iso2c returned as part of map_data. did i miss it?Cosh
map_data has the following fields: long, lat, group, order, region, subregion. no iso2cCosh
@Cosh Here, country names are irrelevant since they are just used to generate a random vector from which the map's colour gradient is defined in order to answer OP's question: which is not the same as yours. If you need help with rworldmap data, please post a new question on SO or contact the package authors, cheers.Amuse

© 2022 - 2024 — McMap. All rights reserved.