I was wondering if you can help me out with a (probably silly) problem with my code that is driving me nuts. I'm using R and my problems are with the package sf
+ ggplot2
. Is there anyone out there that can help me?. I found a similar question here, but the solution is not exactly what I need.
I'm trying to plot a numeric variable on a map using the sf
package. The variable is numeric and I want the legend to be on a continuous scale. I managed to do that, the problem started when I used the "guides" option to position the title of the legend on top. Here is a reproducible example:
library(sf)
demo(nc, ask = F, echo = F)
ggplot() +
geom_sf(data = nc, aes(fill = BIR74)) +
scale_y_continuous(breaks = 34:36) +
theme(legend.position="bottom",
legend.direction = "horizontal")
This code produces this map:. You can see that the scale of the Bir74 is continuous. If I use
guides(fill = guide_legend(title.position = βtopβ)
, it will work for placing the title of the legend on top, but the legend label is on a discrete scale now. This is an example
ggplot() +
geom_sf(data = nc, aes(fill = BIR74)) +
scale_y_continuous(breaks = 34:36) +
theme(legend.position="bottom",
legend.direction = "horizontal") +
guides(fill = guide_legend(title.position = "top"))
and this is the map resulting:
What I really want is the legend placed at the bottom with the title on top as in map 2, but with the legend on the scale of map 1.
Any help on how to solve this would be massively appreciated!
Cheers!