1

I'm adding bar-plots to maps using ggplot and ggsubplot, but cannot figure out how to specify which to plot first. I'd like to plot the northerly ones first so they sit behind any overlapping plots. With a low fill alpha these should still be viewable. This is the workflow:

library(ggsubplot)
library(ggplot2)
library(maps)
library(plyr)

world_map = map_data("world")
(p = ggplot() + geom_polygon(data = world_map, aes(x=long, y=lat,group=group)))

d = ddply(world_map,.(region),summarize,long=mean(long),lat=mean(lat))
d = d[sample(1:nrow(d), 50),]
d = rbind(d,d)
d$cat = rep(c('A','B'), each=nrow(d)/2)
d$value = sample(1:10, nrow(d), rep=T)
head(d)

p + geom_subplot(data=d, aes(long, lat, group=region, subplot = geom_bar(aes(cat, value, fill=cat), 
                 col='black', alpha=0.9, stat="identity")), width = 30, height=30)

enter image description here

As you can see the plot order seems pretty random. So I tried to change region (country) to an ordered factor:

d$region = factor(d$region, ordered=T)
(ord = count(d[,c('region','lat')], vars=c('region','lat')))
ordered_levels = order(ord$lat, decreasing=T)
print(ord[ordered_levels,])
levels(d$region) = levels(d$region)[ordered_levels]
levels(d$region)

p + geom_subplot(data=d, aes(long, lat, group=region, subplot = geom_bar(aes(cat, value, fill=cat), 
                 col='black', alpha=0.9, stat="identity")), width = 30, height=30)

But this does not seem to solve the problem. Very grateful for any suggestions.

geotheory
  • 22,624
  • 29
  • 119
  • 196

2 Answers2

1

The underlying map was pretty much of a mess in the resulting figure, but this pre-ordering seemed to bring the high latitude items to the front:

world_map = world_map[order(world_map$lat),]

It wasn't clear whether you wanted the negative latitudes to be plotted under the latitudes nearer the Equator, so you also have the option of using abs(world_map$lat) as the order.

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • Thanks for reply, although I don't get how this is supposed to work. If its about changing the data order, why not `d = d[order(d$lat),]` instead? But either way the plots don't happen in any latitudinal order for me. Some northerly plots still plot on top of southerly ones. – geotheory Dec 19 '13 at 17:09
  • I intended the northerly points to always overplot the southerly points. I _thought_ that was what you asked for, or rather I inferred it and asked that you clarify what was intended. I suppose ordering after the ddply operation would be safer. I obviously wasn't clear about the (unstated) overall strategy. – IRTFM Dec 19 '13 at 21:05
  • Sorry for any ambiguity - like I said I need to plot the northerly ones first :) – geotheory Dec 20 '13 at 01:40
  • Ah. The wouldn't it instead be `d <- d[rev(order(d$lat)), ]` or `world_map = world_map[rev(order(world_map$lat)),]`? – IRTFM Dec 20 '13 at 01:43
  • It seems the group argument is the determinant. I didn't see that coming! – geotheory Dec 20 '13 at 01:56
1

Is this what you had in mind?

You need to order d by latitude, as you pointed out, and also use group=lat in the call to geom_subplot(...).

set.seed(1)
d = ddply(world_map,.(region),summarize,long=mean(long),lat=mean(lat))
d = d[sample(1:nrow(d), 50),]
d = rbind(d,d)
d$cat = rep(c('A','B'), each=nrow(d)/2)
d$value = sample(1:10, nrow(d), rep=T)
d <- d[order(d$lat),]
head(d)
p + geom_subplot(data=d, aes(long, lat, group=lat, 
      subplot = geom_bar(aes(cat, value, fill=cat), 
        col='black', alpha=0.9, stat="identity")), width = 30, height=30)
jlhoward
  • 58,004
  • 7
  • 97
  • 140