I have an sf-object that I would like to plot. It is a map over counties in Sweden (21 counties) and for each county, I have a value. This value however can be either a positive or negative value, or even an NA. I would like to plot the map over Sweden and color all counties that have NA as white, and then color all other counties with a gradient (depending on value) for all the continuous values. But when I try I only get this error message: Error: Continuous value supplied to discrete scale
This is not my real data but I am not sure how to demonstrate sf-object. So here is just a dataframe with name of county and values (so no coordinates or anything):
data <- data.frame(Diff = c("NA", "NA", "5", "6.89", "9", "-4", "3.56"),
County = c("Halland", "Gotland", "Skane", "Jonkoping", "Gotaland",
"Blekinge", "Dalarna"))
ggplot(data) +
geom_sf(aes(fill = Diff), color = "black") +
scale_fill_manual(values = c("blue", "yellow", viridis::viridis(4))) +
coord_sf(datum = NA)
So I would like to plot the whole map and if a county have an NA, the county should be white. And then I would like to have different color gradient for the values (ranged from negative to positive values) for each county.
Have enayone experienced the same problem?
The following code works for me
library(sf)
library(tidyverse)
#Downloading data from DIVA GIS website
get_sweden_map <- function(cong=113) {
tmp_file <- tempfile()
tmp_dir <- tempdir()
zp <- sprintf("http://biogeo.ucdavis.edu/data/diva/adm/SWE_adm.zip",cong)
download.file(zp, tmp_file)
unzip(zipfile = tmp_file, exdir = tmp_dir)
fpath <- paste(tmp_dir)
st_read(fpath, layer = "SWE_adm1")
}
swe <- get_sweden_map(114)
#Plot the Counties
plot(swe[5])
data <- tibble(Diff = c("NA", "NA", "5", "6.89", "9", "-4", "3.56"),
County = c("Halland", "Gotland", "Skane", "Jonkoping", "Gotaland",
"Blekinge", "Dalarna"))
#Join the data to the swe shapefile
sweden <- swe %>% left_join(data, by=c("NAME_1"="County"))
#Plotting the data
ggplot(sweden) +
geom_sf(aes(fill = Diff), color = "black") +
scale_fill_manual(values = c("blue", "yellow", viridis::viridis(4))) +
coord_sf(datum = NA)