#code
#***********************************
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.3.2
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(readr)
## Warning: package 'readr' was built under R version 4.3.2
library(lubridate)
## Warning: package 'lubridate' was built under R version 4.3.2
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
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.
data <- mutate(data, time = ymd_hms(time))
data <- select(data, -magType, -nst, -gap, -dmin, -rms, -net, -id, -updated, -place,
-horizontalError, -depthError, -magError, -magNst, -status, -locationSource, -magSource)
data <- mutate(data, days = day(time))
data <- mutate(data, day_type = ifelse(weekdays(time) %in% c("Saturday", "Sunday"), "Weekend", "Weekday"))
data <- data %>%
group_by(year = lubridate::year(time)) %>%
mutate(earthquakes_per_year = n()) %>%
ungroup()
data <- mutate(data,
earthquake_zone = case_when(
between(latitude, 7.2055, 83.1107) & between(longitude, -168.0875, -52.6228) ~ "North America",
between(latitude, -55.9794, 12.4681) & between(longitude, -81.2892, -67.2696) ~ "South America",
TRUE ~ "Others" # For events outside North and South America
))
View(data)
My answer is written here and is explains what I did and why.
I decided to create a single data set file, which can be used for all the following questions. For that I deleted unnecessary columns,Standardized the “time” column to a consistent format,Add a column for weekday/weekend,Add a column for number of earthquakes per year, Add a column to categorize the earthquake zone, on the basis of which i could determine the earthquake zone are in north america or south. The latitude and longitude used for north america and south america are as between(latitude, 7.2055, 83.1107) & between(longitude, -168.0875, -52.6228) and between(latitude, -55.9794, 12.4681) & between(longitude, -81.2892, -67.2696) respectively. In the end, I used view function to confirm the output. 2. Did more earthquakes happen on weekends or weekdays? Include a figure \[5 marks\]
#code
#***********************************
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.3.2
ggplot(data, aes(x = day_type)) +
geom_bar(fill = "skyblue", color = "black") +
labs(title = "Earthquakes Occurrence by Day Type",
x = "Day Type",
y = "Count") +
theme_minimal()
The above analysis demonstrates… working for this data set is worked out in answer of question 1. Above analysis denotes that, clearly earthquakes have occurred more in number on weekdays as compared to weekends.
#code
#***********************************
library(ggplot2)
earthquakes_per_year <- data %>%
group_by(year) %>%
summarise(total_earthquakes = n())
ggplot(earthquakes_per_year, aes(x = year, y = total_earthquakes)) +
geom_line(color = "blue") +
geom_point(color = "blue", size = 2) +
labs(title = "Frequency of Earthquakes Over Time",
x = "Year",
y = "Number of Earthquakes") +
theme_minimal()
The above analysis demonstrates… working for this data set is worked out in answer of question 1. Number of earthquakes were highest in the year 2011 and were the lowest in the year 1961.We can see a along the years, there has been a steady increase in number of earthquakes over time.
#code
#***********************************
library(ggplot2)
earthquakes_1990s_na_sa <- data %>%
filter(year >= 1990 & year <= 1999) %>%
filter(earthquake_zone %in% c("North America", "South America"))
earthquakes_count_na_sa <- earthquakes_1990s_na_sa %>%
group_by(earthquake_zone) %>%
summarise(total_earthquakes = n())
ggplot(earthquakes_count_na_sa, aes(x = earthquake_zone, y = total_earthquakes, fill = earthquake_zone)) +
geom_bar(stat = "identity") +
labs(title = "Earthquakes in North America vs South America (1990s)",
x = "Earthquake Zone",
y = "Total Earthquakes") +
theme_minimal() +
scale_fill_manual(values = c("North America" = "skyblue", "South America" = "lightyellow"))
The above analysis demonstrates… working for this data set is worked out in answer of question 1. Working for the seperation of earthquakes from 1990 - 99 is worked out here. The graph demonstrates that number of earthquakes in north america is more than south america in the 1990s.
#code
#***********************************
earthquakes_per_year <- data %>%
group_by(year) %>%
summarise(total_earthquakes = n())
ggplot(earthquakes_per_year, aes(x = year, y = total_earthquakes)) +
geom_line(color = "blue") +
geom_point(color = "blue") +
labs(title = "Earthquakes Per Year",
x = "Year",
y = "Total Earthquakes") +
theme_minimal()
earthquake_type_counts <- data %>%
count(type) %>%
arrange(desc(n))
ggplot(earthquake_type_counts, aes(x = reorder(type, n), y = n)) +
geom_bar(stat = "identity", fill = "skyblue") +
geom_text(aes(label = n), vjust = -0.5, color = "black", size = 3) + # Add count numbers above the bars
labs(title = "Distribution of Earthquake Types",
x = "Type",
y = "Count") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
average_magnitude_per_year <- data %>%
mutate(year = lubridate::year(time)) %>%
group_by(year) %>%
summarise(avg_magnitude = mean(mag, na.rm = TRUE))
ggplot(average_magnitude_per_year, aes(x = year, y = avg_magnitude)) +
geom_line(color = "blue") +
geom_point(color = "blue") +
labs(title = "Average Earthquake Magnitude Per Year",
x = "Year",
y = "Average Magnitude") +
theme_minimal()
The above analysis demonstrates… Earthquakes per year - Maximum earthquake per year have occurred in year 2011 and minimum number in year 1961. Also there is gradual increase in the number of earthquakes along the year. Distribution of Earthquake Types - The majority of the earthquakes have been caused by tectonic plate movements. Earthquakes were also caused due to nuclear explosions. Earthquakes were also caused because of explosion and volcanic eruption, but in single digit only. Magnitude Intensity -