#' Display earthquakes on an interactive map
#'
#' @param data A NOAA dataframe as created with eq_clean_data()
#' @param annot_col The name of the column to print in annotation as character
#' @param max_rad The maximum radius of a circle as numeric
#' @param ... Additionnal parameters to pass to leaflet (see leaflet doc)
#'
#' @import leaflet
#'
#' @return An interactive map (as leaflet widget)
#' @export
#'
#' @examples
#' eq_map(head(quakes,5))
eq_map<-function(data, annot_col = "popup_text", max_rad = 8, ...){
#Define the popup to print
if(annot_col %in% colnames(data)){
if(annot_col == "date"){
popup <- paste0(
"<b>",
annot_col,
":</b> ",
as.character(
unlist(
base::format(data$date, format = "%Y-%m-%d")
)))
} else {
#Either one of data columns
popup <- paste0(
"<b>",
annot_col,
":</b> ",
as.character(unlist(data[,annot_col])))
}
} else {
#Or else a default popup
popup <- eq_create_label(data)
}
#Computes a radius scale
max_radius <- max(data$EQ_PRIMARY, na.rm = TRUE)
radius_factor <- max_rad/max_radius
#Print on a map
map <- leaflet::leaflet() %>%
leaflet::addTiles() %>%
leaflet::addCircleMarkers(lng = data$LONGITUDE, lat = data$LATITUDE,
radius = data$EQ_PRIMARY*radius_factor,
popup = popup, color = "blue", fill = "blue", ...)
return(map)
}
#' Create a HTML label with Location, Total Death and Magnitude informations
#'
#' @param data A NOAA dataframe as created with eq_clean_data()
#'
#' @return A character vector containing every HTML label to print
#' @export
#'
#' @examples
#' eq_map(head(quakes))
eq_create_label<-function(data){
location <- paste0(
"<b>Location: </b>",
data$LOCATION_NAME,
"<br>\n"
)
magnitude <- paste0(
"<b>Magnitude: </b>",
data$EQ_PRIMARY,
"<br>\n"
)
deaths <- paste0(
"<b>Total deaths: </b>",
data$TOTAL_DEATHS,
"<br>\n"
)
return(paste0(location, magnitude, deaths))
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.