r-tmap / tmap

R package for thematic maps
https://r-tmap.github.io/tmap
GNU General Public License v3.0
862 stars 121 forks source link

Setting position with numbers does not work #799

Open Nowosad opened 1 year ago

Nowosad commented 1 year ago
library(spData)
library(tmap)

# works
tm_shape(nz) +
  tm_polygons("Median_income") +
  tm_layout(legend.position = c("left", "bottom"))


# fails
tm_shape(nz) +
  tm_polygons("Median_income") +
  tm_layout(legend.position = c(0.02, 0.02))
#> Error in l$position$type: $ operator is invalid for atomic vectors
mad-dos-equis commented 4 months ago

I experienced the same issue when trying to set the legend position directly with numeric values in vector c(), which contradicts the documentation. However, if you are looking for a practical workaround in the meantime, I recommend using the tm_pos_in() function and its pos.h and pos.v options within the legend.position() function. From your code above:

tm_shape(nz) + tm_polygons("Median_income") + tm_layout(legend.position = tm_pos_in(pos.h = 0.02, pos.v = 0.02))