Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Geographical borders incomplete using geom_polygon for plotting map - ggplot2

Tags:

r

ggplot2

gis

I have a data frame with coordinates of tweets such as:

library(ggplot2)

df <- data.frame(long = c(-58.1, -58.2, -58.3, -58.4, -58.5, -55),
                 lat = c(-34.1, -34.2, -34.3, -34.4, -34.5, -25))

I would like to plot the Metropolitan area of buenos aires, known as AMBA. It's defined by the area: longitud: (-58, -59) latitude: (-34, -35)

I have a row in my data frame that's outside the AMBA area. (important for question (2))

Here is what i've tried:

Load Argentinian map info

con <- url("http://gadm.org/data/rda/ARG_adm2.RData")
print(load(con))
close(con)

ggmap <- fortify(gadm, region = "NAME_2")        

Set Limits for plot to include just AMBA

lim <- data.frame(lon = c(-59, -58), lat = c(-35, -34))

Plot

ggplot(data=ggmap, aes(x=long, y=lat)) +
   scale_x_continuous(limits = c(-59,-58)) + 
   scale_y_continuous(limits = c(-35,-34)) +
   geom_polygon(data = ggmap, fill = "grey80", aes(group=group)) +
   geom_path(color="white",aes(group=group)) +
   geom_point(data = df, aes(x = lon, y = lat, colour = "red"), alpha = 30/100)

QUESTIONS:

  1. The main issue is that the areas of the political borders aren't complete in the external states, so the map looks odd. Would there be a way to get around this?
  2. Should I subset the data frame in order to keep the observations inside the AMBA area or can i directly do the plot and select the area i'm interested directly. I believe this is what scale_x_continuous(limits =...) does.
like image 258
marbel Avatar asked Jul 14 '13 22:07

marbel


1 Answers

Try coord_map instead of scale_x_continuous/scale_y_continuous. Setting limits on the coordinate system will zoom the plot (like you're looking at it with a magnifying glass), and will not change the underlying data like setting limits on a scale will.

ggplot(data=ggmap, aes(x=long, y=lat)) +
    geom_polygon(data=ggmap, fill="grey80", aes(group=group)) +
    geom_path(color="white",aes(group=group)) +
    geom_point(data=df, aes(x=long, y=lat), colour="red", alpha=30/100) +
    coord_map(xlim=-c(59, 58), ylim=-c(35,34))

R Plot

like image 151
rcs Avatar answered Nov 08 '22 17:11

rcs