1.2.googleMap: Google Maps plotting for lattice

Description Usage Arguments Details Value Note Author(s) References See Also Examples

Description

Plotting georeferenced data on maps using lattice and RgoogleMaps

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
  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, while z and conditioning cond are optional.

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 panel.loaPlot. map.panel can be the default panel.GoogleMapsRaster or the alternative panel.GoogleMaps.

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 GetMap, the supplied latitude, longitude ranges, and any additional RgoogleMaps arguments supplied within the call. The map is supplied via makeMapArg which modifies the RgoogleMaps output before returning it to simplify local handling and (lattice) plotting.

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 RColorBrewer functions, so can handle arguments like recolor.map = c("white", "grey") for greyscale, etc. Disabled by the default FALSE or NULL.

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 formulaHandler

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...) the local alternative lim is used for both ylim and xlim in generic code. In GoogleMap and quickMap, if supplied, xlim and ylim are passed to lattice function xyplot via LatLon2XY.centered to handle local scaling.

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 show.data = FALSE.

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.

For GoogleMap these are makeMapArg, cexHandler, cexHandler and xyplot.

makeMapArg uses the RgoogleMaps function GetMap. So, most GetMap arguments can be directly accessed from either GoogleMap or quickMap via this route, e.g. maptype = "satellite". The returned object is then modified to simplify its handling by the associated panel and axis functions.

By default both data point colour and size are z-scaled for GoogleMap. If z is supplied, and cex and col are not set by the user in the plot command, these plot properties are managed by cexHandler and colHandler, respectively. cexHandler and colHandler arguments can be passed direct as part of a GoogleMap command to fine-tune these, e.g. cex.range to change the cex range that z values are scaled to and col.region to change the color range that is applied to z. See associated Help documents for further information.

Similarly, argument passing to xyplot in both GoogleMap and quickMap provides access to most common plot parameters, e.g. col, pch, and cex for plot symbol color, type and size, respectively.

getMapArg recovers the map from a lattice plot object generated with GoogleMap. Unless the plot object is supplied in the getMapArg call, this is assumed to be the last lattice (trellis) output.

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.

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.

Note

Google Maps outputs are 2D projections of curve sections of the Earth's surface. Therefore, the assignment of points within panels and the annotation of latitudes and longitudes along axis needs to be locally handled to account for this.

GoogleMap and quickMaps use RgoogleMaps functions LatLon2XY, LatLon2XY.centered and XY2LatLon to locally scale both axis and data.

Important: Users wanting to add data to these plots, e.g. using update or layers in latticeExtra, should first rescale the data. Likewise, users wanting to add maps to other plots will need to rescale plotted data to use these maps. See Example 1 below.

Important: The Google API returns a map panel larger than the data (latitude, longitude) range requested. However, it does this using a limited number of panel sizes. This means you may get back a map that is large than necessary. As xlim and ylim are passed to the API when they are called reseting these can produce similar effects (so you may not get exactly the map range you ask for! If you want to manually optimise the map ranges, the best option is currently to start with:

GoogleMap(..., size=c(640,640))

...and then reduce either or both of these values until you generate an appropriate map size.

Author(s)

Karl Ropkins

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 <erich.neuwirth@univie.ac.at> (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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
## 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 makeMapArg(). To recover a map from a previously 
#  plotted loa GoogleMap use getMapArg().

loa documentation built on June 7, 2017, 3:01 a.m.

Search within the loa package
Search all R packages, documentation and source code