努力在R中绘制绘图和Map[关闭]

6jygbczu  于 2023-05-20  发布在  其他
关注(0)|答案(1)|浏览(166)

已关闭,此问题需要更focused。目前不接受答复。
**想改善这个问题吗?**更新问题,使其仅通过editing this post关注一个问题。

6天前关闭。
Improve this question
所以我刚刚开始学习R编程,我的任务是从数据集中创建一个对国家进行分类的直方图:“世界幸福报告”(可用性:8.53)从Kaggle网站下载。
我应该根据他们的幸福分数对这些国家进行分类。我必须将它们分组,例如分数0- 2,5 =不快乐,2,6 -5 =快乐等等。
我试着查找这个并下载了几个软件包(ggplot 2,tidyverse,dplyr),但由于我现在只学习了不到一周的R,我不知道我在做什么以及如何继续。
我还应该做一个世界Map,根据得分组,按颜色对所有国家进行分组(见前面的直方图任务)
我甚至不认为有可能编写一个世界Map,认为只有简单的图表和图表是可能的,所以我真的不知道如何完成这一点。
我意识到这个问题可能因为没有显示以前的工作而违反了一些规则,但正如前面提到的,我只学习了R大约一周,谷歌还没有给我我想要的答案。
任何帮助或指导将不胜感激!
谢谢
如前所述,没有以前的工作,以显示由于缺乏经验的R-编码

rggaifut

rggaifut1#

首先,我下载了Kaggle的数据,并使用了2019年的数据。您可能需要更改happyness_scores的分组方式。

#set working directory
setwd("C:/Users/maarvd/Desktop/stack")

#load libraries
library(data.table) #manipulation of tabular data
library(rnaturalearth) #to load data of country borders
library(rnaturalearthdata) #to load data of country borders
library(sf) #manipulation of spatial data
library(ggplot2) #for visualization

#read data (used 2019 data to give an exapmle)
dt <- fread("2019.csv")

#group countries based on happyness score
dt[Score >= 0 & Score < 2.5, happyness_score := "Unhappy"]
dt[Score >= 2.5 & Score < 5, happyness_score := "Happy"]
dt[Score >= 5, happyness_score := "Superhappy"]

#set order of factor levels
dt[, happyness_score := factor(happyness_score, levels = c("Unhappy", "Happy", "Superhappy"))]

#load shape of countries
countries <- ne_countries(scale = 50, type = "countries", returnclass = "sf")

#set to data.table
countries <- as.data.table(countries)

#select relevant columns
countries <- countries[, c("name_long", "geometry")]

#check which countries or regions do not match with name_long in countries
dt[!`Country or region` %in% countries$name_long]$`Country or region`

#adjust accordingly
dt[`Country or region` == "Trinidad & Tobago", `Country or region` := "Trinidad and Tobago"]
dt[`Country or region` == "South Korea", `Country or region` := "Republic of Korea"]
dt[`Country or region` == "Russia", `Country or region` := "Russian Federation"]
dt[`Country or region` == "North Macedonia", `Country or region` := "Macedonia"]
dt[`Country or region` == "Ivory Coast", `Country or region` := "Côte d'Ivoire"]
dt[`Country or region` == "Congo (Brazzaville)", `Country or region` := "Republic of Congo"]
dt[`Country or region` == "Congo (Kinshasa)", `Country or region` := "Democratic Republic of the Congo"]
dt[`Country or region` == "Laos", `Country or region` := "Lao PDR"]
dt[`Country or region` == "Palestinian Territories", `Country or region` := "Palestine"]
dt[`Country or region` == "Gambia", `Country or region` := "The Gambia"]

#check if all countries/regions match the name in countries shape
dt$`Country or region` %in% countries$name_long

#merge data with shapefile
dt.merged <- merge(dt, countries, by.x = "Country or region", by.y = "name_long")

#set to spatial features
dt.merged <- st_as_sf(dt.merged)

#visualize
ggplot(dt.merged) +
  geom_sf(aes(fill = happyness_score)) +
  theme_bw()

相关问题