Geographically Weighted Regression – Solving Problem with AES and GGPlot2 While Plotting GWR Length

geographically-weighted-regressionpolygon

Having trouble with the following code while doing a Geo-regression

MF <- read.csv("MF.csv", sep=",")
modelo1 <- lm(MF$MFALC_15_2 ~ MF$MFEFF2015+MF$agro1315+MF$MEDALC+MF$density)
summary(modelo1)
plot(modelo1, which=3) 
residuo <- residuals(modelo1)
cores <- c("green","darkgreen","blue","darkblue")
map.residuos <- SpatialPointsDataFrame(data=data.frame(residuo), 
coords=cbind(MF$COORD_X,MF$COORD_Y))
spplot(map.residuos, cuts=quantile(residuo), col.regions=cores, cex=1)
GWRBandwith <- gwr.sel(MF$MFALC_15_2 ~ 
MF$MFEFF2015+MF$agro1315+MF$MEDALC+MF$density, data=MF, 
coords=cbind(MF$COORD_X,MF$COORD_Y), adapt = TRUE)
gwr.model = gwr(MF$MFALC_15_2 ~ 
MF$MFEFF2015+MF$agro1315+MF$MEDALC+MF$density, data=MF, 
coords=cbind(MF$COORD_X,MF$COORD_Y), adapt = GWRBandwith, hatmatrix = TRUE, 
se.fit = TRUE)
gwr.model
resultadogeoregress <- as.data.frame(gwr.model$SDF)
head(resultadogeoregress)
MF$coefMFEFF2015 <- resultadogeoregress$MF.MFEFF2015
MF$coefagro1315 <- resultadogeoregress$MF.agro1315
MF$coefMEDALC <- resultadogeoregress$MF.MEDALC
MF$coefDENSITY <- resultadogeoregress$MF.density
workdir <- "C:\\Users\\pedro\\Documents\\Time Series\\Luciano"
cidades <- readShapePoly("pr2016.shp")
cidadesDF <- fortify(cidades, region = "nome")
head(cidadesDF)

ggplot(data=cidadesDF, aes(y=lat, x=long, group=group, 
fill=id))+geom_polygon()+guides(fill=FALSE)
plot(cidades) #checking the .shp structure

#this is where things start to go wrong
paranacidade <- ggplot(MF, 
aes(x=x,y=y))+geom_point(aes(colour=MF$coefMFEFF2015))+
scale_colour_gradient2(low="red", 
mid="white",high="blue",midpoint=0,space="rgb",
na.value="grey50",guide="colourbar",guide_legend(title="coefs"))
paranacidade+geom_path(data=cidadesDF, aes(group=group), 
colour="grey")+coord_equal()

It returns the error "Aesthetics must be either length 1 or the same as the data (399): colour, x, y"
The fortify function breaks the polygons of my shapefile into millions of rows, but it works for plotting the map by itself. When I try to plot the regression (MF$coefs…) within the shape the lenght of the regression (399) is not compatible with the lenght of rows in the shapefile dataframe. I've tried changing the arguments for aes (tried x=long, group=group, group=id,y=lat etc…)

The data:
shp+other: https://drive.google.com/open?id=1OAtXe9rkDUJVVqRSuO-V2Okb5b8x9aoO

csv: https://drive.google.com/open?id=1T1zm9BXyyFYJmqZphaRg72OWkCk1KpAh separated by commas

Best Answer

The problem is in ggplot(MF, aes(x=x,y=y)), MF doesn't have x and y colunmn labels, so change it to ggplot(MF, aes(x=COORD_X,y=COORD_Y)) and set x and y in cidadesDF:

paranacidade <- ggplot(MF, aes(x=COORD_X,y=COORD_Y))+geom_point(aes(colour=MF$coefMFEFF2015))+
  scale_colour_gradient2(low="red", 
                         mid="white",high="blue",midpoint=0,space="rgb", na.value="grey50",guide="colourbar",guide_legend(title="coefs"))

paranacidade+geom_path(data=cidadesDF, aes(y=lat, x=long,group=group), 
                       colour="grey")+coord_equal()

enter image description here

Related Question