The error is occurring because the column ID_2
does not exist in the object BRmap
, as the error message is stating.
When reading the file downloaded from the page indicated, we can verify that none of its columns is named ID_2
, but there’s the column GID_2
(I don’t know if it’s the one you really want to wear).
BRmap <- readRDS("gadm36_BRA_2_sp.rds")
names(BRmap)
[1] "GID_0" "NAME_0" "GID_1" "NAME_1" "NL_NAME_1" "GID_2"
[7] "NAME_2" "VARNAME_2" "NL_NAME_2" "TYPE_2" "ENGTYPE_2" "CC_2"
[13] "HASC_2"
Something that helps a lot is debugging the codes built with the pipe Operator (%>%
) is to run the code that goes to a pipe, and then to the next one and so on. This helps to identify where the error is. In this case the error is already in the first line.
In addition, I recommend removing the line with the mutate(...)
, because Ids are not numerical, and by converting them to integers you will have only NA
column-wide.
head(BRmap$GID_2)
[1] "BRA.1.1_1" "BRA.1.2_1" "BRA.1.3_1" "BRA.1.4_1" "BRA.1.5_1" "BRA.1.6_1"
Finally, we have:
library(dplyr)
PaísX <- ggplot2::fortify(BRmap, region = "GID_2") %>%
full_join(BRmap@data, by =c("id" = "GID_2")) %>%
select(c(id, long, lat, order, hole, piece, group, NAME_2))
Welcome to Stackoverflow! Unfortunately, this question cannot be reproduced by anyone trying to answer it. Please, take a look at this link and see how to ask a reproducible question in R. So, people who wish to help you will be able to do this in the best possible way.
– Marcus Nunes