0

Something is wrong with the code but I can't figure out what. I am trying to plot the coefficients of a geographically weighted regression on a map showing the districts of Barcelona. I can get R to plot the points or the map but I am unable to overlay them. What could be the issue?

Plot of the coefficients

ggplot(logairbnb, aes(x=x,y=y))+
geom_point(aes(colour=logairbnb$coefdist_center))+
scale_colour_gradientn(colours = terrain.colors(10), 
guide_legend(title="Coefs"))

Barcelona map

ggplot(neighbourhood_fortified, aes(long,lat,colour = 
factor(id))) +
geom_polygon(fill = "white", colours = 
terrain.colors(10))+coord_equal()

Something weird happens with the axes when I try to overlay the graphs

gwr.point1<-ggplot(logairbnb, aes(x=x,y=y))+
geom_point(aes(colour=logairbnb$coefdist_center))+
scale_colour_gradientn(colours = terrain.colors(10), guide_legend(title="Coefs"))

gwr.point1+
geom_polygon(data=neighbourhood_fortified,aes(long,lat,group=id),colour="grey")

2 Answers2

0

The neighbourhood_fortified is in UTM coordinates while logairbnb is in geographic coordinates.

You should transform one of them. Since they are dataframe (as you told in comments), you have to convert first to sf objects and then transform the coordinates in one of them:

logairbnb_geo <- st_as_sf(logairbnb, coords = c("x", "y"), crs=4326)
neigh_geo <- st_as_sf(neighbourhood_fortified, coords = c("long", "lat"), crs=25831)
neigh_geo <- st_transform(neighbourhood_fortified, st_crs(logairbnb))

Then you can use geom_sf to plot them:

ggplot()+
    geom_sf(data=neigh_geo, fill = "white", colours = 
terrain.colors(10)))+
    geom_sf(data=logairbnb_geo, mapping=aes(colour=coefdist_center))+
    scale_colour_gradientn(colours = terrain.colors(10), 
guide_legend(title="Coefs"))+
    coord_equal()

Note: I used the most probable crs based on your plot coordinates, you should check the right crs in your data sources.

Note2: I didn't test the code chunks since I don't have your data.

Josep Pueyo
  • 387
  • 2
  • 11
  • Oh, thank you. These are the object classes. Apparently st transform is not applicable. class(logairbnb) [1] "data.frame" > class(neighbourhood_fortified) [1] "tbl_df" "tbl" "data.frame" – user1009380 Mar 14 '22 at 19:02
  • Oh and btw, neighbourhood_fortified comes from this.`code`neighbourhood <- readOGR("BCN_UNITATS_ADM") neighbourhood_fortified <- tidy(neighbourhood, region="NOM") – user1009380 Mar 14 '22 at 20:06
0

Okay, so I found out this works.

neighbourhood <- readOGR("BCN_UNITATS_ADM")
g<-as.data.frame(logairbnb$coefdist_center)
g<-cbind(logairbnb$y,g)
g<-cbind(logairbnb$x,g)
colnames(g)[1] <- "long"
colnames(g)[2] <- "lat"
colnames(g)[3] <- "coefdist"
g<-SpatialPointsDataFrame(coords =cbind(g$long,g$lat),data = g, 
proj4string = CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 
+towgs84=0,0,0"))
g<-spTransform(g,CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 
+towgs84=0,0,0"))
neighsp<-spTransform(neighbourhood,CRS("+proj=longlat +datum=WGS84 
+ellps=WGS84 +towgs84=0,0,0"))
mapdata1 <- as.data.frame(g)
mapdata2<- fortify(neighsp, region ="NOM")

#Map
gwr.point1<-ggplot(mapdata1, aes(x=long,y=lat))+
geom_point(aes(colour=coefdist))+
scale_colour_gradientn(colours = terrain.colors(100), 
guide_legend(title="Coefs"))

gwr.point1+geom_polygon(data=mapdata2, aes(group=group), 
colour="grey",fill = "white",alpha = 1/3)
  • Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney Mar 16 '22 at 00:07