annstasi

R wold map

Oct 23rd, 2022
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 3.64 KB | None | 0 0
  1. install.packages('tidyverse')
  2. install.packages('ggthemes')
  3. install.packages('plotly')
  4. install.packages('dplyr')
  5. library('tidyverse')
  6. library('ggthemes')
  7. library('plotly')
  8. library('dplyr')
  9. install.packages('factoextra')
  10. install.packages('cluster')
  11. library(factoextra)
  12. library(cluster)
  13. install.packages('maps', dependencies = TRUE)
  14. library(ggplot2)
  15. require(maps)
  16.  
  17. df<-read.csv('https://raw.githubusercontent.com/annstasi/R/main/world-happiness-report-2021.csv', stringsAsFactors = F)
  18. 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"))
  19. 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)
  20.  
  21. str(df)
  22. # Стандартизация данных
  23. data_ST <- scale(df[,-(1:2)])
  24. head(data_ST)
  25.  
  26. set.seed(1234)
  27. countries <- kmeans(data_ST, 4)
  28.  
  29. countries1 <- df
  30. countries1$cluster <- as.factor(countries$cluster) # Добавление колонки с кластерами
  31.  
  32. head(countries1,3)
  33.  
  34. # Уровень счастья по кластерам (1 самый большой, 3 меньший)
  35. countries_avg <- countries1 %>%
  36.   group_by(cluster) %>%
  37.   summarize_if(is.numeric, mean, na.rm = TRUE)
  38.  
  39.  
  40. countries_avg[,1:2]
  41.  
  42. countries_max <- countries1 %>%
  43.   group_by(cluster) %>%
  44.   summarize_if(is.numeric, max, na.rm = TRUE)
  45. countries_max[,1:2]
  46.  
  47. countries_min <- countries1 %>%
  48.   group_by(cluster) %>%
  49.   summarize_if(is.numeric, min, na.rm = TRUE)
  50. countries_min[,1:2]
  51.  
  52. world_map <- map_data("world") # Информация по странам
  53. head(world_map,3)
  54.  
  55. countries1$Country[countries1$Country == "United Kingdom"] <- "UK"
  56. countries1$Country[countries1$Country == "United States"] <- "USA"
  57. countries1$Country[countries1$Country == "Taiwan Province of China"] <- "Taiwan"
  58. countries1$Country[countries1$Country == "North Cyprus"] <- "Cyprus"
  59. countries1$Country[countries1$Country == "Hong Kong S.A.R. of China"] <- "China"
  60. countries1$Country[countries1$Country == "Congo (Brazzaville)"] <- "Republic of Congo"
  61. countries1$Country[countries1$Country == "Palestinian Territories"] <- "Palestine"
  62.  
  63. countries_by_cluster <- countries1[,c("Country", "cluster")]
  64. countries_by_cluster$cluster <- as.numeric(countries_by_cluster$cluster)
  65. # Объединяем данные
  66. clustering_map <- left_join(countries_by_cluster, world_map, by = c("Country"  = "region"), keep = TRUE)
  67.  
  68. clustering_map %>% filter (is.na(region))
  69.  
  70.  
  71. all.layer <- geom_polygon(data = world_map, aes(long, lat, group = group))
  72.  
  73. ggplot(clustering_map, aes(long, lat, group = group))+
  74.   all.layer +
  75.   geom_polygon(aes(fill = cluster)) +
  76.   scale_fill_viridis_c(option = "C", labels=c("5.48-7.84", "4.52-7.07", "3.14-5.38", "2.52-5.34")) # разбить на кластеры по цветам
  77. options(repr.plot.width=15, repr.plot.height=8) # изменить размер карты
  78.  
  79.  
  80.  
  81. # Рисуем карту
  82. thismap <- mutate(world_map, fill = ifelse(region %in% clustering_map$Country, clustering_map$cluster, "white"))
  83. scale_fill_viridis_c(option = "C") # разбить на кластеры по цветам
  84.  
  85. # Use scale_fiil_identity to set correct colors
  86. ggplot(thismap, aes(long, lat, fill = fill, group=group)) +
  87.   geom_polygon(colour="black") + ggtitle("Map of World") #+
  88. #scale_fill_identity()
  89.  
Add Comment
Please, Sign In to add comment