Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

map objects missing #1

Open
low-decarie opened this issue Sep 17, 2012 · 2 comments
Open

map objects missing #1

low-decarie opened this issue Sep 17, 2012 · 2 comments

Comments

@low-decarie
Copy link

Amazing work!

The examples use map_afghanistan and map_americas, which I can not find or reproduce.

I am guessing something like this is needed:
americas <- data.frame(map("americas", plot=FALSE)[c("x","y")])
map_americas <- qplot(x, y, data=americas, geom="path"))

@garrettgman
Copy link
Owner

Thanks. I haven't been able to figure out how to include (and document) the layers in a way that pleases R CMD check yet. However, its simple to make them. Here's the code I used:

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

# getbox by Heike Hoffman, 
# https://github.com/ggobi/paper-climate/blob/master/code/maps.r
getbox <- function (map, xlim, ylim) {
  # identify all regions involved
  small <- subset(map, (long > xlim[1]) & (long < xlim[2]) & (lat > ylim[1]) & (lat < ylim[2]))
  regions <- unique(small$region)
  small <- subset(map, region %in% regions)  

  # now shrink all nodes back to the bounding box
  small$long <- pmax(small$long, xlim[1])
  small$long <- pmin(small$long, xlim[2])
  small$lat <- pmax(small$lat, ylim[1])
  small$lat <- pmin(small$lat, ylim[2])

  # Remove slivvers
  small <- ddply(small, "group", function(df) {
    if (diff(range(df$long)) < 1e-6) return(NULL)
    if (diff(range(df$lat)) < 1e-6) return(NULL)
    df
  })

  small
}


## map layer
## adapted from map_nasa:
# https://github.com/ggobi/paper-climate/blob/master/code/maps.r

# assembling data
world <- map_data("world")
states <- map_data("state")
states$group <- max(world$group) + states$group
both <- rbind(world, states)
americas <- getbox(both, xlim = c(-115, -55), ylim = c(-21.1, 36.6))

# building americas layer
map_americas <- list(
  geom_polygon(aes(long, lat, group = group), data = americas, fill = "grey70", 
               colour = "grey60", inherit.aes = FALSE, show_guide = FALSE),
  scale_x_continuous("", breaks = NULL, expand = c(0.02, 0)),
  scale_y_continuous("", breaks = NULL, expand = c(0.02, 0)))


# building afghanistan layer
afghanistan <- getbox(world, c(60,75), c(28, 39))
map_afghanistan <- list(
  geom_polygon(aes(long, lat, group = group), data = afghanistan, 
               fill = "grey80", colour = "white", inherit.aes = FALSE, 
               show_guide = FALSE),
  scale_x_continuous("", breaks = NULL, expand = c(0.02, 0)),
  scale_y_continuous("", breaks = NULL, expand = c(0.02, 0)))

Best,
Garrett

@adrfantini
Copy link

Hi and thanks for the amazing package.
This is still an issue, map_americas is still not included. I think it should be specified in the manual and/or instructions on how to generate the example maps should be included in the manual.

Thanks!
Adriano

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants