Spaces:
Build error
Build error
File size: 6,495 Bytes
23c2526 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
```{r}
install.packages(c("sf", "raster"))
# Load packages
```
```{r}
install.packages(c("dplyr"))
```
```{r}
# Load necessary libraries
library(sf)
library(ggplot2)
library(rnaturalearth)
library(rnaturalearthdata)
# Read your CSV data
data <- read.csv("GIS_Purpose.csv")
```
```{r}
data_clean <- na.omit(data)
# Convert the cleaned data frame to an sf object, specifying the coordinates and CRS (Coordinate Reference System)
data_sf <- st_as_sf(data_clean, coords = c("lon", "lat"), crs = 4326)
# Get world map data
world <- ne_countries(scale = "medium", returnclass = "sf")
# Plot the world map with points from your data
my_plot <- ggplot(data = world) +
geom_sf() + # This plots the world map as a base layer
geom_sf(data = data_sf, aes(color = Severity), size = 0.4) + # This adds your points on top
theme_minimal() +
labs(title = "Spatial Distribution of Incidents with World Map Basemap") +
theme(legend.position = "right") # Adjust legend position if needed
# Save the plot to a file
ggsave("my_spatial_plot.png", plot = my_plot, width = 10, height = 8, dpi = 300)
```
```{r}
library(lubridate)
library(ggplot2)
library(forecast)
# Check for NA values and remove them
data <- na.omit(data)
# Aggregate data by month
data$Month <- floor_date(data$Datetime, "month")
monthly_incidents <- aggregate(Index ~ Month, data, length)
# Make sure that there are no NA values
monthly_incidents <- na.omit(monthly_incidents)
# Assuming that you've verified the 'monthly_incidents' dataframe and it looks correct
# Create a time series object, checking the start and end values
start_year <- min(year(monthly_incidents$Month), na.rm = TRUE)
start_month <- min(month(monthly_incidents$Month), na.rm = TRUE)
end_year <- max(year(monthly_incidents$Month), na.rm = TRUE)
end_month <- max(month(monthly_incidents$Month), na.rm = TRUE)
# Check if start date is after end date
if (make_date(start_year, start_month) > make_date(end_year, end_month)) {
stop("'start' cannot be after 'end'")
}
# Now create the time series object
ts_data <- ts(monthly_incidents$Index, frequency=12, start=c(start_year, start_month))
```
```{r}
plot(ts_data, main = "Monthly Incidents Time Series", xlab = "Time", ylab = "Number of Incidents", col = "blue")
```
```{r}
decomposed_data <- decompose(ts_data)
plot(decomposed_data)
```
```{r}
incidents_by_severity <- aggregate(Index ~ Severity, data = data, FUN = length)
# Visualize the number of incidents by Severity
ggplot(incidents_by_severity, aes(x = Severity, y = Index, fill = Severity)) +
geom_bar(stat = "identity") +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
labs(x = "Severity", y = "Frequency", title = "Frequency of Incidents by Severity")
```
```{r}
# Assuming 'data' is your dataframe and 'Severity' is the column with the severity level
# First, count the frequency of each severity level
severity_counts <- table(data$Severity)
# Convert the names of the table (the severity levels) to numeric ranks
severity_ranks <- as.numeric(factor(names(severity_counts),
levels = c("Minor", "Moderate", "Severe", "Extreme")))
# Perform Spearman's rank correlation test between severity ranks and their frequencies
cor.test(severity_ranks, severity_counts, method = "spearman")
```
```{r}
# Assuming 'data' is your data frame and 'Category' is the column with incident types
category_counts <- table(data$Category)
top_categories <- sort(category_counts, decreasing = TRUE)[1:5]
```
```{r}
# Convert table to data frame for filtering
top_categories_df <- as.data.frame(top_categories)
# Filter your original data for only top categories
top_data <- data[data$Category %in% names(top_categories), ]
```
```{r}
library(dplyr)
library(ggplot2)
library(maps)
# Assuming 'data' is your data frame, 'Category' is the column with incident types, and 'lon', 'lat' are your longitude and latitude columns
# Calculate counts of incidents for each category at each location
top_data <- data %>%
count(Category, lon, lat) %>%
filter(Category %in% names(top_categories))
# Get world map data
world_map <- map_data("world")
# Create the plot
plot <- ggplot(data = world_map, aes(x = long, y = lat)) +
geom_polygon(aes(group = group), fill = "gray80", color = "white") +
geom_point(data = top_data, aes(x = lon, y = lat, color = Category, size = n), alpha = 0.7) +
scale_size(range = c(4, 16)) + # Adjust the size range as needed
scale_color_brewer(palette = "Dark2") +
labs(title = "Top 5 Categories of Incidents on World Map",
subtitle = "Size of point represents frequency of incidents",
size = "Number of Incidents") +
theme_minimal() +
theme(legend.position = "bottom")
# Save the plot
ggsave("incident_map.png", plot = plot, width = 20, height = 10, dpi = 300)
```
```{r}
library(dplyr)
library(ggplot2)
library(maps)
library(scales) # For more refined control over point sizes
# Assuming 'data' is your data frame, 'Category' is the column with incident types, and 'lon', 'lat' are your longitude and latitude columns
# Calculate counts of incidents for each category at each location
top_data <- data %>%
count(Category, lon, lat) %>%
filter(Category %in% names(top_categories)) %>%
mutate(size = sqrt(n)) # Use square root scaling for point sizes
# Get world map data
world_map <- map_data("world")
# Create the plot with improved aesthetics
incident_map <- ggplot(data = world_map, aes(x = long, y = lat)) +
geom_polygon(aes(group = group), fill = "lightblue", color = "white") + # Use a different fill color for water
geom_point(data = top_data, aes(x = lon, y = lat, color = Category, size = size), alpha = 0.6) +
scale_size_continuous(trans = "identity", range = c(1, 12)) + # Use identity transformation and adjust the size range
scale_color_brewer(palette = "Dark2", name = "Category") +
labs(title = "Top 5 Categories of Incidents on World Map",
subtitle = "Size of point represents frequency of incidents",
size = "Frequency (sqrt scale)") + # Updated legend title to reflect sqrt scaling
coord_quickmap() + # Use an equirectangular projection
theme_minimal() +
theme(legend.position = "bottom",
legend.key.size = unit(0.5, "cm")) # Adjust legend key size for better appearance
# Save the plot using the new variable name
ggsave("incident_map_refined.png", plot = incident_map, width = 12, height = 8, dpi = 300) # Adjusted dimensions for a better aspect ratio
```
|