I need to generate different maps using tmap
. To create a continous colorbar I use
style = "cont"
together with defined breaks (breaks = seq(0, 100, 25))
).
This works well if my data shows some variation (see map 1 below). However, I also have data for which there is no variation across the individual polygons (see map 2). Nevertheless, to allow for direct comparison I want to have the colorbar plotted for map 1 (full range between 0 and 100) also for map 2.
So far I found no solutions to fix that problem. Any ideas?
library(tidyverse)
library(tmap)
library(sf)
library(urbnmapr)
makeMap <- function(data){
tm_shape(data) +
tm_fill("par",
palette = "RdYlGn",
style = "cont",
breaks = seq(0, 100, 25)) +
tm_layout(legend.text.size = 1.5,
legend.bg.color = "white",
legend.bg.alpha = .8) +
tm_borders(lwd = 1, col = "black")
}
# map 1
states_sf1 <- get_urbn_map("states", sf = TRUE) %>%
as.tibble() %>%
mutate(par = runif(51, 0 ,100)) %>%
st_as_sf()
makeMap(states_sf1)
# map 2
states_sf2 <- get_urbn_map("states", sf = TRUE) %>%
as.tibble() %>%
mutate(par = 10) %>%
st_as_sf()
makeMap(states_sf2)
Map 1
Map 2