Open ar-onos opened 9 months ago
I think quarto + grid is a better approach for layouts like this!
I think quarto + grid is a better approach for layouts like this!
Thanks @tim-salabim. The panel already comes in with tmap. is there a way to use viewport from grid to insert an image with inset_vp ? This could be an option to position the this since the logo is in png format before exporting the map.
I still need to implement tm_logo into v4, but for the time being, either the approach suggested by @tim-salabim or alternatively, a workaround based on this approach here: https://stackoverflow.com/a/69699524
Ignore the ggplot2 part in this code. What is important is the l
object, which is a grid::rasterGrob
.
The workaround is:
png("mymap.png", width = 800, height = 800)
tm_shape(NLD_muni) + tm_polygons("population")
grid::grid.draw(l)
dev.off()
When creating the rasterGrob, you specify the x, y, width and height. These determine the position of the logo on the device.
I still need to implement tm_logo into v4, but for the time being, either the approach suggested by @tim-salabim or alternatively, a workaround based on this approach here: https://stackoverflow.com/a/69699524
Ignore the ggplot2 part in this code. What is important is the
l
object, which is agrid::rasterGrob
.The workaround is:
png("mymap.png", width = 800, height = 800) tm_shape(NLD_muni) + tm_polygons("population") grid::grid.draw(l) dev.off()
When creating the rasterGrob, you specify the x, y, width and height. These determine the position of the logo on the device.
Thanks, @mtennekes and @tim-salabim . In ggplot2, I can just do:
TM + annotation_custom(l, xmin = 1, xmax = 1.5, ymin = 210, ymax = 245)
where TM is a ggplot2 object. For tmap V4, is it possible to add something like "tm_annotation" that will allow you to use the viewport to add a png or jpg object to a map while saving with tmap_save ? For example:
AP <- tm_png ("mylogo.png") +tm_annotation( )
# Get bounding box and aspect ratio of i (Country)
xy <- st_bbox(NLD_muni)
asp <- (xy$ymax - xy$ymin)/(xy$xmax - xy$xmin)
## Aspect
# Create viewport for inset map
w <- 0.15
h <- w/3
vp <- viewport(x = 0.95, y = 1.05, width = w, height=w/3, just=c("right", "top"))
##Save Main map and inset to file
tmap_save(NLD, filename= "NLDWhite_20231012.png", dpi = 750, insets_tm= AP, insets_vp= vp)
Another option I imagine is adding this to the tm_layout options:
tm_layout(attr.logo.position.outside=T, attr.logo.position=c("top","right"))
I do a lot of mapping in loops by attributes and having this option to add logos outside where possible will be great.
Imho, it really pays off to learn grid. That way you can have full control of your plotting and mix every and all grid based graphics (tmap, ggplot, lattice, native grid) into the layout of your choice. This may not help directly with your current endeavour, but it will help in the future . And now I'm shutting up and leave it to the experts here...
Imho, it really pays off to learn grid. That way you can have full control of your plotting and mix every and all grid based graphics (tmap, ggplot, lattice, native grid) into the layout of your choice. This may not help directly with your current endeavour, but it will help in the future . And now I'm shutting up and leave it to the experts here...
Thanks @tim-salabim for your recommendations.
I need to put my logo at the top right of a plot just outside the map window within the title panel box. Is there a way to position the logo alone without attr.position.outside = T, please? I want all other attributes within the plot window except the logo
Here is an example of what I mean: