The package can generate a simple point map with one or more layers. First we can get the sample data:
library(svis) ## Get the sample data pts <- sample_data() ## This is a SpatialPointsDataFrame and then then be ploted with the sp package library(sp) plot(pts)
This data also contains covariates in the pts@data
slot , which describes
each point in the data.
str(pts@data[,1:7])
The map contains a function that runs on each feature that returns a popuptext.
We need to define this in the data as a variable named pts@data$popup_text
.
We will put the Status of the dot in that variable so we can see that text
in the map popup.
pts@data$popup_text <- pts@data$Status head(pts@data$Status)
Now we can make a layer with those points and put it in a map
## Convert to layer object layer1 <- layer(pts) ## Generate the page header header <- page_header() ## Map with a single layer: body <- page_body(map_div(layer1)) page <- page_page(header, body) map <- tempfile(fileext = "html") capture.output(page, file = map)
Then view the page
browseURL(map)
## Tricky here: During the build of thé vignette, I place the map file ## in the do folder. Then include it in an iframe as the basename(map) ## because when you view the vignette you are working in the local ## dir. map <- "../inst/doc/map1.html" capture.output(page, file = map) knitr::include_url(basename(map), height = "1000px")
## The first layer pts <- sample_data("cwd") pts@data$popup_text <- pts@data$Status ## And a second layer pts2 <- sample_data("asf") ## Add some popup text pts2@data$popup_text <- paste(pts2@data[,"K\u00F6n"], pts2@data$Status)
Now we can make a layer with those points and put it in a map
## Convert to layer objects with 2 different names: layer1 <- layer(pts, layer_title = "CWD") layer2 <- layer(pts2, layer_title = "ASF", fillColor = shQuote("#ff0000")) layersob <- layers(list(layer1, layer2)) ## Generate the page header header <- page_header() ## Map with a single layer: body <- page_body(map_div(layersob)) page <- page_page(header, body) map <- tempfile(fileext = "html") capture.output(page, file = map)
Then view the page
browseURL(map)
map <- "../inst/doc/map2.html" capture.output(page, file = map) knitr::include_url(basename(map), height = "1000px")
pts <- sample_data() ## Your variable of interest should be a factor pts@data$Djurslag <- as.factor(pts@data$Djurslag) ## We'll put the species in the popuptext too pts@data$popup_text <- pts@data$Djurslag ## Create a layer layer1 <- layer(pts, layer_title = "CWD", byvar = "Djurslag") ## Plot it: header <- page_header() body <- page_body(map_div(layer1)) page <- page_page(header, body) map <- tempfile(fileext = "html") capture.output(page, file = map)
Then view the page
browseURL(map)
map <- "../inst/doc/map3.html" capture.output(page, file = map) knitr::include_url(basename(map), height = "1000px")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.