[R-sig-Geo] gplot error in rasterVis

Natalie Wagenbrenner nwagenbrenner at gmail.com
Thu May 30 23:51:16 CEST 2013


Thanks for the quick reply. Yes, this works for me now.

One more follow-up question -- I'm trying to plot a raster on top of a
static google map image. I can do this:

library(RgoogleMaps)
library(raster)
library(maps)
library(ggmap)

r <- raster(ncol=10, nrow=10)
values(r) <- 1:ncell(r)
extent(r)<-c(-113.5732, -112.4852, 43.04848, 43.81728)

#not ideal-> no legend, raster not transparent
bsbMap<-get_map(location = c(lon = -113.027724, lat = 43.402726), zoom
= 8, maptype = 'terrain')
m <- ggmap(bsbMap)+ inset_raster(r, xmin(r), xmax(r), ymin(r), ymax(r))

but is there a way to control plotting of the raster (e.g., to set the
transparency or color scheme) and add a legend? I was hoping to do
this with geom_raster rather than inset_raster, but it looks like
ggmap doesn't work with geom_raster. I have used geom_raster to make a
similar map with state/county boundaries:

#would like to create this sort of overlay on a static google map
rdf <- rasterToPoints(r)
rdf <- data.frame(rdf)
colnames(rdf)<-c("lon","lat","T")

states<-map_data("state", region=c("idaho", "montana", "utah", "wyoming"))
counties<-map_data("county", region=c("idaho"))

g <- ggplot( data=rdf) + geom_raster(aes(x=lon, y=lat, fill=T) )
g<-g + scale_fill_gradient(name="T (C)", low="blue", high="orange")
g<-g + theme_bw() + xlim(c(-115.5,-110.5)) + ylim(c(41.5,45))
g<-g + geom_path(data=states, aes(x=long,y=lat,group=group),
colour="black", alpha=0.5)
g<-g + geom_path(data=counties, aes(x=long,y=lat,group=group),
colour="black", alpha=0.5)

Any suggestions for the most efficient way to create this sort of
raster overlay on a google map?

Thanks,
Natalie

On Thu, May 30, 2013 at 6:52 AM, Oscar Perpiñán Lamigueiro
<oscar.perpinan at gmail.com> wrote:
> Hi,
>
> With the current definition of gplot you have to add the layers you need
> to plot (that's why you get the error "No layers to plot"). Take a look
> at the example of the help page. With your example:
>
> gplot(r) + geom_tile(aes(fill=value)) + coord_equal()
>
> On the other hand, the warning when you use gplot is because both
> latticeExtra and ggplot2 define the function layer. If ggplot2 is loaded
> after rasterVis, the layer definition from latticeExtra becomes
> masked. If you plan to use latticeExtra (for example, to add layers over
> the result of a levelplot call) you should load ggplot2 before
> rasterVis.
>
> Best,
>
> Oscar.
>
> Natalie Wagenbrenner <nwagenbrenner at gmail.com> writes:
>
>> I'm trying to use gplot() in rasterVis to plot a RasterLayer object
>> using ggplot2
>> graphics, but I keep getting an error.  Here is a reproducible
>> example:
>>
>> library(raster)
>> library(rasterVis)
>>
>> r <- raster(ncol=10, nrow=10)
>> values(r) <- 1:ncell(r)
>> gplot(r)
>>
>> The following `from` values were not present in `x`: col, color, pch,
>> cex, lty, lwd, srt, adj, bg, fg, min, max
>> Error: No layers in plot
>>
>> I see the following warning when I load rasterVis:
>>
>> Loading required package: ggplot2
>>
>> Attaching package: ‘ggplot2’
>>
>> The following object(s) are masked from ‘package:latticeExtra’:
>>
>>     layer
>>
>> Perhaps this is part of the problem? Or I am misunderstanding the
>> gplot() methods somehow? Thanks for any suggestions.
>>
>> Natalie
>>
>> Session info:
>>
>> R version 2.14.1 (2011-12-22)
>> Platform: x86_64-pc-linux-gnu (64-bit)
>>
>> locale:
>>  [1] LC_CTYPE=en_CA.UTF-8       LC_NUMERIC=C
>> LC_TIME=en_CA.UTF-8
>>  [4] LC_COLLATE=en_CA.UTF-8     LC_MONETARY=en_CA.UTF-8
>> LC_MESSAGES=en_CA.UTF-8
>>  [7] LC_PAPER=C                 LC_NAME=C
>> LC_ADDRESS=C
>> [10] LC_TELEPHONE=C             LC_MEASUREMENT=en_CA.UTF-8
>> LC_IDENTIFICATION=C
>>
>> attached base packages:
>> [1] grid      stats     graphics  grDevices utils     datasets
>> methods   base
>>
>> other attached packages:
>>  [1] ggmap_2.3           RgoogleMaps_1.2.0.3 png_0.1-4
>> maps_2.3-2          rasterVis_0.20-07
>>  [6] hexbin_1.26.2       latticeExtra_0.6-24 RColorBrewer_1.0-5
>> lattice_0.20-0      ggplot2_0.9.3.1
>> [11] rgdal_0.8-9         raster_2.1-25       sp_1.0-9
>> ncdf_1.6.6          vimcom_0.9-8
>> [16] setwidth_1.0-3      colorout_1.0-0
>>
>> loaded via a namespace (and not attached):
>>  [1] colorspace_1.2-2 dichromat_2.0-0  digest_0.6.3     gtable_0.1.2
>>   labeling_0.1     mapproj_1.2-1
>>  [7] MASS_7.3-16      munsell_0.4      plyr_1.8         proto_0.3-10
>>   reshape2_1.2.2   rjson_0.2.12
>> [13] scales_0.2.3     stringr_0.6.2    zoo_1.7-9
>>
>> _______________________________________________
>> R-sig-Geo mailing list
>> R-sig-Geo at r-project.org
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
>
> --
> Oscar Perpiñán Lamigueiro
> Grupo de Sistemas Fotovoltaicos (IES-UPM)
> Dpto. Ingeniería Eléctrica (EUITI-UPM)
> URL: http://procomun.wordpress.com
> Twitter: @oscarperpinan



More information about the R-sig-Geo mailing list