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.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Brad Davis
  • 1,063
  • 3
  • 18
  • 38
  • 1
    are you sure? it looks OK on my machine – mucio Apr 27 '15 at 23:03
  • Maybe this post can be useful [link] (http://stackoverflow.com/q/19791210/709777) – pacomet Apr 29 '15 at 09:31
  • @mucio 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. – Brad Davis Apr 29 '15 at 15:47
  • maybe use `fill=factor(num_responses)` otherwise the colors can be too similar – mucio Apr 29 '15 at 15:52
  • I tried that, it's not displaying the colours in relation to the num_responses vector at all. I'm totally confused. – Brad Davis Apr 29 '15 at 16:09

1 Answers1

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

shekeine
  • 1,445
  • 10
  • 22
  • Perhaps remove Antarctica and at least use `coord_map()`? – hrbrmstr Apr 28 '15 at 01:05
  • @hrbrmstr 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? cheers – shekeine Apr 28 '15 at 08:28
  • 1
    The `col` vector is unused, right? Can you please remove it? – Max Ghenis Feb 26 '16 at 10:55
  • 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) – user2345448 Jan 03 '18 at 17:18
  • @user2345448 Hi, you should use the country ISO codes to relate map.world data to other sources, perform joins etc, not the country names ;-). – shekeine Jan 03 '18 at 20:41
  • Obviously. But i dit not see the iso2c returned as part of map_data. did i miss it? – user2345448 Jan 03 '18 at 22:29
  • map_data has the following fields: long, lat, group, order, region, subregion. no iso2c – user2345448 Jan 03 '18 at 22:41
  • @user2345448 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. – shekeine Jan 03 '18 at 23:18