I have a histogram in my legend that displays the distribution of my data. I would like to edit the labels of the axis to improve readability. Ideally, this would be changed to reflect the categories, so the values of 500 and 1500 can be removed.
library(tmap)
custom_colors <- c("#ffd500",'orange', "#e60000")
tm_map <- tm_shape(merged) +
tm_borders(lwd=NA) +
tm_polygons("aantal_observaties",
id = "pc4",
breaks= c(1,1001, 2001, 3000),
n=3,
palette = custom_colors,
style = "fixed",
title = "Observations",
alpha = 1,
na.color="purple",
colorNA='#ebebeb',
legend.hist=TRUE,
legend.show=FALSE) +
tm_layout(legend.outside=TRUE,legend.outside.position = "right",
frame=FALSE,
title = 'Observations',
title.size = 1)
tm_map + tm_add_legend(labels=c("1 to 1000", "1001 to 2000", "2001 to 3000", "0"),
col = c("#ffd500",'orange', "#e60000", "#ebebeb"),
border.lwd = NA,
)

Did some toying around with this and it seems like
legend.histmay not be supported intmapv4. Installingtmapand loading the library gave me the message:Anyway, after installing that version, I was unable to get a
legend.hist. On this other SO post on customizing the histogram axes in tmap Martijn Tennekes saysThat being the case, you can get something similar using
ggplot2andpatchwork, though specifying the layout is a little clunky.Since you didn't post data, I created some that I think is similar enough:
And for the plot:
Since you create the histogram separately, you have full control over it. Know this isn't ideal since it's not in
tmapbut thought it might be close to what you're after.More on using
patchwork(orgridExtra/cowplot) here