question about the borders function

37 views
Skip to first unread message

Felipe

unread,
Jun 28, 2010, 11:47:53 AM6/28/10
to ggplot2
Hi all:
Does anyone know how to use the borders function with a shapefile
imported into R
using the readOGR function? I don't get an error message but instead
it creates a bigger
plot area below the shapefile. Please consider the example below after
downloading the
shapefile from this link:

https://secure.filesanywhere.com/fs/v.aspx?v=897263875a6472a99baa

library(rgdal)
library(maptools)
library(ggplot2)
libray(maps)
dsn="C:/PathToShapefile"
wolves.map <- readOGR(dsn=dsn, layer="PNW_wolf_habitat_grid")
wolves.mapDF <- as.data.frame(wolves.map)
gpclibPermit()
wolves.ds <- fortify(wolves.map,region="GRID2_ID")

# Rename the 'GRID2'_ID to 'id' to to be able to merge with the
shapefile wolves.map
wolves.mapDF <- rename(wolves.mapDF,c(GRID2_ID="id"))
names(wolves.mapDF)

# merge the shapefile wolves.ds and wolves.mapDF dataframe to be able
to use the wolves.mapDF variables with ggplot2
wolves.ggmap <- merge(wolves.ds, wolves.mapDF, by = "id", all = TRUE)
wolves.ggmap <- wolves.ggmap[order(wolves.ggmap$order), ]
head(wolves.ggmap)

# Shapefile of 5 states, plot using ggplot2
wolves.plot <- ggplot(wolves.ds,aes(long,lat,group=group)) +
geom_polygon(colour='white',fill='lightblue')
wolves.plot
wolves.plot + borders("state") # doesn't work when borders function is
added to the plot

I was trying to generate a map with the 5 states and place it on top
of the above shapefile but the
column names are different,,perhaps will try to merge both datasets?

# How to show the state borders of Washington, Oregon, Idaho, Montana
and Wyoming on map?
#library(maps)
# extract county outlines and names from maps() database
#areaStudy <- map_data("state", c("oregon","washington",
"montana","wyoming", "idaho"))
#head(areaStudy)

fernando

unread,
Jun 28, 2010, 4:57:46 PM6/28/10
to Felipe, ggplot2
Hi Felipe,

In my opinión, the problem is that borders import the data directly with
geographical coordinates. Consider the following alternative (sessionInfo()
as ps);

##########
proj4string(wolves.map)


areaStudy <- map_data("state", c("oregon","washington","montana","wyoming",
"idaho"))

head(areaStudy)
areaStudy[, 1:2] <- project(as.matrix(areaStudy[, 1:2]),
proj4string(wolves.map)) # from rgdal, project the coordinates of state
polygons
areaStudy <- areaStudy[order(areaStudy$order), ]
wolves.plot <- ggplot(wolves.ds,aes(long,lat,group=group)) +
geom_polygon(colour='white',fill='lightblue') +
geom_path(data = areaStudy, aes(group = group)) # add just the
border to experiment
wolves.plot
###########

You can also so the converse (project the coordinates in wolves.map to
geographic). Think this will allow to use coord_map ...
Hope this helps,
fernando

ps:

> sessionInfo()
R version 2.11.1 (2010-05-31)
x86_64-pc-mingw32

locale:
[1] LC_COLLATE=Spanish_Spain.1252 LC_CTYPE=Spanish_Spain.1252
[3] LC_MONETARY=Spanish_Spain.1252 LC_NUMERIC=C
[5] LC_TIME=Spanish_Spain.1252

attached base packages:
[1] grid stats graphics grDevices utils datasets methods
[8] base

other attached packages:
[1] mapproj_1.1-8.2 gpclib_1.5-1 maps_2.1-4 ggplot2_0.8.7
[5] digest_0.4.2 reshape_0.8.3 plyr_0.1.9 proto_0.3-8
[9] maptools_0.7-34 lattice_0.18-8 foreign_0.8-40 rgdal_0.6-27
[13] sp_0.9-64

loaded via a namespace (and not attached):
[1] tools_2.11.1

-----Mensaje original-----
De: ggp...@googlegroups.com [mailto:ggp...@googlegroups.com] En nombre de
Felipe
Enviado el: lunes, 28 de junio de 2010 17:48
Para: ggplot2
Asunto: question about the borders function

https://secure.filesanywhere.com/fs/v.aspx?v=897263875a6472a99baa

--
You received this message because you are subscribed to the ggplot2 mailing
list.
Please provide a reproducible example: http://gist.github.com/270442

To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

Felipe Carrillo

unread,
Jun 29, 2010, 12:45:39 AM6/29/10
to fernando, ggplot2
Thanks Fernando, that created the borders just how I wanted. I would like
to implement this in the future:

"You can also so the converse (project the coordinates in wolves.map to
geographic). Think this will allow to use coord_map ..."

I just started using R to handle shapefiles and I'm pretty green about converting
my dataset to geographic coordinates.
Do I need a different package to project the coordinates to geographic or
rgdal it's all I need? I use ggplot2 for all my plots and it would be nice to use
coord_map() with it. Thanks


 

ps:

-----Mensaje original-----
De: > ymailto="mailto:ggp...@googlegroups.com"
> href="mailto:ggp...@googlegroups.com">ggp...@googlegroups.com [mailto:> ymailto="mailto:ggp...@googlegroups.com"
> href="mailto:ggp...@googlegroups.com">ggp...@googlegroups.com] En nombre

> de
Felipe
Enviado el: lunes, 28 de junio de 2010 17:48
Para:
> ggplot2
Asunto: question about the borders function

Hi all:
Does
> anyone know how to use the borders function with a shapefile
imported into
> R
using the readOGR function? I don't get an error message but instead
it
> creates a bigger
plot area below the shapefile. Please consider the example
> below after
downloading the
shapefile from this link:

> href="https://secure.filesanywhere.com/fs/v.aspx?v=897263875a6472a99baa"
> target=_blank
> >https://secure.filesanywhere.com/fs/v.aspx?v=897263875a6472a99baa

To post: email > ymailto="mailto:ggp...@googlegroups.com"
> href="mailto:ggp...@googlegroups.com">ggp...@googlegroups.com
To
> unsubscribe: email ggplot2+> href="mailto:unsub...@googlegroups.com">unsub...@googlegroups.com
More
> options: http://groups.google.com/group/ggplot2

--
You received this
> message because you are subscribed to the ggplot2 mailing list.
Please

> provide a reproducible example: > target=_blank >http://gist.github.com/270442

To post: email > ymailto="mailto:ggp...@googlegroups.com"
> href="mailto:ggp...@googlegroups.com">ggp...@googlegroups.com
To
> unsubscribe: email ggplot2+> href="mailto:unsub...@googlegroups.com">unsub...@googlegroups.com
More
> options: > >http://groups.google.com/group/ggplot2


fernando

unread,
Jun 29, 2010, 12:17:19 PM6/29/10
to Felipe Carrillo, ggplot2
Hi Felipe,

You can use rgdal::project for this (e.g. see the argument inv in ?project).
I put some code bellow based on your example. For a general overview of R
spatial capabilities, check http://cran.r-project.org/web/views/Spatial.html


I guess nevertheless that the projection used in your shapefile (Albers) is
probably appropriate (e.g. I think it is widely used in USA and Canada?).

Hope this helps,
fernando


library(rgdal)
library(maptools)
library(ggplot2)
library(maps)

dsn="C:/PathToShapefile"
# original at
https://secure.filesanywhere.com/fs/v.aspx?v=897263875a6472a99baa


wolves.map <- readOGR(dsn=dsn, layer="PNW_wolf_habitat_grid")
wolves.mapDF <- as.data.frame(wolves.map)
gpclibPermit()
wolves.ds <- fortify(wolves.map,region="GRID2_ID")

# Rename the 'GRID2'_ID to 'id' to to be able to merge with the shapefile
wolves.map
wolves.mapDF <- rename(wolves.mapDF,c(GRID2_ID="id"))
names(wolves.mapDF)

# merge the shapefile wolves.ds and wolves.mapDF dataframe to be able

# to use the wolves.mapDF variables with ggplot2


wolves.ggmap <- merge(wolves.ds, wolves.mapDF, by = "id", all = TRUE)
wolves.ggmap <- wolves.ggmap[order(wolves.ggmap$order), ]
head(wolves.ggmap)

############################################################################
##
# retrieve geographical coordinates for wolves.ggmap
wolves.ggmap[, 2:3] <- project(as.matrix(wolves.ggmap[, 2:3]),
proj4string(wolves.map), inv = TRUE)
############################################################################
##

areaStudy <- map_data("state", c("oregon","washington","montana","wyoming",
"idaho"))
head(areaStudy)

wolves.plot <- ggplot(wolves.ggmap, aes(long, lat, group = group)) +
geom_polygon(colour = I('white'), fill = I('lightblue')) +
geom_path(data = areaStudy, aes(group = group)) + # add just the
border to experiment
coord_map(project = "albers", lat0 = 23, lat1 = 29.5)
wolves.plot

############################################################################
##

-----Mensaje original-----
De: Felipe Carrillo [mailto:mazatla...@yahoo.com]
Enviado el: martes, 29 de junio de 2010 6:46
Para: fernando; ggplot2
Asunto: Re: question about the borders function

Reply all
Reply to author
Forward
0 new messages