I need help with scale_fill_manual using a shapefile in ggplot2. I have tried many thing and I am finally posting so hopefully someone will be able to give me a hint. I am basically ploting a shapefile and use scale_fill_manual to vizualise it with custom colors, I then overlay some point on it but when I tried to include my new points in the legend, my original colors are there but the values get all messed up. The upper part ploting the shapefile works fine, but the bottom part when overlaying the new points is where I need help. I have some comments inline. See below: The path to download the shapefile is: https://login.filesanywhere.com/fs/v.aspx?v=8c6a66875b6574bbaa68
library(tidyverse)
library(rgdal)
library(maptools)
library(plyr)
library(sp)
library(geosphere)
library(data.table)
library(rgeos)
wolves.map <- readOGR(dsn=".", layer="PNW_wolf_habitat_grid")
message(proj4string(wolves.map)) # it is in Albers Equal Area projection.
#Select presence/abscense only (1 and 0)
wolfsub <- wolves.map[!wolves.map$WOLVES_99 %in% 2,]
wolfsub$MAJOR_LC <-as.numeric(as.character(wolfsub$MAJOR_LC))
# Add columns to the wolfsub dataset. 42 = Forest, 51 = Shrub, > 81 = Agriculture
wolfsub$Forest<-ifelse(wolfsub$MAJOR_LC==42,1,0)
wolfsub$Shrub<-ifelse(wolfsub$MAJOR_LC==51,1,0)
wolfsub$Agriculture <- ifelse(wolfsub$MAJOR_LC > 81,1,0)
# create the model
mod1<-glm(WOLVES_99 ~ RD_DENSITY + Forest + Shrub + Agriculture,family = binomial,data = wolfsub)
summary(mod1)
#fitted(mod1)
wolfsub$WOLVES_99pred <- fitted(mod1) # add the predicted values to wolfsub
# Convert the wolves.map shapefile to data.frame
wolves.mapDF <- as.data.frame(wolves.map)
#fortify wolves.map to be used with ggplot2
wolves.ds <- fortify(wolves.map,region="GRID2_ID")
# Rename the 'GRID2'_ID to 'id' to to be able to merge with the shapefile wolves.map
wolves.mapDF <- rename(wolves.mapDF,c(GRID2_ID="id"))
# merge the shapefile wolves.ds and wolves.mapDF dataframe to be able to use the wolves.mapDF variables with ggplot2
wolves.ggmap <- merge(wolves.ds, wolves.mapDF, by = "id", all = TRUE)
wolves.ggmap <- wolves.ggmap[order(wolves.ggmap$order), ]
wolves.ggmap$MAJOR_LC <-as.numeric(as.character(wolves.ggmap$MAJOR_LC))
### Now do the whole data set
# 42 = Forest, 51 = Shrub, > 81 = Agriculture
wolves.ggmap$Forest<-ifelse(wolves.ggmap$MAJOR_LC==42,1,0)
wolves.ggmap$Shrub<-ifelse(wolves.ggmap$MAJOR_LC==51,1,0)
wolves.ggmap$Agriculture<-ifelse(wolves.ggmap$MAJOR_LC>81,1,0)
# Predict probabilities for the whole dataset
wolves.ggmap$PredictedSuit <- predict(mod1,newdata=wolves.ggmap,type='response')
#Make PredictedSuit a factor
wolves.ggmap$DiscretePred <- cut(wolves.ggmap$PredictedSuit,breaks=c(0,0.29,0.40,0.45,0.6,0.69),dig.lab = 2,include.lowest=TRUE)
#plot and display a legend with the new cuts
Palette1 <- c('grey80','orange','yellow','green','green3','blue')
wolves.pred3 <- ggplot(wolves.ggmap,aes(long,lat,group=group)) + theme_bw() + theme_void() +
geom_polygon(aes(fill=DiscretePred), colour = alpha("white", 1/2), size = 0.2) + theme(legend.position = c(0.14, 0.16)) +
scale_fill_manual(values=Palette1) + guides(fill=guide_legend(ncol=2,"Predicted\n Suitability\n > 0.45"))
wolves.pred3
I get the following graph(good):
All of the above code works as expected. The problem that I am having is down below. The code below works well overlaying the points from a subset of the same shapefile above. However, I lose my scale_fill_manual colors when I try to add the new points to the legend.
#Extract wolves from 2001 first and overlay them on map
wolfsub_01 <- wolves.map[wolves.map$WOLVES_01 %in% 1,]
wolfsub_01$MAJOR_LC <-as.numeric(as.character(wolfsub_01$MAJOR_LC))
#Get centroids to overlay on existing plot
test <- gCentroid(wolfsub_01, byid = TRUE)
#Convert to dataframe to be used with ggplot2
wolf <- as.data.frame(wolfsub_01)
test <- as.data.frame(test)
wolves_test <- cbind(wolf,test)
#Overlay on existing plot
wolves.pred3 +
geom_point(data=wolves_test,aes(x,y,group=NULL,fill='2001 wolves'),color='blue')
If I try to include '2001 wolves' in my legend, my colors stay in the correct order. However, my legend values get all messed up. I tried to re-arrange them with a different palette but it only makes it worse because the colors and labels don't line up with the corresponding color. I also would like help with removing the dots from the legend. How can I get my colors back to my original Palette1 used above on the original plot? Probably a simple thing but I have spent many hours trying and can't figure it out. Thanks beforehand.
I get this plot. Notice the values are all over. I need the values to be in the same order as on first plot.
EDIT: This is what my plots show behind the scenes. The first plot has the following color order:
> g <- ggplot_build(wolves.pred3)
> unique(g$data[[1]]["fill"])
fill
1 grey80
9 orange
115 yellow
241 green3
271 green
And my second plot has this color order which is different from the first one. I wonder how I can make the second match the first color order.
> g <- ggplot_build(a)
> unique(g$data[[1]]["fill"])
fill
1 green3
9 grey80
115 orange
241 green
271 yellow
>