I want the legend of a binned variable discrete_var in the bottom of my figure. I want the bins on one row. The problem is that doing this results in different sizes of bins and the labels too close to each other. How can I set the size of bins, or rotate labels to be place under and vertical to the legend bins (which might do the trick)?
shp |>
ggplot() +
geom_sf(aes(fill = discrete_var)) +
scale_fill_brewer(palette = 'Purples', direction = -1, na.value="grey", drop=FALSE) +
coord_sf(expand = FALSE) +
theme(legend.position = "bottom") +
guides(fill=guide_legend(nrow=1,byrow=TRUE, label.position = "bottom"))
This is what I want, but in the bottom of the figure:



Making the legend key width wider would be one approach with
legend.key.width = unit(2, "cm"):Or to put the text at an angle use
legend.text = element_text(angle = 45, vjust = 0.5):Finally, to make vertical, substitute your final line with
theme(legend.position = "bottom", legend.direction = "vertical"):