coder_007
coder_007

Reputation: 187

Colouring map based on numeric value in R

I have a question related to R. I'm trying to colour the area for different countries on a map based on a numeric value. Now, I don't have the values for all country, so some of them will be empty.

Year    Country       Numeric
2009    Afghanistan   

Like this. So when I get values based on a certain level such as >5, 5-10, etc. I want to fill them with different colours. How can I do that in R? I've spent quite a long time on this without making significant headway.

I can fill in the world map, but can't manipulate it for the data I have.

> p <- ggplot(world, aes(long,lat,group=group)) + 
  geom_polygon(fill="darkgreen",colour="white") +
  theme(panel.background = element_rect(fill = "lightsteelblue2"))

Any advice and tips would be greatly appreciated!

Upvotes: 2

Views: 3495

Answers (3)

coder_007
coder_007

Reputation: 187

This is basically the code that I used to solve what I needed to do:

library(xlsx)
library(maps)
library(maptools)
library(mapproj)
library(mapdata)
library(rworldmap)
library(countrycode)

d <- read.xlsx("health_expenditure.xlsx",2)
d.df <- data.frame(d)
d.sub <- subset(d.df,Year=="2009")
w4 <- data.frame(d.sub$Country,data=d.sub$Numeric.Value)
colnames(w4)[1] <- "country"
w4$breaks <- cut(w4$data, 5)
w4$code <- countrycode(w4$country,"country.name","iso3c")
sPDF <- joinCountryData2Map(w4,joinCode="ISO3",nameJoinColumn="code")
par(mai=c(0,0,0.2,0),xaxs="i",yaxs="i")
mapDevice()
mapCountryData(mapToPlot=sPDF, nameColumnToPlot="breaks",missingCountryCol="white",oceanCol="lightsteelblue2",colourPalette="heat",mapTitle="Health Expenditure")

Health Expenditure in 2009

Upvotes: 1

Andy
Andy

Reputation: 1861

You could use rworldmap (which has the advantage of having a modern map including e.g. S.Sudan and excluding e.g. Soviet Union). Maps are available at 3 resolutions (the default is the coarsest suitable for global maps.

library(rworldmap)

#example data including an NA
country <- c('Afghanistan','Albania','Algeria','Andorra','Angola','Antigua and Barbuda')
data <- c(NA, 8.53, 8.64, 21.25, 10.08, 9.07)

dF <- data.frame(country=country, data=data)

#join data to a map to create a spatialPolygonsDataFrame
sPDF <- joinCountryData2Map(dF, joinCode='NAME', nameJoinColumn='country')

#default map (see rworldmap documentation for options e.g. catMethod, numCats, colourPalette, mapRegion)
#missingCountryCol used for NA and countries not in the join file
mapCountryData(sPDF, nameColumnToPlot='data', missingCountryCol='dark grey')

rworldmap plot

Upvotes: 3

mrip
mrip

Reputation: 15163

Include fill as part of your aes:

library(maps)
world<-map_data("world")

set.seed(123)
w2<-data.frame(world,data=sample(10,length(unique(world$group)),T)[world$group])

ggplot(w2,aes(long,lat,group=group,fill=data))+
    geom_polygon(color="white")+
    scale_fill_gradient(low="lightgreen",high="darkgreen")+
    theme(panel.background = element_rect(fill = "lightsteelblue2"))

enter image description here

Upvotes: 2

Related Questions