#' This function generates a leaflet map with circles at each earthquake's location. Should be used with the output of \code{\link{eq_location_clean}}, but will work with any dataframe with \code{DATE}, \code{LONGITUDE}, \code{LATITUDE} and \code{MAG} columns.
#'
#' @param data dataframe with \code{DATE}, \code{LONGITUDE}, \code{LATITUDE} and \code{MAG} columns.
#'
#' @param annot_col character vector, indicating the column containing information to be displayed inside of the popup.
#'
#' @importFrom magrittr %>%
#'
#' @importFrom leaflet leaflet addTiles addCircles
#'
#'
#' @return A map with earthquakes as circles.
#'
#' @examples
#'
#' \dontrun{
#'
#' # Full example.
#' eq_clean_data() %>%
#' dplyr::filter(!is.na(TOTAL_DEATHS)) %>%
#' dplyr::select(DATE, YEAR, COUNTRY, REGION, LONGITUDE, LATITUDE, MAG, TOTAL_DEATHS) %>%
#' tidyr::drop_na() %>%
#' dplyr::filter(COUNTRY == "MEXICO" & YEAR >= 2000) %>%
#' eq_map()
#' }
#'
#'
#' @export
eq_map <- function(data, annot_col = "DATE") {
if(all(c("LONGITUDE","LATITUDE","MAG", annot_col) %in% colnames(data))){
leaflet::leaflet() %>%
leaflet::addTiles() %>%
leaflet::addCircleMarkers(
lng = data[["LONGITUDE"]],
lat = data[["LATITUDE"]],
radius = data[["MAG"]],
weight = 1,
popup = data[[annot_col]]
)
} else {
stop("Required column missing from input data")
}
}
#' This function creates HTML to be displayed in the pops generated by the \code{\link{eq_map}} function.
#'
#' @param data dataframe with \code{LOCATION}, \code{MAG}, and \code{TOTAL_DEATHS} columns
#'
#' @importFrom stringr str_c str_replace_na
#'
#' @return vector containing HTML
#'
#' @examples
#'
#' \dontrun{
#' eq_clean_data() %>%
#' dplyr::filter(!is.na(TOTAL_DEATHS)) %>%
#' dplyr::select(DATE, YEAR, COUNTRY, REGION, LONGITUDE, LATITUDE, MAG, TOTAL_DEATHS) %>%
#' tidyr::drop_na() %>%
#' dplyr::filter(COUNTRY == "MEXICO" & YEAR >= 2000) %>%
#' dplyr::mutate(popup_text = eq_create_label(.)) %>%
#' eq_map(annot_col = "popup_text")
#' }
#'
#' @export
eq_create_label <- function(data) {
if(all(c("REGION","TOTAL_DEATHS","MAG") %in% colnames(data))){
loc <- stringr::str_c("<b>Location:</b>", data[["REGION"]],"<br>") %>%
stringr::str_replace_na("")
mag <- stringr::str_c("<b>Magnitude:</b>", data[["MAG"]],"<br>") %>%
stringr::str_replace_na("")
dead <- stringr::str_c("<b>Total Deaths:</b>", data[["TOTAL_DEATHS"]],"<br>") %>%
stringr::str_replace_na("")
stringr::str_c(loc, mag, dead)
} else {
stop("Missing columns needed to generate labels")
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.