##################################################
## Geom for plotting a time line of earthquakes ##
##################################################
#'
#' This function plots a time line of earthquakes from \code{data}
#' generated by U.S. National Oceanographic and Atmospheric Administration
#' (NOAA) database.
#'
#' @param data A preproccessed tibble (data.frame) of NOAA earthquakes
#' dataset
#' @param xmindate a character vector indicating the mininum date specified for
#' earthquakes time line plotting. Should follow the format of YYYY-MM-DD.
#' @param xmaxdate a character vector indicating the maximum date specified
#' for earthquakes time line plotting. Should follow the format of YYYY-MM-DD.
#' @param country a vector of character strings for the countries of interest.
#' By default it shows earthquake data related to USA.
#'
#' @return This function returns a ggplot
#'
#' @import ggplot2
#' @import dplyr
#'
#' @examples
#' NOAA_clean_data = eq_clean_data("https://www.ngdc.noaa.gov/nndc/struts/results?type_0=Exact&query_0=$ID&t=101650&s=13&d=189&dfn=signif.txt")
#' geom_timeline(NOAA_clean_data, xmindate = "1999-01-01", xmaxdate = "2017-12-31", country = c("CHINA", "USA", "JAPAN"))
#'
#' @export
geom_timeline = function(data, xmindate, xmaxdate, country = c("USA")) {
data_filtered = filter_date_and_country(data, xmindate, xmaxdate, country)
ggplot2::ggplot(data_filtered, aes(x = DATE, y = COUNTRY,
size = EQ_MAG_MW, color = DEATHS)) +
ggplot2::geom_point(alpha=0.4)+
ggplot2::theme(legend.position = "bottom",
legend.direction = "horizontal",
legend.box = "horizontal",
legend.key = element_rect(fill = "white"),
axis.title.y = element_blank(),
panel.background = element_rect(fill = "white", colour = NA),
axis.line = element_line(colour = "black"),
axis.line.y = element_line(colour = "white"),
axis.ticks.y = element_line(colour = "white"))+
ggplot2::labs(size = "Richter scale value", color = "# deaths")
}
#' Geom for plotting a time line of earthquakes with annotations
#'
#' This function plots a time line of earthquakes from \code{data}
#' U.S. National Oceanographic and Atmospheric Administration
#' (NOAA) database. This geom adds a vertical line to each data point with
#' a text annotation (e.g. the location of the earthquake) attached to each line.
#'
#' @param data A preproccessed tibble (data.frame) of NOAA earthquakes
#' dataset
#' @param xmindate a character vector indicating the mininum date specified for
#' earthquakes time line plotting. Should follow the format of YYYY-MM-DD.
#' @param xmaxdate a character vector indicating the maximum date specified
#' for earthquakes time line plotting. Should follow the format of YYYY-MM-DD.
#' @param country a vector of character strings for the countries of interest.
#' By default it shows earthquake data related to USA.
#' @param n_max a numeric value for maximum number of earthquakes to be cosidered
#' for location annotations
#'
#' @return This function returns a ggplot
#'
#' @import ggplot2
#' @import dplyr
#'
#' @examples
#' NOAA_clean_data = eq_clean_data("https://www.ngdc.noaa.gov/nndc/struts/results?type_0=Exact&query_0=$ID&t=101650&s=13&d=189&dfn=signif.txt")
#' geom_timeline_label(NOAA_clean_data, xmindate = "1999-01-01", xmaxdate = "2017-12-31", country = c("CHINA", "USA"), n_max = 7.2)
#'
#' @export
geom_timeline_label = function(data, xmindate, xmaxdate, country = c("USA"), n_max){
data_filtered = filter_date_and_country(data, xmindate, xmaxdate, country)
ggplot2::ggplot(data_filtered, aes(x = DATE, y = COUNTRY,
size = EQ_MAG_MW,
color = DEATHS,
label=LOCATION_NAME)) +
ggplot2::geom_point(alpha=0.4)+
ggplot2::theme(legend.position = "bottom",
legend.direction = "horizontal",
legend.box = "horizontal",
legend.key = element_rect(fill = "white"),
axis.title.y = element_blank(),
panel.background = element_rect(fill = "white", colour = NA),
axis.line = element_line(colour = "black"),
axis.line.y = element_line(colour = "white"),
axis.ticks.y = element_line(colour = "white"))+
ggplot2::labs(size = "Richter scale value", color = "# deaths")+
ggplot2::geom_text(data=data_filtered[data_filtered$EQ_MAG_MW > n_max,],
aes(x = DATE,y = COUNTRY, angle = 30, size = 5),
nudge_y = 0.1, hjust = 0, show.legend = FALSE)
}
#' Plot earthquakes on the map
#'
#' This function plots earthquakes from \code{data}
#' generated by U.S. National Oceanographic and Atmospheric Administration
#' (NOAA) database on the map. The function maps the epicenters
#' (LATITUDE/LONGITUDE) and annotates each point with in pop up window
#' containing annotation data stored in a column of the data frame.
#'
#' @param data A preproccessed tibble (data.frame) of NOAA earthquakes
#' dataset
#' @param annot_col column used for the annotation in the pop-up
#'
#' @return This function returns a ggplot
#'
#' @import dplyr
#' @importFrom lubridate year
#' @importFrom leaflet leaflet addTiles addCircleMarkers
#'
#' @examples
#' library(dplyr)
#' NOAA_clean_data = eq_clean_data("https://www.ngdc.noaa.gov/nndc/struts/results?type_0=Exact&query_0=$ID&t=101650&s=13&d=189&dfn=signif.txt")
#' NOAA_clean_data %>% dplyr::filter(COUNTRY == "MEXICO" & lubridate::year(DATE) >= 2000) %>% eq_map(annot_col = "DATE")
#' NOAA_clean_data %>% dplyr::filter(COUNTRY == "MEXICO" & lubridate::year(DATE) >= 2000) %>% dplyr::mutate(popup_text = eq_create_label(.)) %>% eq_map(annot_col = "popup_text")
#'
#' @export
eq_map = function(data, annot_col) {
leaflet::leaflet(data = data) %>%
leaflet::addTiles() %>%
leaflet::addCircleMarkers(~LONGITUDE, ~LATITUDE, radius = ~EQ_PRIMARY,
popup = ~as.character(data[[annot_col]]),
fillOpacity = 0.3, weight = 1)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.