Search code examples
rchoroplethchoroplethr

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


I am self-teaching R to me and at the moment I am having a look on Choropleths in R. For this I am using the packages 'choroplethr' and 'choroplethrAdmin1'.

After plotting all the examples from the instruction of these packages I wanted to plot a map of Germany's administrative 1 levels with random values. So I tried to to use the admin_1choropleth command. The help functions says that the command needs a dataset with two specific columns:

  • one called 'regions' which includes regions with the same spelling as they are in the 'admin1.regions'-dataset provided by the package
  • a second which is called 'value' to match the including values to the regions

For example this code produces a map of Japans Population per km² using the dataset:'df_japan_census' which contains a column called 'value' and one called 'region'

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

So for plotting an example map of Germany I just used the 'admin1.regions'-dataset (because it includes the regions needed) and added a column 'value' to it. Then I tried to plot it. This is how the code is looking:

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

But now I am getting this Error:

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

This is my first questions on stackoverflow and I am neither a natural English speaker or a programmer, so I hope you can understand my problem.

If you are having any questions do not hesitate to ask me.

Best, Marcel

P.S.: To make it easier for you, this should be all you need to reproduce the Error.

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)

Solution

  • The reason you get this error is due to duplicates in column region of admin1.regions. If you use unique(admin1.regions$region) you will get a vector of length 4358, while the orignal dataset is 4399 (so 41 duplicates).

    How to tackle this problem? You only need the entries containing "germany", so remove all the non-"gemany" entries.

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

    Now your code should work.

    PS. cool name