使用highcharter绘制地图图形的问题
Problem in plotting a map graph using highcharter
我在尝试使用 highcharter 包为 choroplet 编写 R 代码时遇到问题。我正在尝试在第 84-112 行复制以下 link 中的代码:https://www.kaggle.com/gloriousc/global-terrorism-in-1970-2016/code。
我遇到了 2 个错误:
当运行第95行时,报错说没有对象叫"countrycode_data"。我在互联网上查看 countrycode_data 是什么,我发现它是一个包含国家代码的数据集,与数据集中的国家名称相关联。 Countrycode_data,据我了解,它应该包含在我安装的 "countrycode" 包中,但我没有找到如何访问此数据集。为了克服这个问题,我从互联网上下载了这个数据集并设法继续编写代码。
当运行从第103行开始的choroplet代码时,遇到如下错误:"Error: %in%
(x = tail(joinBy, 1), table = names(df)) is not TRUE"。我实际上不知道这个错误可能意味着什么,所以我在这里寻求帮助。
我设法克服了第一个错误问题,尽管我不确定这是正确的方法。
我将把整个代码留在这里:
knitr::opts_chunk$set(echo=TRUE, error=FALSE)
library(dplyr) #manipulate table
library(ggplot2) #visualization
library(highcharter) #making map
library("viridisLite") #Default Color Maps
library(countrycode) #list of country code
library(treemap) #make a treemap chart
library(reshape2) #melt function
library(plotly) #pie chart
library(tm) #text mining
library(SnowballC) #stemming text
library(wordcloud) #make a text chart
library(RColorBrewer) #make a color pallette
library(DT) #make datatable
#input the data
terror <- read.csv("../input/globalterrorismdb_0617dist.csv")
恐怖事件地图
#count terrorism incidents per country as a dataframe
countries <- terror %>%
group_by(country_txt) %>%
summarise(Total = round(n()))
#Making a terrorism map
#Credit to umeshnarayanappa
names(countries) <- c("country.name", "total") #change the column name
countries$iso3 <- countrycode_data[match(countries$country.name, countrycode_data$country.name.en), "iso3c"] #add iso3 column from country_code
data(worldgeojson, package = "highcharter")
dshmstops <- data.frame(q = c(0, exp(1:5)/exp(5)),
c = substring(viridis(5 + 1, option = "D"), 0, 7)) %>% #from viridisLite, make a color
list_parse2() #from highchart package, parse df to list
highchart() %>% #from highchart package
hc_add_series_map(worldgeojson, countries, value = "total", joinBy = "iso3") %>%
hc_colorAxis(stops = dshmstops) %>%
hc_legend(enabled = TRUE) %>%
hc_add_theme(hc_theme_db()) %>%
hc_mapNavigation(enabled = TRUE) %>%
hc_title(text = "Global Terrorism in 1970-2016", style = list(fontSize = "25px")) %>%
hc_add_theme(hc_theme_google()) %>%
hc_credits(enabled = TRUE, text = "Sources: National Consortium for the Study of Terrorism and Responses to Terrorism (START)", style = list(fontSize = "10px"))
我想说明的是,即使我按 ctrl+c ctrl+v 这些行,它们也不适合我。
感谢您阅读所有内容,也希望能得到您的帮助。
我试图复制这个例子。我希望以下内容足以让您自己工作并复制示例。 countrycode_data
似乎在 psData 包中。这个包需要 rJava 包,它现在不在我的机器上。当您在寻找解决方法时,我找到了自己的方法;我抓取了包括 iso3 在内的国家/地区数据。 (您也可以使用 ISOcodes 包。)您需要检查两个数据集中的国家/地区名称是否相同,这是一个常见的挑战。您通常会看到一些不匹配的情况。我没有时间全部更正,但我向您展示了如何修改 recode()
中的一些国家/地区名称。底线是您要将 iso3 添加到 countries
。因此,您需要确保尽可能使用相同的国家/地区名称。 (很明显,有些国家已经不存在了。你真的不能对它们做任何事情。)作者在他的代码中使用了 match()
,但我宁愿使用 left_join()
来做同样的事情。在此之后,我认为您已准备好遵循其余代码。注意 hc_add_series_map()
也在做一个连接过程。 worldgeojson
有一个 属性 叫做 iso3
。 countries
必须有一个名为 iso3
的列。否则,您将再次收到相同的错误消息。
library(tidyverse)
library(data.table)
library(rvest)
library(highcharter)
library(viridisLite)
# I used fread(). This is much faster.
terror <- fread("globalterrorismdb_0919dist.csv")
# I wrote my own code which does the same job.
count(terror, country_txt) %>%
setNames(nm = c("country.name", "total")) -> countries
# Get iso3 data
map_dfc(.x = c("official", "shortname", "iso3"),
.f = function(x) {read_html("http://www.fao.org/countryprofiles/iso3list/en/") %>%
html_nodes(paste("td.", x, sep = "")) %>%
html_text() %>%
gsub(pattern = "\n(\s+)?", replacement = "")}) %>%
setNames(nm = c("official", "shortname", "iso3")) -> iso3
# Revise some country names.
mutate(iso3, shortname = trimws(sub(x = shortname, pattern = "\(.*\)",
replacement = "")),
shortname = recode(.x = shortname,
`Bosnia and Herzegovina` = "Bosnia-Herzegovina",
`Brunei Darussalam` = "Brunei",
Czechia = "Czech Republic",
Congo = "Republic of the Congo",
`Côte d'Ivoire` = "Ivory Coast",
`Russian Federation` = "Russia",
`United Kingdom of Great Britain and Northern Ireland` = "United Kingdom",
`United States of America`= "United States"
)) -> iso3
# Join the two data sets
left_join(countries, iso3, by = c("country.name" = "shortname")) -> countries
data(worldgeojson, package = "highcharter")
dshmstops <- data.frame(q = c(0, exp(1:5)/exp(5)),
c = substring(viridis(5 + 1, option = "D"), 0, 7)) %>% #from viridisLite, make a color
list_parse2()
highchart() %>% #from highchart package
hc_add_series_map(worldgeojson, df = countries,
value = "total", joinBy = "iso3") %>%
hc_colorAxis(stops = dshmstops) %>%
hc_legend(enabled = TRUE) %>%
hc_add_theme(hc_theme_db()) %>%
hc_mapNavigation(enabled = TRUE) %>%
hc_title(text = "Global Terrorism in 1970-2016", style = list(fontSize = "25px")) %>%
hc_add_theme(hc_theme_google()) %>%
hc_credits(enabled = TRUE,
text = "Sources: National Consortium for the Study of Terrorism and Responses to Terrorism (START)",
style = list(fontSize = "10px"))
我在尝试使用 highcharter 包为 choroplet 编写 R 代码时遇到问题。我正在尝试在第 84-112 行复制以下 link 中的代码:https://www.kaggle.com/gloriousc/global-terrorism-in-1970-2016/code。 我遇到了 2 个错误:
当运行第95行时,报错说没有对象叫"countrycode_data"。我在互联网上查看 countrycode_data 是什么,我发现它是一个包含国家代码的数据集,与数据集中的国家名称相关联。 Countrycode_data,据我了解,它应该包含在我安装的 "countrycode" 包中,但我没有找到如何访问此数据集。为了克服这个问题,我从互联网上下载了这个数据集并设法继续编写代码。
当运行从第103行开始的choroplet代码时,遇到如下错误:"Error:
%in%
(x = tail(joinBy, 1), table = names(df)) is not TRUE"。我实际上不知道这个错误可能意味着什么,所以我在这里寻求帮助。
我设法克服了第一个错误问题,尽管我不确定这是正确的方法。
我将把整个代码留在这里:
knitr::opts_chunk$set(echo=TRUE, error=FALSE)
library(dplyr) #manipulate table
library(ggplot2) #visualization
library(highcharter) #making map
library("viridisLite") #Default Color Maps
library(countrycode) #list of country code
library(treemap) #make a treemap chart
library(reshape2) #melt function
library(plotly) #pie chart
library(tm) #text mining
library(SnowballC) #stemming text
library(wordcloud) #make a text chart
library(RColorBrewer) #make a color pallette
library(DT) #make datatable
#input the data
terror <- read.csv("../input/globalterrorismdb_0617dist.csv")
恐怖事件地图
#count terrorism incidents per country as a dataframe
countries <- terror %>%
group_by(country_txt) %>%
summarise(Total = round(n()))
#Making a terrorism map
#Credit to umeshnarayanappa
names(countries) <- c("country.name", "total") #change the column name
countries$iso3 <- countrycode_data[match(countries$country.name, countrycode_data$country.name.en), "iso3c"] #add iso3 column from country_code
data(worldgeojson, package = "highcharter")
dshmstops <- data.frame(q = c(0, exp(1:5)/exp(5)),
c = substring(viridis(5 + 1, option = "D"), 0, 7)) %>% #from viridisLite, make a color
list_parse2() #from highchart package, parse df to list
highchart() %>% #from highchart package
hc_add_series_map(worldgeojson, countries, value = "total", joinBy = "iso3") %>%
hc_colorAxis(stops = dshmstops) %>%
hc_legend(enabled = TRUE) %>%
hc_add_theme(hc_theme_db()) %>%
hc_mapNavigation(enabled = TRUE) %>%
hc_title(text = "Global Terrorism in 1970-2016", style = list(fontSize = "25px")) %>%
hc_add_theme(hc_theme_google()) %>%
hc_credits(enabled = TRUE, text = "Sources: National Consortium for the Study of Terrorism and Responses to Terrorism (START)", style = list(fontSize = "10px"))
我想说明的是,即使我按 ctrl+c ctrl+v 这些行,它们也不适合我。
感谢您阅读所有内容,也希望能得到您的帮助。
我试图复制这个例子。我希望以下内容足以让您自己工作并复制示例。 countrycode_data
似乎在 psData 包中。这个包需要 rJava 包,它现在不在我的机器上。当您在寻找解决方法时,我找到了自己的方法;我抓取了包括 iso3 在内的国家/地区数据。 (您也可以使用 ISOcodes 包。)您需要检查两个数据集中的国家/地区名称是否相同,这是一个常见的挑战。您通常会看到一些不匹配的情况。我没有时间全部更正,但我向您展示了如何修改 recode()
中的一些国家/地区名称。底线是您要将 iso3 添加到 countries
。因此,您需要确保尽可能使用相同的国家/地区名称。 (很明显,有些国家已经不存在了。你真的不能对它们做任何事情。)作者在他的代码中使用了 match()
,但我宁愿使用 left_join()
来做同样的事情。在此之后,我认为您已准备好遵循其余代码。注意 hc_add_series_map()
也在做一个连接过程。 worldgeojson
有一个 属性 叫做 iso3
。 countries
必须有一个名为 iso3
的列。否则,您将再次收到相同的错误消息。
library(tidyverse)
library(data.table)
library(rvest)
library(highcharter)
library(viridisLite)
# I used fread(). This is much faster.
terror <- fread("globalterrorismdb_0919dist.csv")
# I wrote my own code which does the same job.
count(terror, country_txt) %>%
setNames(nm = c("country.name", "total")) -> countries
# Get iso3 data
map_dfc(.x = c("official", "shortname", "iso3"),
.f = function(x) {read_html("http://www.fao.org/countryprofiles/iso3list/en/") %>%
html_nodes(paste("td.", x, sep = "")) %>%
html_text() %>%
gsub(pattern = "\n(\s+)?", replacement = "")}) %>%
setNames(nm = c("official", "shortname", "iso3")) -> iso3
# Revise some country names.
mutate(iso3, shortname = trimws(sub(x = shortname, pattern = "\(.*\)",
replacement = "")),
shortname = recode(.x = shortname,
`Bosnia and Herzegovina` = "Bosnia-Herzegovina",
`Brunei Darussalam` = "Brunei",
Czechia = "Czech Republic",
Congo = "Republic of the Congo",
`Côte d'Ivoire` = "Ivory Coast",
`Russian Federation` = "Russia",
`United Kingdom of Great Britain and Northern Ireland` = "United Kingdom",
`United States of America`= "United States"
)) -> iso3
# Join the two data sets
left_join(countries, iso3, by = c("country.name" = "shortname")) -> countries
data(worldgeojson, package = "highcharter")
dshmstops <- data.frame(q = c(0, exp(1:5)/exp(5)),
c = substring(viridis(5 + 1, option = "D"), 0, 7)) %>% #from viridisLite, make a color
list_parse2()
highchart() %>% #from highchart package
hc_add_series_map(worldgeojson, df = countries,
value = "total", joinBy = "iso3") %>%
hc_colorAxis(stops = dshmstops) %>%
hc_legend(enabled = TRUE) %>%
hc_add_theme(hc_theme_db()) %>%
hc_mapNavigation(enabled = TRUE) %>%
hc_title(text = "Global Terrorism in 1970-2016", style = list(fontSize = "25px")) %>%
hc_add_theme(hc_theme_google()) %>%
hc_credits(enabled = TRUE,
text = "Sources: National Consortium for the Study of Terrorism and Responses to Terrorism (START)",
style = list(fontSize = "10px"))