## Warning: package 'readr' was built under R version 4.3.2
## Rows: 28835 Columns: 22
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (8): magType, net, id, place, type, status, locationSource, magSource
## dbl (12): latitude, longitude, depth, mag, nst, gap, dmin, rms, horizontalE...
## dttm (2): time, updated
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
## spc_tbl_ [28,835 × 22] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
## $ time : POSIXct[1:28835], format: "1960-01-02 12:21:59" "1960-01-03 11:24:06" ...
## $ latitude : num [1:28835] -55.88 43.7 11.37 6.42 -55.59 ...
## $ longitude : num [1:28835] -1.89 84.54 42.61 94.76 -27.28 ...
## $ depth : num [1:28835] 15 15 15 15 35 15 15 35 15 10 ...
## $ mag : num [1:28835] 6.31 5.69 6.07 5.56 6.23 5.52 5.88 6.01 5.64 5.84 ...
## $ magType : chr [1:28835] "mw" "mw" "mw" "mw" ...
## $ nst : num [1:28835] NA NA NA NA NA NA NA NA NA NA ...
## $ gap : num [1:28835] NA NA NA NA NA NA NA NA NA NA ...
## $ dmin : num [1:28835] NA NA NA NA NA NA NA NA NA NA ...
## $ rms : num [1:28835] NA NA NA NA NA NA NA NA NA NA ...
## $ net : chr [1:28835] "iscgem" "iscgem" "iscgemsup" "iscgem" ...
## $ id : chr [1:28835] "iscgem877909" "iscgem877920" "iscgemsup877930" "iscgem877954" ...
## $ updated : POSIXct[1:28835], format: "2022-04-26 16:25:46" "2022-04-26 16:25:48" ...
## $ place : chr [1:28835] "Bouvet Island region" "108 km ENE of Xinyuan, China" "26 km NNW of 'Ali Sabieh, Djibouti" "85 km NW of Sabang, Indonesia" ...
## $ type : chr [1:28835] "earthquake" "earthquake" "earthquake" "earthquake" ...
## $ horizontalError: num [1:28835] NA NA NA NA NA NA NA NA NA NA ...
## $ depthError : num [1:28835] 25 8 25 25 25 44.9 11.3 25 25 12.8 ...
## $ magError : num [1:28835] 0.37 0.2 1.07 0.25 0.2 0.29 0.23 0.22 0.2 0.2 ...
## $ magNst : num [1:28835] NA NA NA NA NA NA NA NA NA NA ...
## $ status : chr [1:28835] "reviewed" "reviewed" "reviewed" "reviewed" ...
## $ locationSource : chr [1:28835] "iscgem" "iscgem" "iscgemsup" "iscgem" ...
## $ magSource : chr [1:28835] "iscgem" "iscgem" "iscgemsup" "iscgem" ...
## - attr(*, "spec")=
## .. cols(
## .. time = col_datetime(format = ""),
## .. latitude = col_double(),
## .. longitude = col_double(),
## .. depth = col_double(),
## .. mag = col_double(),
## .. magType = col_character(),
## .. nst = col_double(),
## .. gap = col_double(),
## .. dmin = col_double(),
## .. rms = col_double(),
## .. net = col_character(),
## .. id = col_character(),
## .. updated = col_datetime(format = ""),
## .. place = col_character(),
## .. type = col_character(),
## .. horizontalError = col_double(),
## .. depthError = col_double(),
## .. magError = col_double(),
## .. magNst = col_double(),
## .. status = col_character(),
## .. locationSource = col_character(),
## .. magSource = col_character()
## .. )
## - attr(*, "problems")=<externalptr>
## time latitude longitude
## Min. :1960-01-02 12:21:59.72 Min. :-77.080 Min. :-180.00
## 1st Qu.:1978-12-01 13:39:24.60 1st Qu.:-19.204 1st Qu.: -77.78
## Median :1994-12-27 17:33:23.62 Median : -3.533 Median : 101.42
## Mean :1994-01-30 13:00:48.25 Mean : 1.661 Mean : 37.37
## 3rd Qu.:2009-04-08 00:07:15.83 3rd Qu.: 26.859 3rd Qu.: 144.65
## Max. :2023-12-30 17:16:24.91 Max. : 86.923 Max. : 180.00
##
## depth mag magType nst
## Min. : -4.00 Min. :5.500 Length:28835 Min. : 0.0
## 1st Qu.: 13.00 1st Qu.:5.600 Class :character 1st Qu.:130.2
## Median : 33.00 Median :5.720 Mode :character Median :233.0
## Mean : 68.96 Mean :5.879 Mean :260.3
## 3rd Qu.: 51.90 3rd Qu.:6.000 3rd Qu.:364.0
## Max. :700.00 Max. :9.500 Max. :934.0
## NA's :5 NA's :20949
## gap dmin rms net
## Min. : 8.00 Min. : 0.005 Min. :0.005 Length:28835
## 1st Qu.: 24.20 1st Qu.: 1.187 1st Qu.:0.880 Class :character
## Median : 36.00 Median : 2.574 Median :1.000 Mode :character
## Mean : 44.21 Mean : 4.361 Mean :0.994
## 3rd Qu.: 54.52 3rd Qu.: 5.147 3rd Qu.:1.100
## Max. :360.00 Max. :39.730 Max. :3.220
## NA's :18335 NA's :24006 NA's :8204
## id updated place
## Length:28835 Min. :2013-11-15 03:59:43.47 Length:28835
## Class :character 1st Qu.:2022-04-26 23:23:24.75 Class :character
## Mode :character Median :2022-04-28 18:19:52.98 Mode :character
## Mean :2021-12-14 13:54:10.51
## 3rd Qu.:2022-05-02 18:13:14.44
## Max. :2024-01-02 19:12:00.64
##
## type horizontalError depthError magError
## Length:28835 Min. : 0.085 Min. : 0.000 Min. :0.000
## Class :character 1st Qu.: 5.800 1st Qu.: 1.805 1st Qu.:0.059
## Mode :character Median : 7.100 Median : 3.900 Median :0.200
## Mean : 7.258 Mean : 6.503 Mean :0.208
## 3rd Qu.: 8.550 3rd Qu.: 6.600 3rd Qu.:0.300
## Max. :99.000 Max. :569.200 Max. :1.640
## NA's :24452 NA's :16335 NA's :20602
## magNst status locationSource magSource
## Min. : 0.0 Length:28835 Length:28835 Length:28835
## 1st Qu.: 18.0 Class :character Class :character Class :character
## Median : 32.0 Mode :character Mode :character Mode :character
## Mean : 47.4
## 3rd Qu.: 55.0
## Max. :941.0
## NA's :23050
# Exploring unique types of earthquakes
unique_types <- unique(earthquakes$type)
print(unique_types)## [1] "earthquake" "nuclear explosion" "explosion"
## [4] "volcanic eruption"
## Warning: Unknown or uninitialised column: `source`.
## NULL
## [1] "time" "latitude" "longitude" "depth"
## [5] "mag" "magType" "nst" "gap"
## [9] "dmin" "rms" "net" "id"
## [13] "updated" "place" "type" "horizontalError"
## [17] "depthError" "magError" "magNst" "status"
## [21] "locationSource" "magSource"
# Exploring unique location sources
unique_location_sources <- unique(earthquakes$locationSource)
print(unique_location_sources)## [1] "iscgem" "iscgemsup" "ushis" "official" "hv" "ci"
## [7] "us" "a" "h" "u" "g" "nc"
## [13] "ag" "e" "b" "uw" "doe" "spe"
## [19] "brk" "ags" "pgc" "bou" "ak" "ott"
## [25] "uu" "ren" "casc" "isk" "ath" "the"
## [31] "rom" "gcmt" "mdd" "wel" "guc" "unm"
## [37] "csem" "aeic" "rspr" "jma" "nn" "car"
## [43] "sja" "teh" "beo" "ucr" "se" "tul"
## [49] "tap" "thr" "lim" "us_wel" "pr" "pt"
# Exploring unique magnitude sources
unique_mag_sources <- unique(earthquakes$magSource)
print(unique_mag_sources)## [1] "iscgem" "iscgemsup" "official" "rot" "brk" "pal"
## [7] "mat" "mos" "hv" "pas" "ci" "us"
## [13] "isc" "1020" "nc" "1000" "gcmt" "1009"
## [19] "uw" "1023" "hrv" "ak" "par" "uu"
## [25] "duputel" "nied" "nn" "se" "pgc" "us_gcmt"
## [31] "us_pgc" "pr" "guc" "pt"
## time latitude longitude depth mag
## 0 0 0 5 0
## magType nst gap dmin rms
## 0 20949 18335 24006 8204
## net id updated place type
## 0 0 0 245 0
## horizontalError depthError magError magNst status
## 24452 16335 20602 23050 0
## locationSource magSource
## 0 0
# Removing unnecessary columns
earthquakes <- earthquakes[, !names(earthquakes) %in% c("net", "id", "updated")]#I used the read_csv() function from the readr package to read the earthquake data from the CSV file because it is a convenient and efficient function for reading CSV files into R.
#I used the str() function to check the structure of the data, which helps me understand the types of variables in the dataset and how they are organized.
#I used the head() function to view the first few rows of the data, which provides a quick overview of the dataset and helps me understand its contents.
#I used the unique() function to explore unique values in relevant columns such as ‘magType’, ‘type’, ‘locationSource’, and ‘magSource’. This allows me to understand the different categories and sources of information present in the dataset.
#Finally, I provided comments on the types of earthquakes and their sources of information based on the unique values explored, which helps to interpret the data and understand its significance.
#The ‘magType’ column indicates the type of magnitude measurement used for earthquakes. #The ‘type’ column categorizes earthquakes based on their nature (e.g., ‘earthquake’, ‘nuclear explosion’). #The ‘locationSource’ column specifies the organization responsible for reporting the earthquake location. #The ‘magSource’ column identifies the organization responsible for reporting the earthquake magnitude.
## Warning: package 'tidyverse' was built under R version 4.3.2
## Warning: package 'ggplot2' was built under R version 4.3.2
## Warning: package 'tibble' was built under R version 4.3.2
## Warning: package 'tidyr' was built under R version 4.3.2
## Warning: package 'purrr' was built under R version 4.3.2
## Warning: package 'dplyr' was built under R version 4.3.2
## Warning: package 'stringr' was built under R version 4.3.2
## Warning: package 'forcats' was built under R version 4.3.2
## Warning: package 'lubridate' was built under R version 4.3.2
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ purrr 1.0.2
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.4.4 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(lubridate)
start_date <- "1960-01-02T12:21:58.720Z"
end_date <- "2023-12-30T17:16:24.908Z"
# Converting start and end dates
start_date <- ymd_hms(start_date)
end_date <- ymd_hms(end_date)
# Generating a sequence of dates from start to end date
date_sequence <- seq.Date(as.Date(start_date), as.Date(end_date), by = "day")
# Calculating the number of days since January 2, 1960 for each date
days_since_1960 <- as.numeric(date_sequence - as.Date("1960-01-02"))
# Calculating the day of the week for each date
day_of_week <- weekdays(date_sequence)
# Combining dates, days since 1960, and day of the week into a dataframe
earthquakes_df <- data.frame(date = date_sequence, days_since_1960 = days_since_1960, day_of_week = day_of_week)
# Counting the number of earthquakes by day of the week
earthquakes_count <- earthquakes_df %>%count(day_of_week) %>%mutate(category = ifelse(day_of_week %in% c("Saturday", "Sunday"), "Weekend", "Weekday"))
# Plot the distribution of earthquakes by day of the week
ggplot(earthquakes_count, aes(x = category, y = n, fill = category)) +
geom_bar(stat = "identity") + labs(x = "Category", y = "Number of Earthquakes",title = "Distribution of Earthquakes by Day of the Week") +
scale_fill_manual(values = c("Weekday" = "yellow", "Weekend" = "brown")) +theme_minimal()#I loaded the necessary libraries for data manipulation and visualization. Then, I defined the start and end dates, converted them to the appropriate format, and generated a sequence of dates between them.
#Next, I calculated the day of the week for each date and categorized them as either “Weekday” or “Weekend”.
#Finally, I plotted the distribution of earthquakes by day of the week using a bar plot. The plot showed that more earthquakes happened on weekdays compared to weekends, as indicated by the higher count of earthquakes on weekdays in the dataset.
#code
#***********************************
# To Read the dataset
earthquakes_data <- read_csv("https://jjvenky.github.io/GG606AW24/data/earthquakes_5.5_1960-2023.csv")## Rows: 28835 Columns: 22
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (8): magType, net, id, place, type, status, locationSource, magSource
## dbl (12): latitude, longitude, depth, mag, nst, gap, dmin, rms, horizontalE...
## dttm (2): time, updated
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# Converting time column to POSIXct format
earthquakes_data$time <- ymd_hms(earthquakes_data$time)
# Extracting year from the time column
earthquakes_data <- earthquakes_data %>%mutate(year = year(time))
# Counting the number of earthquakes per year
earthquakes_by_year <- earthquakes_data %>% group_by(year) %>%summarize(num_earthquakes = n())
# Plotting the number of earthquakes per year with trend line and annotations
ggplot(earthquakes_by_year, aes(x = year, y = num_earthquakes)) +geom_line(color = "skyblue") + geom_smooth(method = "lm", se = FALSE, color = "red") + geom_text(data = subset(earthquakes_by_year, year %in% c(1970, 1990, 2010, 2023)),aes(label = num_earthquakes), hjust = -0.1, vjust = -0.5, size = 3) + labs(x = "Year", y = "Number of Earthquakes", title = "Frequency of Earthquakes Over Time",caption = "Annotations indicate peak years") + theme_minimal()## `geom_smooth()` using formula = 'y ~ x'
#First, I read the earthquake dataset and converted the time column to the POSIXct format to extract the year from it. Then, I calculated the number of earthquakes per year and plotted the trend over time using a line plot.
#To visualize the trend more clearly, I added a trend line using linear regression and annotated specific years (1970, 1990, 2010, and 2023) to highlight peak earthquake occurrences.
#The plot indicates that there has been a fluctuation in the frequency of earthquakes over the years, with some years experiencing higher seismic activity than others. The trend line suggests a slight increase in earthquake frequency over time. Overall, while there are fluctuations, the general trend does not show a significant change in the frequency of earthquakes over the examined period.
#code
#***********************************
# Loading necessary libraries
library(dplyr)
# Converting time column to datetime format
earthquakes_data$time <- ymd_hms(earthquakes_data$time)
# Extracting year from the time column
earthquakes_data$year <- year(earthquakes_data$time)
# Filtering the dataset to include only earthquakes from the 1990s
earthquakes_1990s <- earthquakes_data %>%filter(year >= 1990 & year <= 1999)
# Defining longitude and latitude ranges for North America and South America according to google
north_america_longitude_range <- c(-169.0, -55.0)
north_america_latitude_range <- c(7.0, 83.0)
south_america_longitude_range <- c(-90.0, -35.0)
south_america_latitude_range <- c(-56.0, 12.0)
# Filtering earthquakes for North America and South America
earthquakes_na <- earthquakes_1990s %>%
filter(longitude >= north_america_longitude_range[1] & longitude <= north_america_longitude_range[2],
latitude >= north_america_latitude_range[1] & latitude <= north_america_latitude_range[2])
earthquakes_sa <- earthquakes_1990s %>%
filter(longitude >= south_america_longitude_range[1] & longitude <= south_america_longitude_range[2],latitude >= south_america_latitude_range[1] & latitude <= south_america_latitude_range[2])
# Counting earthquakes in North America and South America
earthquakes_count <- tibble(Continent = c("North America", "South America"), Count = c(nrow(earthquakes_na), nrow(earthquakes_sa)))
# Plotting the count of earthquakes in each continent
library(ggplot2)
ggplot(earthquakes_count, aes(x = Continent, y = Count, fill = Continent)) + geom_bar(stat = "identity") + labs(title = "Number of Earthquakes in North America and South America (1990s)",x = "Continent",y = "Number of Earthquakes",fill = "Continent") + theme_minimal()#Loading Necessary Libraries: The dplyr library is loaded, which provides functions for data manipulation.
#Converting Time Column: The ymd_hms() function from the lubridate package is used to convert the time column in the dataset to datetime format. This ensures that the time information is properly recognized.
#Extracting Year: The year() function is applied to extract the year component from the datetime values in the time column. This allows us to filter earthquakes by year.
#Filtering Dataset for the 1990s: The dataset is filtered to include only earthquakes from the 1990s using the filter() function. This ensures that we focus on earthquakes that occurred within the specified timeframe.
#Defining Longitude and Latitude Ranges: Longitude and latitude ranges are defined for North America and South America based on information obtained from Google.
#Filtering Earthquakes for Each Continent: Using the longitude and latitude ranges, earthquakes are filtered separately for North America (earthquakes_na) and South America (earthquakes_sa).
#Counting Earthquakes: The number of earthquakes in North America and South America during the 1990s is counted and stored in a tibble called earthquakes_count.
#Plotting the Counts: A bar plot is created using ggplot2 to visualize the count of earthquakes in each continent. The x-axis represents the continents, the y-axis represents the number of earthquakes, and each continent is filled with a different color for clarity.
#Based on the plot and the counts, it’s evident that South America had more earthquakes during the 1990s compared to North America. This conclusion is drawn from the higher count of earthquakes in South America as depicted by the bar plot.
#code
#***********************************
# Extracting years from the time column
earthquakes_data$year <- lubridate::year(earthquakes_data$time)
# Count the number of earthquakes per year
earthquakes_per_year <- earthquakes_data %>%
group_by(year) %>%
summarise(num_earthquakes = n())
# Plot the trend of earthquakes per year with a smooth line
ggplot(earthquakes_per_year, aes(x = year, y = num_earthquakes)) +
geom_line(color = "red") +
geom_smooth(method = "loess", se = FALSE, color = "blue") +
labs(title = "Trend of Earthquakes per Year",
x = "Year",
y = "Number of Earthquakes") +
theme_minimal()## `geom_smooth()` using formula = 'y ~ x'
# Extract relevant columns for earthquake type analysis
earthquakes_type <- earthquakes_data %>% group_by(year, type) %>%
summarise(num_earthquakes = n())## `summarise()` has grouped output by 'year'. You can override using the
## `.groups` argument.
# Plot the distribution of earthquake types over time using a stacked area plot
ggplot(earthquakes_type, aes(x = year, y = num_earthquakes, fill = type)) +
geom_area(position = "stack", color = "white") +
labs(title = "Distribution of Earthquake Types over Time",
x = "Year", y = "Number of Earthquakes", fill = "Type") +
theme_light() # Change the theme to theme_light()#Extracting years from the time column: The code utilizes the lubridate package to extract the year component from the ‘time’ column in the earthquake data.
#Plotting the trend of earthquakes per year: It creates a line plot using ggplot2 to visualize the trend of earthquakes per year. The x-axis represents the year, and the y-axis represents the number of earthquakes. The plot uses a red line to connect the data points.And then add a smooth line to demonstrate the trend.
#Extracting relevant columns for earthquake type analysis: The code groups the earthquake data by year and earthquake type and then calculates the number of earthquakes for each type in each year.
#Plotting the distribution of earthquake types over time: It creates a stacked area plot using ggplot2 to show the distribution of earthquake types over time. The x-axis represents the year, the y-axis represents the number of earthquakes, and different colors represent different earthquake types. The plot uses a light theme (theme_light()).
Explanation:
#References:
#For the figure “Distribution of Earthquakes by Day of the Week”:
Visualizing Amounts: The figure represents the count of earthquakes on different days of the week, focusing on the magnitude of the quantitative values.
Bar Plot: The use of a bar plot aligns with Wilke’s discussion in Chapter 6, which emphasizes bars for visualizing numerical values associated with categories.
#For the figure “Frequency of Earthquakes Over Time”:
This code snippet draws inspiration from Chapter 13 of Wilke’s book, which discusses visualizing time series data using line graphs. Specifically, it utilizes the principles outlined in Section 13.1, where the author explains the significance of visualizing temporal order through line graphs. By representing the trend of monthly preprint submissions over time, as demonstrated in Figure 13.1 and 13.2, the code emphasizes the increase in submission volume while maintaining the temporal structure of the data.
#For the figure Number of Earthquakes in North America and South America Bar Plot: The use of a bar plot aligns with Wilke’s discussion in Chapter 6, which emphasizes bars for visualizing numerical values associated with categories.
#For the figure Trend of Earthquakes per year From Chapter 14 of Wilke’s book, “Fundamentals of Data Visualization”, which discusses visualizing trends in data. Specifically, it incorporates the concept of smoothing data to focus on overarching trends while de-emphasizing short-term fluctuations, as outlined in Section 14.1. By plotting the trend of earthquakes per year with a smooth line, the code aims to highlight the long-term patterns in earthquake occurrences. Additionally, it draws inspiration from Chapter 13, which discusses visualizing time series data using line graphs, particularly in Section 13.1 where the significance of visualizing temporal order through line graphs is explained. This helps maintain the temporal structure of the data while showcasing the trend of earthquakes over time.
#For the figure Distribution of Earthquake Types over Time This code demonstrates the application of principles outlined in Chapter 2 of Wilke’s book, “Fundamentals of Data Visualization”, particularly focusing on aesthetics and scales. It maps earthquake data onto various aesthetics to create a stacked area plot illustrating the distribution of earthquake types over time. The mapping of earthquake count onto the y-axis and year onto the x-axis represents the use of position scales, which is a fundamental aspect of data visualization discussed in Section 2.2 of the book. Additionally, the use of color aesthetics to represent different earthquake types aligns with the concept of mapping data values onto aesthetics, as discussed in the same section.