# [R] ggmap (or ggplot2 with mapping question)

Roy Mendelssohn - NOAA Federal roy.mendelssohn at noaa.gov
Sat Dec 27 02:35:33 CET 2014

```Just as a followup, for the archives:

geom_rect does work. The aes for geom_rect requires xmax, xmin, ymax, ymin, - which are the  limits of the bounding box.  So:

> ggmap(myMap) +  geom_rect(aes(xmin=-130,xmax=-125,ymin=30,ymax=35),colour="white",fill="red”)

should work.  geom_polygon will work also (which could be convenient for doing the groupings), if a data frame of the following sort is created:

> test<-data.frame(
>   lon=c(-130,-130,-125,-125,-125,-125,-120,-120),
>   lat=c(20,25,25,20,30,35,35,30),
>   group=c(1,1,1,1,2,2,2,2),
>   order=c(1,2,3,4,5,6,7,8)
> )
>

then:

> ggmap(myMap) + geom_polygon(data=test,aes(x = lon, y = lat, group=group),color="white",fill="red”)

should also work.  The more I look at various maps the more I like just using ggplot2, Eric Anderson gave me this example:

> library(mapdata)
> library(maptools)
>
> w <- map_data("worldHires", ylim = c(-25,50), xlim = c(-180,-70))
> z=ggplot() + geom_polygon(data = w, aes(x=long, y = lat, group = group), fill = "grey80") +coord_fixed(1.3, xlim = c(-180,-70), ylim = c(-25,50)) + theme_bw()
>

then either of the two geoms can be added to “z” to make the desired plot.

-Roy

On Dec 26, 2014, at 4:16 PM, David Winsemius <dwinsemius at comcast.net> wrote:

>
>> On Dec 26, 2014, at 3:26 PM, Roy Mendelssohn - NOAA Federal <roy.mendelssohn at noaa.gov> wrote:
>>
>> HI All:
>>
>> Okay I am feeling particular brain dead today.  i have the following map created so far using ggmap:
>>
>>> library(ggmap)
>>> myLocation <- c(-178, -28, -72, 48)
>>> myMap <- get_map(location=myLocation, source="google", maptype="satellite", crop=FALSE)
>>>
>
> All if get is a matrix. No map.
>
>
>>
>> I want to color in some lat-lon 5 degree boxes based on some criterion,  and it look like this could be done use geom_polygon if I can figure out how to get the data in the format required for geom_polygon.  To simplify the problem, suppose i have a bounding box:
>>
>> subbox<-c(-130, 20, -125, 25)
>>
>
> That looks more like a specification for geom.rect than for geom_polygon.
>
>
>> and I want to color that box red on myMap defined above.    To put this in context, I actually have 360 squares which have been clustered by some criteria, and I want to color them all based on the cluster number of the box. but if I can figure out how to do the simple problem above I can  extend it to the more general problem.
>>
>> Basically the question is how can I get that simple box information into the format required for eom_polygon.
>
> If it were used for eom_polygon I would imagine you need to put it in the form pairs (x,y)
>
> subbox<-list(x=c(-130,-125,-125,-130,-130),y=c( 20,20,25, 25,20))
>
> Just a guess mind, you.
>
> —
> David.
>>
>> Thanks for any help.
>>
>> -Roy
>>
>>
>> **********************
>> "The contents of this message do not reflect any position of the U.S. Government or NOAA."
>> **********************
>> Roy Mendelssohn
>> Supervisory Operations Research Analyst
>> NOAA/NMFS
>
>
>

**********************
"The contents of this message do not reflect any position of the U.S. Government or NOAA."
**********************
Roy Mendelssohn
Supervisory Operations Research Analyst
NOAA/NMFS
Environmental Research Division
Southwest Fisheries Science Center
***Note new address and phone***