Learn R Programming

googleway (version 2.0.0)

add_polylines: Add polyline

Description

Add a polyline to a google map

Usage

add_polylines(map, data = get_map_data(map), polyline = NULL, lat = NULL,
  lon = NULL, id = NULL, geodesic = NULL, stroke_colour = NULL,
  stroke_weight = NULL, stroke_opacity = NULL, info_window = NULL,
  mouse_over = NULL, mouse_over_group = 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 polyline

geodesic

logical

stroke_colour

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

stroke_weight

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

stroke_opacity

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

info_window

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

mouse_over

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

mouse_over_group

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

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

Examples

Run this code
# NOT RUN {
## using lat/lon coordinates

map_key <- "your_api_key"

google_map(data = tram_route, key = map_key) %>%
  add_polylines(lat = "shape_pt_lat", lon = "shape_pt_lon")


## using encoded polyline and various colour / fill options
url <- 'https://raw.githubusercontent.com/plotly/datasets/master/2011_february_aa_flight_paths.csv'
flights <- read.csv(url)
flights$id <- seq_len(nrow(flights))


## encode the routes as polylines
lst <- lapply(unique(flights$id), function(x){
  lat = c(flights[flights["id"] == x, c("start_lat")], flights[flights["id"] == x, c("end_lat")])
  lon = c(flights[flights["id"] == x, c("start_lon")], flights[flights["id"] == x, c("end_lon")])
  data.frame(id = x, polyline = encode_pl(lat = lat, lon = lon))
})

flights <- merge(flights, do.call(rbind, lst), by = "id")

style <- map_styles()$night

google_map(key = map_key, style = style) %>%
  add_polylines(data = flights, polyline = "polyline", mouse_over_group = "airport1",
               stroke_weight = 1, stroke_opacity = 0.3, stroke_colour = "#ccffff")


# }

Run the code above in your browser using DataLab