How do I set longitude and latitude as the axes for this ggplot of a map?

650 Views Asked by At

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

There are 2 best solutions below

1
On

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)
2
On

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.