Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding text labels to tmap plot

Tags:

r

tmap

I'm trying to add some text labels a tmap plot.

library(tmap)
library(raster)

jnk <- getData("GADM",country="IND",level=2)

map_file <- tm_shape(jnk) +
  tm_polygons() +
  tm_text("NAME_1", remove.overlap = TRUE)

My problem is I'm getting duplicate text when I plot (can't post image since I'm new). I think I might have to group by some sort of geometry and NAME_1 combination but I'm unsure where to go from here.

Any advice would be great!

like image 366
in123321 Avatar asked Dec 31 '22 20:12

in123321


1 Answers

I am not certain what is your problem (as you were unable to post your image) but consider this code:

library(tmap)
library(raster)


jnk <- getData("GADM",country="IND",level=1)

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

I have made some minor changes to your code:

  • downloaded level 1 detail instead of level 2 detail (districts were too numerous, states are OK)
  • removed legend from the tm_polygons() call
  • made the letters of tm_text() smaller (to fit the north-eastern states)

enter image description here

like image 70
Jindra Lacko Avatar answered Jan 08 '23 02:01

Jindra Lacko