maj
maj

Reputation: 2529

R: world maps with ggplot2/ggmap - How to load a png image as a map

I am trying to find a workaround for ggmap's missing support of world maps (i.e. you can't create any maps that show latitudes > 80°, due to idiosyncrasies in the mapproj package). To a limited extend however, it seems possible to create empty world maps and save them as an image (png etc.), even if you can't use the ggmap object directly as one normally would in ggmap(get_map(...)).

That's why I'd like to load a png (ideally, one I created with ggmap) into ggplot2 and use that as a map instead. How exactly can I do that?

I am aware that you can load background images in ggplot2 (see this stackoverflow question). But I'd also like to plot points on my map - it's important that the latitude/longitude values are mapped correctly.

(Notice: The code in this answer to World map with ggmap provides some code that, in terms of the output, comes close to what I had in mind.)

Upvotes: 2

Views: 1553

Answers (1)

Jonas Tundo
Jonas Tundo

Reputation: 6197

Here is an example without ggmap that you can use.

require(ggplot2)   
require(cshapes)

world <- cshp(date=as.Date("2012-01-1"))
world.points <- fortify(world, region='COWCODE')

world.points2 <- merge(world.points,world@data,by.x="id",by.y="COWCODE",all.x=TRUE )

# Add a variable 'size' per country 
world.points2$size <- factor(ifelse(world.points2$AREA < 121600,"small",ifelse(world.points2$AREA > 515000, "large", "medium")))

# Coord_fixed fixes the aspect ratio.
p <- ggplot(world.points2,aes(long,lat,group=group,fill=size)) + geom_polygon(colour="grey50") + coord_fixed()
p

Upvotes: 2

Related Questions