Plotting ESRI shape file in ggplot

1,442 views
Skip to first unread message

Arnold Salvacion

unread,
May 2, 2010, 7:31:41 AM5/2/10
to ggp...@googlegroups.com
Hi Guys,

I am new to R and ggplot..can somebody help me on how I can plot (from file importing to plotting) a map with .shp format on ggplot?

Thanks in advance.

Best regards,

Arnold


 



--
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

Matthew Maenner

unread,
May 3, 2010, 6:10:00 PM5/3/10
to ggplot2
I use fortify (in the ggplot2 package) to convert shapefiles for
plotting in ggplot. I'm also new, so I don't know if there's a better
way to go about this.

Example:
shape<-readShapeSpatial("path:/file.shp")
shape2<-fortify(shape, region="key")
shape2<-shape2[order(shape2$order), ]
ggplot(data=shape2, aes(long, lat, group=group)) + geom_polygon()
+coord_map()

It seems you can now use xlim & ylim in coord_map to zoom in! Also,
sometimes geom_path seems to work better than geom_polygon.

The "key" is the grouping variable for each contiguous region. I have
run into trouble when a single key in the shapefile refers to multiple
areas (e.g. islands that are all coded as a single region in the
shapefile)--then I try to create a unique value for each region by
combining variables in the shapefile.

It produces really attractive maps, though. Definitely worth the extra
steps over plotting a shapefile directly.

Matt

On May 2, 6:31 am, Arnold Salvacion <arnold_salvac...@yahoo.com>
wrote:

Osmo Salomaa

unread,
May 5, 2010, 3:31:27 AM5/5/10
to ggplot2
How, if at all, does fortify handle holes in polygons?

I came across this a while back as I wrote my own conversion functions
with a similar purpose. Either fortify didn't exist back then or I
just didn't find it. Anyway, once I got a hold of the difficult data
structures returned by readShape*, it was fairly easy to convert
shapefiles with points and lines. Also polygons that you intend to
plot as a border with geom_path were easy enough to convert, but if
you want to fill polygons, then holes cannot be separate, but rather
they need to be attached to the outer border. And those attaching
segments need to be added.

Another thing I noticed was that shapefiles are often too large to be
manipulated in memory. The readShape* functions read the whole shape
database into memory, which is often enough to use all available
memory. Nothing I could do about that, but I found it better to
implement conversion functions as shapefile to CSV-file conversions
writing one shape at a time to a file instead of manipulating large
data frames in memory.

hadley wickham

unread,
May 5, 2010, 4:37:58 PM5/5/10
to Osmo Salomaa, ggplot2
> How, if at all, does fortify handle holes in polygons?

It returns them as independent polygons with hole = TRUE. Then it's
up to you to draw them on top of the land with the appropriate
colours. I know that this isn't the best way to deal with them, but
it's simple and it works for most maps I've come across.

> Another thing I noticed was that shapefiles are often too large to be
> manipulated in memory. The readShape* functions read the whole shape
> database into memory, which is often enough to use all available
> memory. Nothing I could do about that, but I found it better to
> implement conversion functions as shapefile to CSV-file conversions
> writing one shape at a time to a file instead of manipulating large
> data frames in memory.

That's a good point, but if the shapefiles are too big to fit in
memory, then I think you're going to have problems drawing them
because of the very high resolution.

Hadley

--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

Osmo Salomaa

unread,
May 6, 2010, 7:57:08 AM5/6/10
to hadley wickham, ggplot2
On 5.5.2010 23:37, hadley wickham wrote:
> It returns them as independent polygons with hole = TRUE. Then it's
> up to you to draw them on top of the land with the appropriate
> colours. I know that this isn't the best way to deal with them, but
> it's simple and it works for most maps I've come across.

For most cases it does work, yes, but sometimes you need to be able to
see the background through the holes.

The PBSmapping package [1] provides a findPolys function which can be
used to find the outer polygon to match a hole, or actually a point,
which can be any node of the hole polygon. findPolys deals with similar
data frames as ones that are usable with ggplot.

[1] http://cran.r-project.org/web/packages/PBSmapping/index.html

--
Osmo Salomaa <osmo.s...@strafica.fi>
Strafica Oy <http://www.strafica.fi/>
Reply all
Reply to author
Forward
0 new messages