0

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] Brazil -- Map

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))
Z.Lin
  • 28,055
  • 6
  • 54
  • 94
Luis
  • 1,388
  • 10
  • 30

1 Answers1

1

you'd better invest in sf package for spatial data. ggplot2 knows how to handle those data and you can manipulate them like data.frame. Following code shoul'd work as long as I cannot get your data, but it worked on similar data on my computer. Hope this helps

library(sf)
library(ggplot2)

map <- read_sf("mapa/UFEBRASIL.shp")
ideal_map <- merge(map, dados,by.x = "NM_ESTADO", by.y = "NM_ESTADO")

ggplot(ideal_map) +
  geom_sf() +
  geom_sf_label(aes(label=paste(NM_ESTADO,Valor,sep="\n"))

# with self avoiding labels using ggrepel package
library(ggrepel)

ggplot(ideal_map) +
  geom_sf() +
  geom_label_repel(
    aes(label = label=paste(NM_ESTADO,Valor,sep="\n"), geometry=geometry),
    stat = "sf_coordinates"
  )
Billy34
  • 1,777
  • 11
  • 11
  • Thanks much, @billy34 Amazing solution! I'm wondering if there's any way to prevent from overlapping when using geom_sf_label ? – Luis Apr 10 '19 at 15:18
  • Using informations from https://github.com/slowkow/ggrepel/issues/111 I added to my answer an example of self_avoiding labels. If you think that my answer was usefull please accept it as answer – Billy34 Apr 10 '19 at 15:30
  • Thanks much! Everything is fine!!. it's amazing how we can learn from stackoverflow! – Luis Apr 10 '19 at 16:13