3

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: enter image description here

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"))

enter image description here

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?

Thomas
  • 1,392
  • 3
  • 22
  • 38

1 Answers1

0

The polygons overlap, you can set alpha.

ggplot(plot.df, aes(x = long, y = lat)) +
  geom_polygon(aes(fill = factor(value), group = group), alpha = .4)+
  coord_fixed() +
  scale_fill_manual(values = c("red", "green", "blue", "orange"), breaks = c("1", "2", "3", "4")) +
  theme(legend.position = 'none')

enter image description here

myincas
  • 1,500
  • 10
  • 15