Learn R Programming

loa (version 0.2.22)

1.2.GoogleMap.and.geoplotting.tools: Google Maps plotting for lattice

Description

Plotting georeferenced data on maps using lattice and RgoogleMaps

Usage

GoogleMap(x, data = NULL, panel = panel.loaPlot,
        map = NULL, map.panel = panel.GoogleMapsRaster, 
        recolor.map = FALSE, ..., lon.lat = FALSE) 

  GoogleMap.old(x, data = NULL, map = NULL, 
        map.panel = panel.GoogleMapsRaster, 
        panel = panel.xyplot, 
        recolor.map = FALSE, ...)

  googleMap(...)

  quickMap(lat, lon, show.data = FALSE, ...)

  #map handlers
  makeMapArg(ylim, xlim, aspect = NULL, 
        recolor.map = FALSE, ...)
  getMapArg(object = trellis.last.object())

  #map panel handlers
  panel.GoogleMapsRaster(map)
  panel.GoogleMaps(map)

  #axis handlers
  xscale.components.GoogleMaps(lim, ..., map = map)
  yscale.components.GoogleMaps(lim, ..., map = map)
  axis.components.GoogleMaps(map, xlim = NULL, ylim = NULL, ...)

Arguments

x
For GoogleMap and GoogleMap.old only. A formula setting the plot structure, by default z ~ latitude * longitude | cond. The axis elements latitude and longitude are required,
data
For GoogleMap and GoogleMap.old only. If supplied, the assumed source of the elements of formula x, typically a data.frame.
panel, map.panel
For GoogleMap and GoogleMap.old only. The panels to use when generating the plot data and mao layers, respectively. panel is by default the standard loa scatter plot panel
map
For GoogleMap and related functions only. If supplied, a modified RgoogleMaps output, to be used as the plot background. If NULL (default), this is generated using the RgoogleMaps function
recolor.map
For GoogleMap and RgoogleMapsWrapper only. If supplied, a vector of elements that R can treat as colors, used as a color scale to recolor map. This uses standard
lon.lat
For GoogleMap only, logical. Should the plot formula be applied as z ~ lon * lat | cond? This operation is handled using the formula.type argument in formulaHand
lat,lon
For quickMap only. Numeric vectors of latitude and longitude values.
ylim, xlim, lim
The latitude and longitude plot ranges. ylim and xlim are only required by makeMapArg, which uses these to set the requested map size. For the axis handlers (yscale... and xscale...<
aspect
The aspect ratio of the plot. If not supplied (recommended), this is determined based on map size, but can be forced by user.
show.data
For quickMap only, a Logical. Should the lat, lon values supplied be plotted on the map (show.data = TRUE) or just be used to define the range/size of the map being generated? Default
object
For getMapArg only, a lattice plot to recover an RgoogleMaps map from. (If not supplied, this is assumed to last lattice plot.)
...
Additional arguments are passed on to related functions. For, quickMap these are makeMapArg and the lattice function xyplot

Value

  • GoogleMap and quickMap return trellis objects, much like conventional lattice plot functions. makeMapArg returns a modified form of the RgoogleMaps function GetMap output suitable for use as the map argument with the above functions. Note: the automatic assignment of the RgoogleMaps function argument size getMapArg recovers the map from an existing GoogleMap output. panel.GoogleMapsRaster and panel.GoogleMaps generate panel outputs suitable for use in standard lattice panel functions. yscale.components.GoogleMaps, xscale.components.GoogleMaps and axis.components.GoogleMaps generate suitable latitude, longitude scales for use with map layers.

Details

NOTE: GoogleMap and related panel and axis handling functions are currently in development functions and may be subject to changes. GoogleMap provides lattice-style conditioning/handling for RgoogleMaps outputs. This uses loaPlot and the latest version of panelPal to manage default panel and key settings. GoogleMap.old is the previous version of the GoogleMap which uses the previous version of panelPal googleMap is a GoogleMap wrapper, included because this alternative form of the plot name was used in earlier versions of the package. quickMap is crude map plotter intended to demonstrate the use of the other 'handler' functions when building dedicated mapping functions. makeMapArg accepts latitude and longitude ranges and RgoogleMaps function GetMap arguments, and produces an output suitable for use with the panel.GoogleMapsRaster and panel.GoogleMaps panel functions or in subsequent GoogleMap calls if, e.g., the users wishes to reuse an existing map. panel.GoogleMapsRaster and panel.GoogleMaps are lattice panel functions that generate map layers for a lattice plot using makeMapArg outputs. yscale.components.GoogleMaps and xscale.components.GoogleMaps are y- and x-axis handlers for use with the above panels. axis.components.GoogleMaps is a wrapper that combines yscale.components.GoogleMaps and xscale.components.GoogleMaps and allows both axis to be set from the lattice function argument axis rather than each individually, via yscale.components and xscale.components.

References

This function makes extensive use of code developed by others. lattice: Sarkar, Deepayan (2008) Lattice: Multivariate Data Visualization with R. Springer, New York. ISBN 978-0-387-75968-5 RColorBrewer: Erich Neuwirth (2011). RColorBrewer: ColorBrewer palettes. R package version 1.0-5. http://CRAN.R-project.org/package=RColorBrewer RgoogleMaps: Markus Loecher and Sense Networks (2011). RgoogleMaps: Overlays on Google map tiles in R. R package version 1.1.9.6. http://CRAN.R-project.org/package=RgoogleMaps

See Also

In other packages, see RgoogleMaps: GetMap; LatLon2XY; LatLon2XY.centered; and, XY2LatLon. lattice: xyplot; panel.xyplot; and panel.levelplot.

Examples

Run this code
## Example 1
## quickMap code
## as example of third-party use of functions

quickMap <- function(lat, lon, show.data = FALSE, ...){

    #get map
    map <- makeMapArg(lat, lon, ...)

    #scale axis for map projection
    map.axis.comps <- axis.components.GoogleMaps(map)
    map.axis <- function(components, ...) 
                   axis.default(components = map.axis.comps, ...)

    #scale data for map projection
    #see ?Rgooglemaps:::LatLon2XY
    temp <- LatLon2XY.centered(map, lat, lon)
    lat <- temp$newY
    lon <- temp$newX

    #plot data on map
    xyplot(lat~lon, 
           xlim = map$xlim, ylim = map$ylim,
           aspect = map$aspect, 
           axis = map.axis,
           panel = function(...){
               panel.GoogleMapsRaster(map)
               if(show.data)
                   panel.xyplot(...)
           }, ...)
}

## Example 2
## Off-line GoogleMap examples

#  Use a subsample of lat.lon.meuse
temp <- lat.lon.meuse[sample(1:155, 15),]


GoogleMap(zinc~latitude*longitude, col.regions=c("grey", "darkred"), 
          data=temp, map=roadmap.meuse)

GoogleMap(zinc~latitude*longitude, col.regions=c("grey", "darkred"),
          panel=panel.binPlot, 
          data=temp, map=roadmap.meuse)

GoogleMap(cadmium*50+copper*10+lead*2+zinc~latitude*longitude, 
          col.regions=c("grey", "darkred"), 
          key.z.main="Concentrations", panel.zcases = TRUE,
          data=temp, map=roadmap.meuse)

GoogleMap(cadmium*50+copper*10+lead*2+zinc~latitude*longitude, 
          col.regions=c("grey", "darkred"), panel=panel.zcasePiePlot,
          data=temp, map=roadmap.meuse)

#  Note 1:
#  Here, the map argument is supplied so example works off-line. 
#  If not supplied and R is on-line, GoogleMap will (try to) get map 
#  from the Google API. Repeat any of above without map argument 
#  to see. For example:
#  GoogleMap(zinc~latitude*longitude, col.regions=c("grey", "darkred"), 
#            data=lat.lon.meuse)
#  (The map will appear slightly different because non-default 
#  size and maptype settings were used to make roadmap.meuse. See 
#  ?roadmap.meuse for details.)  

#  Note 2:
#  To make a map for use with panel.GoogleMaps or panel.GoogleMapsRaster 
#  without plotting use markMapArg(). To recover a map from a previously 
#  plotted loa GoogleMap use getMapArg().

Run the code above in your browser using DataLab