knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
The package enables two kind of vizualizations:
There are several objects which support this kind of visualization:
geom_timeline()
- the geometry which allows plotting time linesgeom_timeline_label()
- the geometry to annotate remarkable casestheme_timeline()
- quite a minimalistic theme to satisfy the assignment requirements for displaying datatheme_timeline()
- Follows the style of the assignmenJust use it in order to follow the style of the Coursera's assignment
geom_timeline()
- Plots time linesThis geom plots a time line of earthquakes ranging from xmin
to xmax
dates (which are optional) with a point for each earthquake. Optional aesthetics also include color
, size
, and alpha
(for transparency). The required x
aesthetic is a date and an optional y
aesthetic is a factor indicating some stratification in which case multiple time lines will be plotted for each level of the factor (e.g. country).
geom_timeline_label
- Annotates the most prominent casesThis geom adds a vertical line to each data point with a text annotation (e.g. the location of the earthquake) attached to each line. There is an option to subset to n_max
number of earthquakes, where we take the n_max
largest (by magnitude) earthquakes.
Aesthetics are x
, which is the date of the earthquake and label
which takes the column name from which annotations will be obtained. If n_max
is omitted, each earthquake is annotated
Let's consider some examples.
library(Earthquakes4Coursera) library(dplyr) library(ggplot2) NOAAC <- file_read() %>% eq_clean_data() NOAAC %>% dplyr::filter(COUNTRY == "USA", between(lubridate::year(DATE), 2000, 2017)) %>% ggplot(aes(x = DATE, color = DEATHS, size = EQ_PRIMARY)) + geom_timeline() + theme_timeline() + labs(size = "Richter scale value", colour = "# deaths") + geom_timeline_label(aes(label = LOCATION_NAME), n_max = 5)
I cannot come up with any reason why we may want to filter data inside the geom rather than a step before like we did for year and country, but let's check hoq it works. I'm going to add an additional filtering using the xmin and xmax parameters of the geom:
NOAAC %>% dplyr::filter(COUNTRY == "USA", between(lubridate::year(DATE), 2000, 2017)) %>% ggplot(aes(x = DATE, color = DEATHS, size = EQ_PRIMARY)) + geom_timeline(xmin = eq_get_date(2002, 8, 1), xmax = eq_get_date(2014, 12, 31)) + theme_timeline() + labs(size = "Richter scale value", colour = "# deaths") + geom_timeline_label(aes(label = LOCATION_NAME), n_max = 5)
We can see that the earthquakes beyond the bounds of [nmin
, nmax
] are not shown.
Now let's stratify the earthquakes by country and set n_max
= 3
NOAAC %>% dplyr::filter(COUNTRY %in% c("USA", "CHINA"), between(lubridate::year(DATE), 2008, 2011)) %>% ggplot(aes(x = DATE, y = COUNTRY, color = DEATHS, size = EQ_PRIMARY)) + geom_timeline() + theme_timeline() + labs(size = "Richter scale value", color = "# deaths") + geom_timeline_label(aes(label = LOCATION_NAME), n_max = 3)
Just for fun (actually to show how it works with BC dates)
NOAAC %>% dplyr::filter( COUNTRY %in% c("ISRAEL", "ITALY", "CHINA"), between(lubridate::year(DATE), -70, 70)) %>% ggplot(aes(x = DATE, y = COUNTRY, color = DEATHS, size = EQ_PRIMARY)) + geom_timeline() + theme_timeline() + labs(size = "Richter scale value", colour = "# deaths") + geom_timeline_label(aes(label = LOCATION_NAME))
There are several objects which support this kind of visualization:
eq_map()
- the main function to visualize data on a mapeq_create_label()
- assembles an annotation text for a pop-upeq_create_label()
- Creates an annotation text for a pop-upThe function combines the following information about an earthquake:
If an earthquake is missing values for any of these items, it is skipped for that element of the tag.
eq_map()
- Visualizes earthquakes on a mapThe function takes an argument eq_data
containing the data frame with earthquakes to visualize. 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.
The user is able to choose which column to use for the annotation in the pop-up with a function argument named annot_col
. Each earthquake is shown with a circle whose radius is proportional to the earthquake's magnitude.
Let's show on the map the earthquake in Mexico that have happen since 2000. Just chose one of the columns (let say, DATE
) as an annotation text to show in a pop-up window.
NOAAC %>% dplyr::filter(COUNTRY == "MEXICO" & lubridate::year(DATE) >= 2000) %>% eq_map(annot_col = "DATE")
Let's now show the earthquakes that have happened in Russia since 2010. In this time we are going to use eq_create_label()
to annotate earthqakes with additional information about "Location", "Total deaths", and "Magnitude"
NOAAC %>% dplyr::filter(COUNTRY == "RUSSIA" & lubridate::year(DATE) >= 2010) %>% dplyr::mutate(popup_text = eq_create_label(.)) %>% eq_map(annot_col = "popup_text")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.