library(dplyr) library(ggplot2) library(leaflet) library(devtools) devtools::load_all()
This package provides functions and geometries to manipulate and visualize in a clean and comprehensive way the information related to earthquakes from the NOAA' database.
This package was built as fulfillment project to the "Mastering Software Development in R Capstone" course from Coursera.org on July 2017.
The first step is to get the data set and clean it for further use. Earthquakes data can be downloaded from NOAA' web page.
#utils::data(rawdata) #rawdata_file <- system.file("extdata", "rawdata.csv", package = "courseraeq") #rawdata <- readr::read_csv(rawdata_file) colnames(rawdata) data <- eq_clean_data(rawdata)
The function eq_clean_data
create a Date field and make sure that relevant variables are in numeric format.
This function also call eq_location_clean
to clean the location name of where the earthquake occurred.
An example is as follow:
eq_location_clean("JORDAN: BAB-A-DARAA,AL-KARAK")
A convenient way of visualizing the earthquakes is by plotting a timeline with a point for each earthquake,
using the ggplot2
geometry geom_timeline()
.
The size and color of the points can be assigned to other properties: by default size represent the
earthquake magnitude and color the number of deaths.
Each line can be associated to a category through the aesthetic y
. By default the category is Country.
The additional geometry geom_timeline_label()
adds a vertical line and a label to identify the earthquakes.
The parameter n_max
defines how many label are plotted, to avoid overplotting.
A dedicated theme theme_timeline()
has been designed to improve the visualization. It is a modification of
the theme_classic()
.
data %>% dplyr::filter(COUNTRY == "USA" | COUNTRY == "MEXICO", YEAR > 2000) %>% ggplot2::ggplot(ggplot2::aes(x = DATE, y = COUNTRY)) + geom_timeline(ggplot2::aes(size = EQ_PRIMARY, color = DEATHS), fill = NA) + geom_timeline_label(ggplot2::aes(label = LOCATION_NAME, size = EQ_PRIMARY), n_max = 5) + theme_timeline() + ggplot2::labs(size = "Richter Scale value:", color = "# of Deaths:")
Another way of visualizing the earthquakes is by use of an interactive map, obtained with the leaflet
package.
The function eq_map
plot a map and a point for each earthquake. The size of the point is proportional to the
earthquake magnitude. Earthquakes with no magnitude information, which is often the case for old earthquakes,
are plotted, but with a small grey dot.
Additional information can be displayed in a pop up window.
data %>% dplyr::filter(COUNTRY == "MEXICO" & lubridate::year(DATE) >= 2000) %>% eq_map(annot_col = "DATE")
For an interactive version of the map, check the html version of this vignette.
A further improvement is provided by the function eq_create_label
which built the HTML code to be shown in the
pop up, by combining date, location, magnitude and number of deaths.
It can be called as follows:
data %>% dplyr::filter(COUNTRY == "ITALY" & lubridate::year(DATE) >= 1000) %>% dplyr::mutate(popup_text = eq_create_label(.)) %>% eq_map(annot_col = "popup_text")
For an interactive version of the map, check the html version of this vignette.
This package is released under MIT license. Check the license file for details.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.