So lets say I have this example code
require(sf)
require(ggplot2)
tmp1 <- tempfile()
tmp2 <- tempfile()
download.file("http://api.thenmap.net/v2/se-4/geo/2020-06-03", destfile = tmp1)
download.file("http://api.thenmap.net/v2/se-7/geo/2020-06-03", destfile = tmp2)
county <- read_sf(tmp1)
county$id <- as.numeric(county$id)
county$id[5] <- NA
ggplot(county)
geom_sf(aes(fill = id))
scale_fill_viridis_c()
theme_void()
I am just using the id of the counties in Sweden as "data" (for a continuous scale) in this example and I have a NA value as well which I chose by random.
However I want for example the counties with id 10 and 21 in the sf object to be set manually the color of red due to the fact that they are outliers in my data and I want to visualize that. But I don't want these two counties to change the scale of the other countires i.e. I want the continuous scale to be between 1-20 (like they are now), but in my data their values are let's say 100 so I don't want these outliers to influence the scale of the non-outlier data and keep the scale as it is.
I tried to extract the objects longitude and latitude values into a data frame and group the two counties by 1 and 2. Then plot it but it does not work since it will viewed as discrete data (I understand why) i.e. adding this to the ggplot code
geom_polygon(data=large, aes(x=lng, y=lat, fill= "red", group=id), colour="black")
How can I solve this problem?
CodePudding user response:
One quick option would be to add a second geom_sf
layer for which you use only the data for the id
s 10 and 21 and for which you set fill="red"
:
library(sf)
library(ggplot2)
tmp1 <- tempfile()
tmp2 <- tempfile()
download.file("http://api.thenmap.net/v2/se-4/geo/2020-06-03", destfile = tmp1)
download.file("http://api.thenmap.net/v2/se-7/geo/2020-06-03", destfile = tmp2)
county <- read_sf(tmp1)
county$id <- as.numeric(county$id)
ggplot(county)
geom_sf(aes(fill = id))
geom_sf(data = subset(county, id %in% c(10, 21)), fill = "red")
scale_fill_viridis_c()
theme_void()
Or using the same approach you could add a red. colored outline around your outlier regions:
ggplot(county)
geom_sf(aes(fill = id))
geom_sf(data = subset(county, id %in% c(10, 21)), color = "red", linewidth = 1, fill = NA)
scale_fill_viridis_c()
theme_void()