Error: anyDuplicated(self$user.df$region) == 0 is not TRUE

Error: anyDuplicated(self$user.df$region) == 0 is not TRUE

我正在自学 R,目前我正在研究 R 中的 Choropleths。为此,我正在使用包 'choroplethr' 和 'choroplethrAdmin1'.

根据这些软件包的说明绘制所有示例后,我想绘制一张带有随机值的德国行政级别 1 的地图。所以我尝试使用 admin_1choropleth 命令。帮助功能说该命令需要一个包含两个特定列的数据集:

例如,此代码使用以下数据集生成日本每平方公里人口的地图:'df_japan_census',其中包含一个名为 'value' 的列和一个名为 'region'

的列
admin1_choropleth("japan",
              df_japan_census,
              "Japan Population",
              "per km²")

因此,为了绘制德国示例地图,我只使用了 'admin1.regions' 数据集(因为它包含所需的区域)并添加了一列 'value' 给它。然后我试图绘制它。这是代码的样子:

admin1.regions$value<-3:4401 #the numbers are just placeholders
admin1_choropleth("germany",admin1.regions)

但现在我收到这个错误:

Error: anyDuplicated(self$user.df$region) == 0 is not TRUE

这是我在 Whosebug 上的第一个问题,我既不是天生的英语母语者也不是程序员,所以我希望你能理解我的问题。

如果您有任何问题,请随时问我。

最好的, 马塞尔

P.S.: 为了让您更轻松,这应该是您重现错误所需的全部内容。

install.packages("choroplethr")
library(choroplethr)
install.packages("choroplethrAdmin1")
install.packages("ggplot2")
library(choroplethrAdmin1)
library(ggplot2)

admin1.regions$value<-3:4401
admin1_choropleth("germany",admin1.regions)

您收到此错误的原因是 admin1.regionsregion 列重复。如果你使用 unique(admin1.regions$region) 你会得到一个长度为 4358 的向量,而原始数据集是 4399(所以有 41 个重复项)。

如何解决这个问题?您只需要包含 "germany" 的条目,因此删除所有非 "gemany" 条目。

admin1.regions$value<-3:4401 #the numbers are just placeholders
admin1.regions1 <- admin1.regions[which(admin1.regions$country == "germany"),]
admin1_choropleth("germany",admin1.regions)

现在您的代码应该可以工作了。

PS。好听的名字