The goal of the earthquakedata package is to visualise the data in a timeline and using an interactive map:
eq_clean_data()
cleans the earhtquake data set and prepares it for visualisationgeom_timeline()
allows the user to view the earthquake data using ggplot2geom_timeline_label()
allows the user to view a specific amount of location labels on the timelinetimeline_theme()
a theme created for visualising the timelineeq_map()
lets the user plot the earthquake data on an interactive mapeq_create_label()
allows the user to see more detailed information regarding the earhtquake in the annotation text for each eventYou can install earthquakedata from github with:
# install.packages("devtools")
devtools::install_github("pvisser82/earthquakedata")
It is quite easy to clean your earthquake data set using the eq_clean_data()
function. This function takes the data set as a parameter and returns a cleaned data set.
For the example below to work, it is assumed that the raw data set is in your working directory.
df_cleaneddata <- eq_clean_data(readr::read_delim("earthquakes.tsv.gz",delim = "\t"))
Once you have cleaned data, it is handy to plot it using a timeline to display the earthquake events over a time period with additional dimensions for magnitude and number of casualties. You have the option to also add the locations of the earthquakes as labels.
Plot a timeline using the geom_timeline()
function. Set the aesthetics as follows:
- x = DATE
- y = COUNTRY
- size = EQ_PRIMARY
- colour = DEATHS
readr::read_delim("earthquakes.tsv.gz",delim = "\t") %>%
eq_clean_data() %>%
dplyr::filter(COUNTRY == c("USA") & lubridate::year(DATE) >= 2000) %>%
ggplot() +
geom_timeline(aes(x = DATE, y = COUNTRY,size = EQ_PRIMARY, colour = DEATHS))
It is possible to set the number of labels using the geom_timeline_label()
function is conjunction with the geom_timeline()
function. This will allow you to set the n_max
variable. This will display the labels for the n_max
number of earthquakes with the highest magnitude.
readr::read_delim("earthquakes.tsv.gz",delim = "\t") %>%
eq_clean_data() %>%
dplyr::filter(COUNTRY == c("USA") & lubridate::year(DATE) >= 2000) %>%
ggplot() +
geom_timeline(aes(x = DATE, y = COUNTRY,size = EQ_PRIMARY, colour = DEATHS)) +
geom_timeline_label(aes(x = DATE, y = COUNTRY, label = LOCATION_NAME, size = EQ_PRIMARY), n_max = 5) +
ggtitle("Earthquake Timeline") +
theme_timeline() +
labs(size = "Richter Scale value:", colour = "# of Deaths:")
By specifying multiple countries in the data set, it is possible to create more than one timeline on the same graph. The labels can be enabled or disabled as with single countries
readr::read_delim("earthquakes.tsv.gz",delim = "\t") %>%
eq_clean_data() %>%
dplyr::filter(COUNTRY == c("USA", "MEXICO") & lubridate::year(DATE) >= 2000) %>%
ggplot() +
geom_timeline(aes(x = DATE, y = COUNTRY,size = EQ_PRIMARY, colour = DEATHS)) +
geom_timeline_label(aes(x = DATE, y = COUNTRY, label = LOCATION_NAME, size = EQ_PRIMARY), n_max = 5) +
ggtitle("Earthquake Timeline") +
theme_timeline() +
labs(size = "Richter Scale value:", colour = "# of Deaths:")
Because the earthquake data also contains the coordinates of the earthquakes, it is possible to plot them on an interactive Leaflet map using the eq_map()
function. You can also use the eq_create_label()
function to display more information for each earthquake.
The eq_map()
function is used to plot the location of each earthquake on the map. The size of the circles represent the magnitude of the earthquakes. The user can set the annot_col
parameter to any column in the earthquake data set. This will allow the user to see the value of that column for the corresponding earthquake by clicking on the circle.
readr::read_delim("earthquakes.tsv.gz",delim = "\t") %>%
eq_clean_data() %>%
dplyr::filter(COUNTRY == "MEXICO" & lubridate::year(DATE) >= 2000) %>%
eq_map(annot_col = "DATE")
The eq_create_label()
function takes the data set as an argument and creates an HTML label that can be displayed on the map. The label consists of the Location, Magnitude and Total deaths. If one of these fields are missing a value, the field is ignored when the label is built.
readr::read_delim("earthquakes.tsv.gz",delim = "\t") %>%
eq_clean_data() %>%
dplyr::filter(COUNTRY == "MEXICO" & lubridate::year(DATE) >= 2000) %>%
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.