devtools::load_all()
This package contains 3 sets of functions you can use to work more efficiently with NOAA earthquakes datasets. Thoses sets are:
- Loading (eq_clean_data());
- Plotting on a timeline (geom_timeline() and geom_timeline_label());
- Plotting on a map (eq_map() and eq_create_label()).
All of those functions are described below.
eq_clean_data <- function(data = NULL, filename = NULL, replaceDateNA_by = c(NA, NA, "00", "00", "00"))
This function will return a clean dataframe from 3 kinds of inputs:
- If no argument is set, dataset "quake" will be used as a base data.frame (an extraction from NOAA database, the 15th of July 2017)
- "data" can be used to start from preloaded raw NOAA earthquake data.frame
- "filename" can be used to start from a local file, downloaded from NOAA earthquakes website
As this library is mostly used to plot earthquakes in time, some functions will require a date input. But depending on the earthquake recording, all date elements might not be avaliable to reconstitute a complete date (eg. we have year and month but not day, hour, min and sec).
The replacedDateNA_by vector can be used to replace missing date elements by default values, which will allow date computations. Specify in order c(MONTH, DAY, HOUR, MINUTE, SECOND).
geom_timeline <- function(data, colors = c("light blue", "blue", "black"), alpha = 0.75, size = NULL, xmindate = NULL, xmaxdate = NULL)
This function will return a ggplot2 graph which plots earthquakes in time, for different countries.
db <- utils::head(eq_clean_data(),500) geom_timeline(db)
Parameters will help to customize the plot depending on what you want to plot:
- data takes a cleaned database
- colors takes a color vector, which corresponds to color gradient used in legend to represent #TOTAL_DEATHS. If you don't want to analyze #TOTAL_DEATHS, set color as a single value (eg. "blue"") and #TOTAL_DEATHS legend will disappear.
- alpha represents the transparency of plotted dots (1 for no transparency, 0 for total transparency).
- size represents a fixed size you can set if you don't want dots' size to be proportionnal to EQ_PRIMARY, size legend will disappeaer.
- xmindate and xmaxdate are Date values you can set to zoom on a certain period of time. Be aware that it will be a zoom: values outisde xmindate/xmaxdate range will disappear but color scales will behave as if they were still present. If you don't want that to happen, please use a subsetted data.frame with the time period you want to plot.
Following example shows a simplified timeline graph (the same as above but with other parameters):
db <- utils::head(eq_clean_data(),500) geom_timeline(db, colors = "blue", alpha = 1, size = 5)
Following example shows a zoom:
db <- utils::head(eq_clean_data(),500) geom_timeline(db, xmindate = as.Date(lubridate::ymd_hms("1100-01-01 01:00:00")), xmaxdate = as.Date(lubridate::ymd_hms("1300-01-01 01:00:00")))
Note that you'll always get an x axis with 5 time breaks.
geom_timeline_label <- function(dtb, n_max = 10, label = "LOCATION_NAME")
This function will add labels to an existing ggplot2 timeline plot.
db <- utils::head(eq_clean_data(),500) geom_timeline(db)+geom_timeline_label(db, n_max = 5)
Parameters will help to customize the label depending on what you want to plot:
- dtb takes a cleaned database
- n_max sets the number of earthquakes to plot a label on
- label is the name of the column to use to define the label's text
Following example shows the same graph with only 2 labels, and EQ_PRIMARY as label:
db <- utils::head(eq_clean_data(),500) geom_timeline(db)+geom_timeline_label(db, n_max = 2, label = "EQ_PRIMARY")
eq_map<-function(data, annot_col = "popup_text", max_rad = 8, ...)
This function will create a leaflet map widget plotting earthquakes that are contained in data:
eq_map(head(quakes,5))
Parameters will help to customize the map:
- data takes a cleaned database
- annot_col takes the name of the column to add in popup text. If the column doesn't exist, or if "popup_text" is set, popups will contain the HTML character string produced by eq_create_label() - see below.
- max_rad takes the maximal radius a plotted earthquake should have on the map
- ... allows to pass argument to leaflet
Below, a map example using "EQ_primary" in annotation popups
eq_map(head(quakes,5), annot_col = "EQ_PRIMARY")
eq_create_label<-function(data)
This function will create an HTML code to fill map popups. This code corresponds to following structure:
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" )
It will print a popup rendering like:
Location: LOCATION
Magintude: MAGINTUDE
Total deaths: TOTAL_DEATHS
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.