Reputation: 155
I am importing shapefile into R and try to plot it with labels. Unfortunately some labels overlap. That's why I have to use parameter "auto.placement = T" for the "tm_text" function. But this parameter places some labels outside plotting region (partially). Position of labels on every plot is random. Sometimes labels are withing plotting region, but most of the times are not (cutted).
As you can see on screenshot "Palangos m." is cutted down to "angos m." and "Klaipedos m." is cutted down to "aipedos m.".
Screenshot: map
tm_shape(area_r1) +
tm_fill("winner", title = "Winner", style = "cat",
palette = c("#FFFFB3", "#1F78B4", "#1A9850", "#E7298A") ) +
tm_legend(text.size = 0.75) +
tm_layout("", legend.position = c("left", "bottom")) +
tm_borders("grey60") +
tm_layout(frame = F) +
tm_text("savivald", size = .65, col = "black", auto.placement = T)
What can I do in order to fit this labels into plotting region?
Upvotes: 2
Views: 1700
Reputation: 21
@Jinda Lacko has a creative solution using bbox
, but for some reason it did not work for me. An easy solution is to use main.title
in tm_layout
, for example tm_layout(main.title = "Big Fat Title")
. See this question for more details.
Upvotes: 0
Reputation: 8699
Controlling the randomness of auto.placement = T
is difficult (though setting seed may help).
What you can do is adjusting the bounding box of your tmap
object oh so slightly, so that there is more room on the left for the two or so missing letters.
Increasing the bbox by a half is probably an exaggeration, but you can tune it as required.
bbox_new <- st_bbox(area_r1) # current bounding box
xrange <- bbox_new$xmax - bbox_new$xmin # range of x values
yrange <- bbox_new$ymax - bbox_new$ymin # range of y values
bbox_new[1] <- bbox_new[1] - (0.5 * xrange) # xmin - left
# bbox_new[3] <- bbox_new[3] + (0.5 * xrange) # xmax - right
# bbox_new[2] <- bbox_new[2] - (0.5 * yrange) # ymin - bottom
# bbox_new[4] <- bbox_new[4] + (0.5 * yrange) # ymax - top
bbox_new <- bbox_new %>% # take the bounding box ...
st_as_sfc() # ... and make it a sf polygon
tm_shape(area_r1, bbox = bbox_new) +
tm_fill("winner", title = "Winner", style = "cat",
palette = c("#FFFFB3", "#1F78B4", "#1A9850", "#E7298A") ) +
tm_legend(text.size = 0.75) +
tm_layout("", legend.position = c("left", "bottom")) +
tm_borders("grey60") +
tm_layout(frame = F) +
tm_text("savivald", size = .65, col = "black", auto.placement = T)
I wrote a blog post summarizing the technique a while back. https://www.jla-data.net/eng/adjusting-bounding-box-of-a-tmap-map/
Your example is not exactly reproducible, but this image (making more space for the "big fat, title" on North Carolina map) should give you idea.
Upvotes: 4