0

I'm trying to do a map using library tmap and this dataset.

Following this discussion, I'm using the following simple code:

library(tmap)
library(geobr)
library(sf)

rg = st_as_sf(polygons_regioes_adm_sp)

tm_shape(rg) + 
  tm_polygons("regiao_administrativa",
              legend.show = F) +
  tm_text("regiao_administrativa", size = 1/2)

That yields the following map with overlapping DUPLICATE text. I tried insert remove.overlap = TRUE inside tm_text but this doesn't worked better.

enter image description here

Furthermore i'm getting the following message:

old-style crs object detected; please recreate object with a recent sf::st_crs()

In this context, How I get a map without duplicate text ?

Phil
  • 7,287
  • 3
  • 36
  • 66
Alien
  • 116
  • 8
  • Hi @Marcos Junio, please provide a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) so that we can help you in the best way. Thanks – lovalery Nov 13 '21 at 15:06
  • Hi @lovalery , thanks for the feedback. I provided a link that gives access to the data I used, in addition I provided the simple code that I ran to get the figure. This is already a reproducible example. Anyone who downloads the data can easily run my example. – Alien Nov 13 '21 at 15:32
  • Hi @ Marcos Junio. O.K. sorry. Actually I'm not used to work with `.rda` files. Anyway, the main thing is that I finally managed to read your file. So, please find my answer to your problem below. If it is OK, please consider marking this answer as "accepted" and/or upvoted. If not, please tell me what is wrong. Cheers. – lovalery Nov 13 '21 at 18:33

1 Answers1

1

Your code to display the map with the tmap library is fine. The problem is that the sf object polygons_regioes_adm_sp has a mixture of POLYGON and MULTIPOLYGON. So, you just need to simplify the sf object by running the st_cast() function.

As for the projection problem, you just need to specify the EPSG code to assign a recent form of the CRS to the sf object (NB: as for the warning message - cf.below - do not worry, it is fine)

So, please find below the code to display correctly the names of the administrative regions.

library(tmap)
library(sf)
library(dplyr) # please, do not forget to load this library


rg <- polygons_regioes_adm_sp %>% 
  st_set_crs(4674) %>% 
  st_cast()
#> old-style crs object detected; please recreate object with a recent sf::st_crs()
#> Warning: st_crs<- : replacing crs does not reproject data; use st_transform for
#> that

tm_shape(rg) + 
  tm_polygons("regiao_administrativa",
              legend.show = F) +
  tm_text("regiao_administrativa", size = 1/2)

Created on 2021-11-13 by the reprex package (v2.0.1)

lovalery
  • 4,524
  • 3
  • 14
  • 28