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)
plot(modelo1, which=3) 
residuo <- residuals(modelo1)
cores <- c("green","darkgreen","blue","darkblue")
map.residuos <- SpatialPointsDataFrame(data=data.frame(residuo), 
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)
resultadogeoregress <- as.data.frame(gwr.model$SDF)
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")

ggplot(data=cidadesDF, aes(y=lat, x=long, group=group, 
plot(cidades) #checking the .shp structure

#this is where things start to go wrong
paranacidade <- ggplot(MF, 
paranacidade+geom_path(data=cidadesDF, aes(group=group), 

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

  • Welcome to GIS SE, please ckeck the tour and edit your question. Also, upload some data or make a reproducible example to know where the issue is
    – aldo_tapia
    Apr 20, 2018 at 12:16
  • Done, uploaded data on gdrive
    – Pedro Iora
    Apr 20, 2018 at 13:04
  • You upload only .shp file. Do you know how to share shapefiles?
    – aldo_tapia
    Apr 20, 2018 at 14:41
  • Sorry, my mistake, reuploading the .rar file with all the correct extensions. Having some troubles because of doing so through the cellphone
    – Pedro Iora
    Apr 20, 2018 at 14:49

1 Answer 1


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))+
                         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), 

enter image description here

  • I tried MF$COORD_X instead of COORD_X as argument for aes for some reason. Thanks, worked like a charm
    – Pedro Iora
    Apr 20, 2018 at 16:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.