2

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.)

Community
  • 1
  • 1
maj
  • 2,479
  • 1
  • 19
  • 25
  • If you want to plot a worldmap you could do: library(ggplot2) library(maps) #Get world map info world_map <- map_data("world") #Create a world plot p <- ggplot() + geom_polygon(data=world_map,aes(x=long, y=lat,group=group)) +scale_y_continuous(breaks=seq(-80,80,by=20)) – Jonas Tundo Oct 21 '13 at 15:03
  • @JT85: This would sure be a solution. I've been using the maps package before, but now I'm trying to find ways to create maps that are more detailed, better looking and particularly, more suitable for academic writing. That's why I thought I could perhaps somehow use an actual map (an image rather than, say, a series of SpatialPolygons) to plot my data on. – maj Oct 21 '13 at 19:36
  • @JT85: I hope I made it clear that your solution is not what I was aiming at. But anyway, thanks for your contribution. – maj Oct 22 '13 at 20:40
  • What do you mean with more detailed and better looking? Country level with colours per country for example? You can go as detailed as you like if you have the correct shapefile and for instance colour each country according to a certain variable. I'll post an answer as example. For academic purposes I would prefer this because you focus on the content. – Jonas Tundo Oct 23 '13 at 08:19
  • @JT85: "better looking": Among other things, I'm supposed to teach my fellow students about maps in R. The idea was, if they use maps that are particularly attractive (and easy to create), they will feel comfortable playing around with it. That's why I thought google maps-related R packages like ggmap might be the right choice. – maj Oct 29 '13 at 21:22

1 Answers1

2

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
Jonas Tundo
  • 6,137
  • 2
  • 35
  • 45
  • 1
    +1 for reminding me of this easy approach to world maps, even though it doesn't answer the question. – maj Oct 29 '13 at 21:39