1

I am trying, quite unsuccessfully, to create a global choropleth population density map for a specific 120 countries, using ggplot2. Once I have created this choropleth map, I want to add number of threatened species (biodiversity) on top. I have gathered the population of all 120 countries and have the area of each country also. I also have the total number of threatened species per country. I am trying to create something like this:

This is a lot like what I want to create!

Here is my code so far:


(Import Data set called "data")


library(ggplot2)    
library(splines)
library(mgcv)
library(plyr)
library(maps)

Creating data sets

humpopden2010 <- (data$X2010.Census)/(data$Area) # 2010 Census Population Divided by Country area

specthreat2010 <- (data$ThreatMammals2010 + data$ThreatBirds2010)/(data$Area/10^6)

specrich <- (data$TotalMammals2004 + data$TotalBirds2004)/(data$Area/10^6)

ATTEMPT 1 (Just trying to make pop density map)

world_map <- map_data("world")
names(humpopden2010) <- tolower(names(humpopden2010))
humpopden2010$Country <- tolower(rownames(data$X2010.Census))
choro <- merge(world_map, population, by = "Country")
choro <- choro[order(choro$order), ]
qplot(long, lat, data = choro, group = group, fill = humpopden2010,
  geom="polygon")

ATTEMPT 2

world_map <- map_data("world")
names(world_map)[5] <- "country"

p_country <- ddply(data, "country", summarise, total = length(Country))

p_map <- merge(p_country, world_map, by = "country", all = T)
p_map <- p_map[order(p_map$order), ]

ggplot(p_map, aes(long, lat)) +
geom_polygon(aes(group = group, fill = log(total)), colour = "grey60", size = .3) + ylim(-55, 85)

I can't even get the pop density map to create... need to do that before I can try to put on the number of threatened species.

Made a map with nothing on it here...

world <- map_data("world")
worldmap <- ggplot(world, aes(x=long, y=lat, group=group)) +
geom_path() +
scale_y_continuous(breaks=(-2:2) * 30) +
scale_x_continuous(breaks=(-4:4) * 45)

print(worldmap)
  • Welcome to GIS.SE. We've already done some, but it might help if you did a bit more style editing of your code (such as adding some spaces around operators, brackets, etc) to make it easier to read. Use the "edit" button, between "share" and "flag". – Martin F Apr 30 '14 at 0:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.