4

I am trying to create a map with several points (about 20), where each point will be represented by a number, and where a legend will have these numbers and names of my points. Is it possible to do it with the tmap package (or any other R package)?

This is what I have:


library(sf)                                                                        
#> Linking to GEOS 3.5.0, GDAL 2.1.2, proj.4 4.9.2
library(tmap)                                                                      
data(Europe)                                                                       

df_obj <- data.frame(id=c(1,2), name=c("First", "Second"), x=c(0, 25), y=c(45, 50))
sf_obj = st_as_sf(df_obj, coords = c("x", "y"), crs = 4326)                        

tm_shape(st_as_sf(Europe)) +                                                       
  tm_polygons() +                                                                    
  tm_text("iso_a3", size="AREA", root=4) +                                           
  tm_shape(sf_obj) +                                                                 
  tm_symbols() +                                                                     
  tm_text("id", col="name") +                                                        
  tm_format_Europe() +                                                               
  tm_style_bw()                                                                      

This is what I want (see the legend):

enter image description here

1
  • is base plot an option or you need it with ggplot2? Jan 29, 2018 at 22:44

3 Answers 3

3
+50

My two cents... Set labels and text in tm_add_legend() and set legend.col.show = F in tm_text:

tm_shape(st_as_sf(Europe)) +                                                       
  tm_polygons() +                                                                    
  tm_text("iso_a3", size="AREA", root=4) +                                           
  tm_shape(sf_obj) +                                                                 
  tm_symbols() +         
  tm_text("id", col="name",legend.col.show = F) + 
  tm_add_legend(title = 'name',type = 'text',labels = sf_obj$name, text = sf_obj$id) +
  tm_format_Europe() +                                                               
  tm_style_bw()  

enter image description here

2
  • I've been trying to use this code to do a similar thing, but am ending up with absurdly small text (the 1, 2, 3 .. etc) next to correctly sized labels. Any idea why this might be happening?
    – cschwab98
    Oct 21, 2021 at 16:24
  • @cschwab98 add size argument. I.e., tm_add_legend(title = 'name',type = 'text',labels = sf_obj$name, text = sf_obj$id, size = 4)
    – aldo_tapia
    Oct 21, 2021 at 20:33
2

here is a base plot solution for you:

library(sf)                                                                        
#> Linking to GEOS 3.5.0, GDAL 2.1.2, proj.4 4.9.2
library(magrittr)                                                                    
data(Europe)                                                                       

df_obj <- data.frame(id=c(1,2), name=c("First", "Second"), x=c(0, 25), y=c(45, 50))
sf_obj <- st_as_sf(df_obj, coords = c("x", "y"), crs = 4326)                        

euro <- st_as_sf(Europe) %>% 
  st_transform(4326) %>% 
  st_geometry

plot(euro, col='lightgrey', axes= T)
plot(sf_obj, add= T, pch= 19, cex=2.5, col="black",)
df_obj %$% 
  text(x,y,id,cex=1, col='white')

enter image description here

1

Just a wild guess but does it work if instead of

tm_text("id", col="name")

you use

tm_add_legend(type = "text", labels = "id")

I hope this works for you

source: https://cran.r-project.org/web/packages/tmap/tmap.pdf

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.