I have uploaded of a shapefile here.
#First, read it in
library(rgdal)
pols <- readOGR(dsn="stack", layer="shapefile")
plot.df <- broom::tidy(pols, by="nr") %>% mutate(id = as.numeric(id))
#some values I'd like to plot
plot.df$value[plot.df$id==0] <- 4
plot.df$value[plot.df$id==1] <- 3
plot.df$value[plot.df$id==2] <- 3
library(ggplot2)
ggplot(plot.df, aes(x=long, y=lat))+
geom_polygon(aes(fill=factor(value), group=group))+
coord_fixed()+
scale_fill_manual(values = c("red", "green", "blue", "orange"),
breaks = c("1", "2", "3", "4"))
This code should produce the following plot:
The thing is, that I have another polygon which should appear (colored in green).
I've come up with a workaround:
ggplot(plot.df, aes(x=long, y=lat))+
geom_polygon(aes(fill=factor(value), group=group))+
geom_polygon(data=subset(plot.df, id==0), aes(fill=factor(value), group=group))+
coord_fixed()+
scale_fill_manual(values = c("red", "green", "blue", "orange"),
breaks = c("1", "2", "3", "4"))
However, I'm sure there is a more elegant solution. Using id
as grouping variable as proposed here leads to distorted polygons.
Does anyone have any other ideas?