I've started recently working with maps and now I feel that "I almost there". The Internet is an amazing tool when we have questions related to R, but it seems that scripts from 2014/2015 are way outdated to do what I want. I've been following this link , and this link with no success.
Let's say a have a "map" file that I can plot easily
library(tidyverse)
library(rgdal)
map <- readOGR("mapa", "UFEBRASIL", encoding = "utf-8")
ideal_map <- fortify(map)
ggplot() +
geom_path(data = ideal_map,
aes(x = long, y = lat, group = group),
colour = "black") )
[Just in case you want to download this readOGR file, https://www.dropbox.com/s/vnpwwxh471ttaop/map.zip?dl=0]
And I now want to insert in this map some extra information, such as the name of each state (NM_ESTADO) and a specific value (Valor) in the middle of the state. This info is gathered in another dataset with the same column names:
> dados %>% names
[1] "NR_REGIAO" "Sigla" **"NM_ESTADO"** "Valor"
> head(map@data,1)
ID CD_GEOCODU **NM_ESTADO** NM_REGIAO
0 1 11 RONDÔNIA NORTE
Now comes the main question:
When I merge both datasets and then I proceed with the fortify function, it seems the new info added just go away:
#merge
ideal_map2 <- merge(map, dados,by.x = "NM_ESTADO", by.y = "NM_ESTADO")
ideal_map2@data
ideal_map <- fortify(ideal_map2)
head(ideal_map)
long lat order hole piece id group
1 -63.32721 -7.976720 1 FALSE 1 0 0.1
2 -63.11838 -7.977107 2 FALSE 1 0 0.1
So, how should I go ?
Thank you! In case you need my dataset file to run the codes, please see below:
dados <- structure(list(NR_REGIAO = c("NORTE", "NORTE", "NORTE", "NORTE",
"NORTE", "NORTE", "NORTE", "NORDESTE", "NORDESTE", "NORDESTE",
"NORDESTE", "NORDESTE", "NORDESTE", "NORDESTE", "NORDESTE", "NORDESTE",
"CENTRO-OESTE", "CENTRO-OESTE", "CENTRO-OESTE", "CENTRO-OESTE",
"SUDESTE", "SUDESTE", "SUDESTE", "SUDESTE", "SUL", "SUL", "SUL"
), Sigla = c("AC", "AP", "AM", "PA", "RO", "RR", "TO", "AL",
"BA", "CE", "MA", "PB", "PE", "PI", "RN", "SE", "DF", "GO", "MT",
"MS", "ES", "MG", "RJ", "SP", "PR", "SC", "RS"), NM_ESTADO = c("ACRE",
"AMAPÁ", "AMAZONAS", "PARÁ", "RONDÔNIA", "RORAIMA", "TOCANTINS",
"ALAGOAS", "BAHIA", "CEARÁ", "MARANHÃO", "PARAÍBA", "PERNAMBUCO",
"PIAUÍ", "RIO GRANDE DO NORTE", "SERGIPE", "DISTRITO FEDERAL",
"GOIÁS", "MATO GROSSO", "MATO GROSSO DO SUL", "ESPIRITO SANTO",
"MINAS GERAIS", "RIO DE JANEIRO", "SÃO PAULO", "PARANÁ", "SANTA CATARINA",
"RIO GRANDE DO SUL"), Valor = c("16", "13", "5", "40", "60",
"10", "20", "19", "89", "62", "27", "20", "84", "26", "17", "107",
"143", "86", "78", "79", "70", "285", "109", "169", "181", "159",
"322")), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-27L))