library(tidyverse)
library(plotly)
library(gridExtra)
library(sf)
library(maps)
library(knitr)
GTcountry <- readr::read_csv("Country.csv")
climate_data <- readxl::read_excel("Climate.xlsx") %>%
mutate(dt = as.Date(dt, format = "%Y-%m-%d"),
year = as.numeric(format(dt, "%Y")))
СРЕДНЕГОДОВОЙ ПРИРОСТ
temp_growth <- climate_data %>%
group_by(year) %>%
summarise(avg_temp = mean(LandAverageTemperature))
temp_growth$temperature_growth <- c(0, diff(temp_growth$avg_temp))
temp_ <- climate_data %>%
group_by(year) %>%
summarise(avg_temp = mean(LandAndOceanAverageTemperature))
temp_$temperature_growth <- c(0, diff(temp_$avg_temp))
d.temp <- tibble(
place = "Ocean",
year = 1901:2015,
d.temp = temp_$avg_temp[2:length(temp_$avg_temp)] - temp_$avg_temp[1:(length(temp_$avg_temp) - 1)])
d.temp <- tibble(
place = "Land",
year = 1901:2015,
d.temp = temp_growth$avg_temp[2:length(temp_growth$avg_temp)] - temp_growth$avg_temp[1:(length(temp_growth$avg_temp) - 1)]) %>%
rbind(d.temp)
p1 <- ggplot(d.temp, aes(year, d.temp, color = place)) +
geom_line() +
geom_point() +
geom_smooth(method = "lm", formula = "y~x") +
facet_wrap(~place)
print(p1)

ПРОГНОЗ ИЗМЕНЕНИЯ ТЕМПЕРАТУРЫ СУШИ И ОКЕАНА
linear_model <- lm(d.temp ~ year, data = d.temp)
new_years <- 2016:2115
new_data <- data.frame(year = new_years)
forecast_values1 <- predict(linear_model, newdata = new_data)
forecast_df <- data.frame(year = new_years, temperature = forecast_values1)
p2 <- ggplot() +
geom_line(data = d.temp, aes(x = year, y = d.temp), color = "blue")+
geom_line(data = forecast_df, aes(x = year, y = temperature), color ="red") +
labs(x = "Year", y = "Temperature",
title = "Прогноз изменения температуры") +
scale_color_manual(name = "Data Type",
values = c("blue", "red"),
labels = c("Existing Data", "Forecast")) +
facet_wrap(~place) +
theme_minimal()
print(p2)

СРАВНЕНИЕ ТЕМПОВ ИЗМЕНЕНИЯ КЛИМАТА В РАЗЛИЧНЫХ СТРАНАХ
country <- na.omit(GTcountry)
country_trends <- country %>%
group_by(Country) %>%
summarize(coefficient = coef(lm(AverageTemperature ~ dt))[2])
country_trends4 <- country_trends %>%
top_n(4)
ТОП-4 СТРАНЫ ПО ПРИРОСТУ ТЕМПЕРАТУРЫ
kable(country_trends4)
| French Southern And Antarctic Lands |
4.10e-05 |
| Heard Island And Mcdonald Islands |
3.71e-05 |
| Turkmenistan |
3.01e-05 |
| Uzbekistan |
3.06e-05 |
f <- country %>%
filter(Country %in% c("French Southern And Antarctic Lands","Heard Island And Mcdonald Islands","Turkmenistan","Uzbekistan"),
dt > as.Date("1900-01-01"))
model_ <- lm(AverageTemperature ~ dt + Country, data = f)
p4 <- ggplot(f, aes(x = dt, y = AverageTemperature, color = Country)) +
labs(x = "Дата", y = "Температура суши",
title = "Изменение климата в различных странах") +
geom_smooth(method = "lm", se = FALSE) +
theme(legend.position = "none") +
theme_minimal()
print(p4)

КАРТА СО СРАВНЕНИЕМ ПРИРОСТА ТЕМПЕРАТУРЫ
p3 <- map("world", fill = TRUE, plot = FALSE) %>%
st_as_sf() %>%
rename(Country = ID, geometry = geom) %>%
left_join(country_trends, by = "Country") %>%
ggplot(aes(geometry = geometry, fill = coefficient)) +
geom_sf() +
scale_fill_gradient(low = "blue", high = "red", name = "Value")
plotly::ggplotly(p3)