Certainly! Here's the relevant code to handle the quantile classification issue in a single line, followed by your summarized code block:
agg_data <- data %>% group_by(state) %>% summarise(total_confirmed = sum(confirmed), total_recovered = sum(recovered), total_deaths = sum(death)) %>% ungroup(); breaks <- unique(quantile(agg_data$total_confirmed, probs = seq(0, 1, length.out = min(7, length(unique(agg_data$total_confirmed)) + 1)), na.rm = TRUE)); if(length(breaks) > 1) { agg_data$category <- cut(agg_data$total_confirmed, breaks = breaks, include.lowest = TRUE) } else { agg_data$category <- as.factor(agg_data$total_confirmed) }; map_data <- joinCountryData2Map(agg_data, joinCode = "NAME", nameJoinColumn = "state"); mapCountryData(map_data, nameColumnToPlot = "total_confirmed", mapTitle = "Global Distribution of COVID-19 Confirmed Cases", colourPalette = "heat")
Here is your summarized R script, incorporating the steps to clean the dataset, aggregate data, visualize data, handle geospatial data and time series forecasting, and produce summary statistics and heatmaps:
library(dplyr); library(lubridate); library(tidyr); library(ggplot2); library(sf); library(leaflet); library(htmlwidgets); library(prophet); library(RColorBrewer); library(rworldmap);
data <- data %>% mutate(state = replace_na(state, "Unknown"), date = as.Date(date), lat = as.numeric(lat), lon = as.numeric(lon)) %>% filter(lat >= -90 & lat <= 90 & lon >= -180 & lon <= 180 & !(confirmed == 0 & recovered == 0 & death == 0));
agg_data <- data %>% group_by(state) %>% summarise(total_confirmed = sum(confirmed), total_recovered = sum(recovered), total_deaths = sum(death)) %>% ungroup();
top_states_confirmed <- agg_data %>% arrange(desc(total_confirmed)) %>% head(10); ggplot(top_states_confirmed, aes(x=reorder(state, total_confirmed), y=total_confirmed)) + geom_bar(stat="identity", fill="steelblue") + coord_flip() + labs(title="Top 10 States by Confirmed Cases", x="State", y="Total Confirmed Cases");
top_states_deaths <- agg_data %>% arrange(desc(total_deaths)) %>% head(10); ggplot(top_states_deaths, aes(x=reorder(state, total_deaths), y=total_deaths)) + geom_bar(stat="identity", fill="red") + coord_flip() + labs(title="Top 10 States by Deaths", x="State", y="Total Deaths");
top_states_recovered <- agg_data %>% arrange(desc(total_recovered)) %>% head(10); ggplot(top_states_recovered, aes(x=reorder(state, total_recovered), y=total_recovered)) + geom_bar(stat="identity", fill="green") + coord_flip() + labs(title="Top 10 States by Recovered Cases", x="State", y="Total Recovered Cases");
data_sf <- st_as_sf(data, coords = c("lon", "lat"), crs = 4326); m_confirmed <- leaflet(data_sf) %>% addTiles() %>% addCircleMarkers(radius = ~log1p(confirmed + 1), color = "blue", popup = ~paste0("<strong>State: </strong>", state, "<br>", "<strong>Confirmed Cases: </strong>", confirmed, "<br>", "<strong>Recovered: </strong>", recovered, "<br>", "<strong>Deaths: </strong>", death)) %>% addLegend("bottomright", colors = c("blue"), labels = c("Confirmed Cases"), opacity = 0.5, title = "COVID-19 Cases"); saveWidget(m_confirmed, "Confirmed_Cases_Map.html");
time_series_data <- data %>% group_by(date) %>% summarise(total_confirmed = sum(confirmed)) %>% ungroup(); time_series_data <- na.omit(time_series_data);
prophet_data <- time_series_data %>% rename(ds = date, y = total_confirmed); model <- tryCatch({ prophet(prophet_data) }, error = function(e) { print("Error fitting model: "); print(e); return(NULL) });
if (!is.null(model) && inherits(model, "prophet")) { future <- make_future_dataframe(model, periods = 30); future <- future %>% mutate(ds = as.Date(ds, format = "%Y-%m-%d")); forecast <- predict(model, future); plot(model, forecast); prophet_plot_components(model, forecast) } else { print("Model fitting failed. Future dataframe and forecast were not created.") }
summary_stats <- agg_data %>% summarise(total_confirmed = sum(total_confirmed), total_recovered = sum(total_recovered), total_deaths = sum(total_deaths), mean_confirmed = mean(total_confirmed), mean_recovered = mean(total_recovered), mean_deaths = mean(total_deaths)); print(summary_stats);
ggplot(data, aes(x = lon, y = lat)) + geom_hex(bins = 30) + scale_fill_gradient(low = "blue", high = "red") + labs(title="Heatmap of COVID-19 Cases", x="Longitude", y="Latitude");
agg_data <- data %>% group_by(state) %>% summarise(total_confirmed = sum(confirmed), total_recovered = sum(recovered), total_deaths = sum(death)) %>% ungroup(); breaks <- unique(quantile(agg_data$total_confirmed, probs = seq(0, 1, length.out = min(7, length(unique(agg_data$total_confirmed)) + 1)), na.rm = TRUE)); if(length(breaks) > 1) { agg_data$category <- cut(agg_data$total_confirmed, breaks = breaks, include.lowest = TRUE) } else { agg_data$category <- as.factor(agg_data$total_confirmed) }; map_data <- joinCountryData2Map(agg_data, joinCode = "NAME", nameJoinColumn = "state"); mapCountryData(map_data, nameColumnToPlot = "total_confirmed", mapTitle = "Global Distribution of COVID-19 Confirmed Cases", colourPalette = "heat")