Learn R Programming

googleway (version 2.0.0)

add_polygons: Add polygon

Description

Add a polygon to a google map.

Usage

add_polygons(map, data = get_map_data(map), polyline = NULL, lat = NULL,
  lon = NULL, id = NULL, pathId = NULL, stroke_colour = NULL,
  stroke_weight = NULL, stroke_opacity = NULL, fill_colour = NULL,
  fill_opacity = NULL, info_window = NULL, mouse_over = NULL,
  mouse_over_group = NULL, draggable = NULL, editable = NULL,
  update_map_view = TRUE, layer_id = NULL, z_index = NULL)

Arguments

map

a googleway map object created from google_map()

data

data frame containing at least a polyline column, or a lat and a lon column. If Null, the data passed into google_map() will be used.

polyline

string specifying the column of data containing the encoded polyline

lat

string specifying the column of data containing the 'latitude' coordinates. Coordinates must be in the order that defines the path.

lon

string specifying the column of data containing the 'longitude' coordinates. Coordinates must be in the order that defines the path.

id

string specifying the column containing an identifier for a polygon.

pathId

string specifying the column containing an identifer for each path that forms the complete polygon. Not required when using polyline, as each polyline is itself a path.

stroke_colour

either a string specifying the column of data containing the stroke colour of each polygon, or a valid hexadecimal numeric HTML style to be applied to all the polygons

stroke_weight

either a string specifying the column of data containing the stroke weight of each polygon, or a number indicating the width of pixels in the line to be applied to all the polygons

stroke_opacity

either a string specifying the column of data containing the stroke opacity of each polygon, or a value between 0 and 1 that will be applied to all the polygons

fill_colour

either a string specifying the column of data containing the fill colour of each polygon, or a valid hexadecimal numeric HTML style to be applied to all the polygons

fill_opacity

either a string specifying the column of data containing the fill opacity of each polygon, or a value between 0 and 1 that will be applied to all the polygons

info_window

string specifying the column of data to display in an info window when a polygon is clicked

mouse_over

string specifying the column of data to display when the mouse rolls over the polygon

mouse_over_group

string specifying the column of data specifying which groups of polygons to highlight on mouseover

draggable

string specifying the column of data defining if the polygon is 'draggable'. The column of data should be logical (either TRUE or FALSE)

editable

string specifying the column of data defining if the polygon is 'editable' (either TRUE or FALSE)

update_map_view

logical specifying if the map should re-centre according to the polyline.

layer_id

single value specifying an id for the layer.

z_index

single value specifying where the polygons appear in the layering of the map objects. Layers with a higher z_index appear on top of those with a lower z_index. See details.

Details

z_index values define the order in which objects appear on the map. Those with a higher value appear on top of those with a lower value. The default order of objects is (1 being underneath all other objects)

  • 1. Polygon

  • 2. Rectangle

  • 3. Polyline

  • 4. Circle

Markers are always the top layer

See Also

encode_pl

Examples

Run this code
# NOT RUN {
map_key <- 'your_api_key'

## polygon with a hole - Bermuda triangle
## using one row per polygon, and a list-column of encoded polylines
pl_outer <- encode_pl(lat = c(25.774, 18.466,32.321),
      lon = c(-80.190, -66.118, -64.757))

pl_inner <- encode_pl(lat = c(28.745, 29.570, 27.339),
       lon = c(-70.579, -67.514, -66.668))

df <- data.frame(id = c(1, 1),
       polyline = c(pl_outer, pl_inner),
       stringsAsFactors = FALSE)

df <- aggregate(polyline ~ id, data = df, list)

google_map(key = map_key, height = 800) %>%
    add_polygons(data = df, polyline = "polyline")

## the same polygon, but using an 'id' to specify the polygon
df <- data.frame(id = c(1,1),
       polyline = c(pl_outer, pl_inner),
       stringsAsFactors = FALSE)

google_map(key = map_key, height = 800) %>%
    add_polygons(data = df, polyline = "polyline", id = "id")

## the same polygon, specified using coordinates, and with a second independent
## polygon
df <- data.frame(myId = c(1,1,1,1,1,1,2,2,2),
      lineId = c(1,1,1,2,2,2,1,1,1),
      lat = c(26.774, 18.466, 32.321, 28.745, 29.570, 27.339, 22, 23, 22),
      lon = c(-80.190, -66.118, -64.757, -70.579, -67.514, -66.668, -50, -49, -51),
      colour = c(rep("#00FF0F", 6), rep("#FF00FF", 3)),
      stringsAsFactors = FALSE)

google_map(key = map_key) %>%
  add_polygons(data = df, lat = 'lat', lon = 'lon', id = 'myId', pathId = 'lineId',
               fill_colour = 'colour')



# }
# NOT RUN {
# }

Run the code above in your browser using DataLab