25

I would like to use R to generate a very basic world map with a specific set of countries filled with a red colour to indicate that they are malaria endemic countries.

I have a list of these countries in a data frame but am struggling to overlay them on a world map.

I have tried using the wrld_simpl object and also the joinCountryData2Map method in the rworldmap package.

I would comment on this answer to prevent addition of a possibly redundant question but I do not have enough reputation at the moment, apologies for this.

https://stackoverflow.com/a/9102797/1470099

I am having difficulty understanding the arguments given to the plot() command - I wondered if there was just an easy way to tell R to plot all of the country NAMEs in my list on the wrld_simpl map instead of using grepl() etc. etc.

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

3 Answers3

28

Using the rworldmap package, you could use the following:

library(rworldmap)

theCountries <- c("DEU", "COD", "BFA")
# These are the ISO3 names of the countries you'd like to plot in red

malDF <- data.frame(country = c("DEU", "COD", "BFA"),
  malaria = c(1, 1, 1))
# malDF is a data.frame with the ISO3 country names plus a variable to
# merge to the map data

malMap <- joinCountryData2Map(malDF, joinCode = "ISO3",
  nameJoinColumn = "country")
# This will join your malDF data.frame to the country map data

mapCountryData(malMap, nameColumnToPlot="malaria", catMethod = "categorical",
  missingCountryCol = gray(.8))
# And this will plot it, with the trick that the color palette's first
# color is red

EDIT: Add other colors and include picture

## Create multiple color codes, with Burkina Faso in its own group
malDF <- data.frame(country = c("DEU", "COD", "BFA"),
  malaria = c(1, 1, 2))

## Re-merge
malMap <- joinCountryData2Map(malDF, joinCode = "ISO3",
  nameJoinColumn = "country")

## Specify the colourPalette argument
mapCountryData(malMap, nameColumnToPlot="malaria", catMethod = "categorical",
  missingCountryCol = gray(.8), colourPalette = c("red", "blue"))

enter image description here

BenBarnes
  • 19,114
  • 6
  • 56
  • 74
  • thanks so much, this really helped me... still got a few of the small African countries that don't appear on the map but that shall be another days task! – phlancelot Jul 02 '12 at 16:53
  • Do you know a possibility to make 2 groups? ie: c("DEU", "COD") in red and c("BFA") in blue? – TeYaP Jan 29 '19 at 16:23
  • 1
    @TeYaP, I just added an example with two colors – BenBarnes Jan 31 '19 at 21:40
  • Do you know how should I zoom on a particular zone (eg. Europe or Africa)? – A1010 Mar 04 '20 at 11:17
  • 1
    @AndreaM, you can set the `mapRegion` argument in the `mapCountryData` function to something like `"Africa"` or use the `xlim` and `ylim` arguments to set the map extents. – BenBarnes Mar 09 '20 at 13:11
14

Try using googleVis package and use gvisGeoMap Functions

e.g.

G1 <- gvisGeoMap(Exports,locationvar='Country',numvar='Profit',options=list(dataMode='regions'))

plot(G1)
Chase
  • 67,710
  • 18
  • 144
  • 161
Sunny Sunny
  • 3,204
  • 4
  • 25
  • 26
11
    library(maptools)
    data(wrld_simpl)
    myCountries = wrld_simpl@data$NAME %in% c("Australia", "United Kingdom", "Germany", "United States", "Sweden", "Netherlands", "New Zealand")
    plot(wrld_simpl, col = c(gray(.80), "red")[myCountries+1])

enter image description here

tim
  • 3,559
  • 1
  • 33
  • 46