Plot a raster layer from raster package with the new geom_raster

517 views
Skip to first unread message

Manuel Spínola

unread,
Feb 24, 2012, 6:48:48 AM2/24/12
to ggp...@googlegroups.com
Dear list members,

How to plot a raster layer from the package "raster" with the new geom_raster?

Here is my raster:

> psi.hat <- predict(fm, predictores)
> class(psi.hat)
[1] "RasterLayer"
attr(,"package")
[1] "raster"
> psi.hat
class       : RasterLayer 
dimensions  : 382, 407, 155474  (nrow, ncol, ncell)
resolution  : 0.008333333, 0.008333333  (x, y)
extent      : -85.95, -82.55833, 8.041667, 11.225  (xmin, xmax, ymin, ymax)
coord. ref. : NA 
values      : in memory
min value   : 6.318936e-05 
max value   : 0.9772914 

> summary(psi.hat)
Cells:  155474 
NAs  :  95049 


Best,

Manuel

--
Manuel Spínola, Ph.D.
Instituto Internacional en Conservación y Manejo de Vida Silvestre
Universidad Nacional
Apartado 1350-3000
Heredia
COSTA RICA
mspi...@una.ac.cr
mspin...@gmail.com
Teléfono: (506) 2277-3598
Fax: (506) 2237-7036
Personal website: Lobito de río
Institutional website: ICOMVIS

Dennis Murphy

unread,
Feb 24, 2012, 10:37:57 AM2/24/12
to Manuel Spínola, ggp...@googlegroups.com
Hi Manuel:

The help page of geom_raster() in 0.9.0 starts out with the following:

"This is a special case of geom_tile where all tiles are the same
size. It is implemented highly efficiently using the internal
rasterGrob function."

Implicit in this statement is that the input data to the geom is a
data frame. From what little I know about the raster package, it
generates its own classes of objects, so it's possible that you need
to convert objects from the raster package into a different format
that ggplot2 will accept.

You might find the rasterVis package useful; by poking around a
little, I discovered a function gplot() which appears to prepare data
objects from the raster package for use in a subsequent ggplot() call.
See the examples section of its help page for a couple of
illustrations. Another possibility is the ggmap package developed by
David Kahle, which has more options for adding maps as background
layers in a ggplot. Like ggplot2, ggmap is in active development.

If you want to plot a raster image in a ggplot, look into
annotation_raster() or annotation_custom(). Section 4 of the
transition guide shows a couple of examples where images were added to
a graph or used as the plot background. The term 'raster' gets bandied
about a bit loosely these days so I'm easily confused about what the
term means.

When you get a solution, would you be kind enough to share it here?
There are several people on this list who have a strong interest in
using ggplot2 for spatial maps. I have a couple of examples in the
sections on geom_map() and geom_raster() in the transition guide, with
a few more in geom_polygon(), but I'm fully aware that's not even
scratching the surface of the potential applications of ggplot2 with
spatial data.

Dennis

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

Manuel Spínola

unread,
Feb 24, 2012, 11:52:03 AM2/24/12
to Dennis Murphy, ggp...@googlegroups.com
Thank you very much Dennis.

I took a look at rasterVis, function gplot, and yes, it can plot a raster using ggplot2.  It use geom_tile. The result is incredible, because the simplicity. Now, my question is. Is it possible to use the new geom_raster and all the new features of ggplot2.  Who developed the gplot function was Robert Hijmans, the same person that developed the rsater package. I can sent a message to him telling that there is a new geoms on the new version of the ggplot2.  May be he can take advantages of the new geoms.

I am attaching a pdf version of the plot.

Best,

Manuel 

2012/2/24 Dennis Murphy <djm...@gmail.com>
ectoCR_plot.pdf
Reply all
Reply to author
Forward
0 new messages