I am trying to present a map using ggplot on a factor-type vector going from 0 to 4 that I color coded. The vector is in the dataframe I named spat.dataframe
and the vector is qt
.
# plot
ggplot(spat.dataframe, aes(long,lat,group=group)) + # the data
geom_polygon(aes(fill=as.factor(qt))) + # make polygons
scale_fill_manual(values = c("0"="#F0F0F0","1"="green","2"="red","3"="blue","4"="purple"),
labels = c(paste0("white (",length(spat.dataframe[spat.dataframe$qt==0,]),")"),
paste0("green (",length(spat.dataframe[spat.dataframe$qt==1,]),")"),
paste0("red (",length(spat.dataframe[spat.dataframe$qt==2,]),")"),
paste0("blue (",length(spat.dataframe[spat.dataframe$qt==3,]),")"),
paste0("pink (",length(spat.dataframe[spat.dataframe$qt==4,]),")")),
drop=F,
name=NULL) +
theme(line = element_blank(), # remove the background, tickmarks, etc
axis.text = element_blank(),
axis.title = element_blank(),
panel.background = element_blank()) +
ggtitle(title) +
geom_path( colour = "#6b6b6b", size = .5 ) +
coord_equal()
My problem lies with the legend portion, because what I want to do is display all of the legend options even if there arent any listed in the vector. So the values with colors blue and purple as well. It may seem like a bit of a stretch to try but I want it on my figure so that I can also display the number of values possible and the number of polygons associated with that value. So, for the values 3 and 4 in my vector qt
, both are zero (i.e., blue (0)
and purple (0)
).
Any suggestions would be greatly appreciated. Thanks.