Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- install.packages('tidyverse')
- install.packages('ggthemes')
- install.packages('plotly')
- install.packages('dplyr')
- library('tidyverse')
- library('ggthemes')
- library('plotly')
- library('dplyr')
- install.packages('factoextra')
- install.packages('cluster')
- library(factoextra)
- library(cluster)
- install.packages('maps', dependencies = TRUE)
- library(ggplot2)
- require(maps)
- df<-read.csv('https://raw.githubusercontent.com/annstasi/R/main/world-happiness-report-2021.csv', stringsAsFactors = F)
- df <- subset(df, select=c("п.їCountry.name", "Regional.indicator", "Ladder.score", "Logged.GDP.per.capita", "Social.support", "Healthy.life.expectancy", "Freedom.to.make.life.choices", "Generosity", "Perceptions.of.corruption", "Dystopia...residual"))
- df <- df %>% rename( Country=п.їCountry.name, Region=Regional.indicator, Happiness_score=Ladder.score, GDP=Logged.GDP.per.capita, Social_support=Social.support, Life_expectancy=Healthy.life.expectancy, Freedom=Freedom.to.make.life.choices, Corruption=Perceptions.of.corruption, Dystopia_residual=Dystopia...residual)
- str(df)
- # Стандартизация данных
- data_ST <- scale(df[,-(1:2)])
- head(data_ST)
- set.seed(1234)
- countries <- kmeans(data_ST, 4)
- countries1 <- df
- countries1$cluster <- as.factor(countries$cluster) # Добавление колонки с кластерами
- head(countries1,3)
- # Уровень счастья по кластерам (1 самый большой, 3 меньший)
- countries_avg <- countries1 %>%
- group_by(cluster) %>%
- summarize_if(is.numeric, mean, na.rm = TRUE)
- countries_avg[,1:2]
- countries_max <- countries1 %>%
- group_by(cluster) %>%
- summarize_if(is.numeric, max, na.rm = TRUE)
- countries_max[,1:2]
- countries_min <- countries1 %>%
- group_by(cluster) %>%
- summarize_if(is.numeric, min, na.rm = TRUE)
- countries_min[,1:2]
- world_map <- map_data("world") # Информация по странам
- head(world_map,3)
- countries1$Country[countries1$Country == "United Kingdom"] <- "UK"
- countries1$Country[countries1$Country == "United States"] <- "USA"
- countries1$Country[countries1$Country == "Taiwan Province of China"] <- "Taiwan"
- countries1$Country[countries1$Country == "North Cyprus"] <- "Cyprus"
- countries1$Country[countries1$Country == "Hong Kong S.A.R. of China"] <- "China"
- countries1$Country[countries1$Country == "Congo (Brazzaville)"] <- "Republic of Congo"
- countries1$Country[countries1$Country == "Palestinian Territories"] <- "Palestine"
- countries_by_cluster <- countries1[,c("Country", "cluster")]
- countries_by_cluster$cluster <- as.numeric(countries_by_cluster$cluster)
- # Объединяем данные
- clustering_map <- left_join(countries_by_cluster, world_map, by = c("Country" = "region"), keep = TRUE)
- clustering_map %>% filter (is.na(region))
- all.layer <- geom_polygon(data = world_map, aes(long, lat, group = group))
- ggplot(clustering_map, aes(long, lat, group = group))+
- all.layer +
- geom_polygon(aes(fill = cluster)) +
- scale_fill_viridis_c(option = "C", labels=c("5.48-7.84", "4.52-7.07", "3.14-5.38", "2.52-5.34")) # разбить на кластеры по цветам
- options(repr.plot.width=15, repr.plot.height=8) # изменить размер карты
- # Рисуем карту
- thismap <- mutate(world_map, fill = ifelse(region %in% clustering_map$Country, clustering_map$cluster, "white"))
- scale_fill_viridis_c(option = "C") # разбить на кластеры по цветам
- # Use scale_fiil_identity to set correct colors
- ggplot(thismap, aes(long, lat, fill = fill, group=group)) +
- geom_polygon(colour="black") + ggtitle("Map of World") #+
- #scale_fill_identity()
Add Comment
Please, Sign In to add comment