Enrique
Enrique

Reputation: 3

Trying to display two sf datasets in r via ggplot but not getting the expected result

**Hi,

Trying to overlay one shp map (Mexico city) with the dataset from kaggle for airbnb (in Mexico city) in r. If I plot them separately it work fine, but when trying to plot them together it shows something weird... here is the code and results so far:**

cdmx3 <- st_read("inegi/09_CDMX/cartografia/municipal.shp")
ggplot() + 
  geom_sf(data = cdmx3, color = "black") + 
  ggtitle("Municipios de CDMX") 

enter image description here

airbnb_mexico <- read.csv(file = "listings.csv")
airbnb_mexico_sf <- st_as_sf(x = airbnb_mexico, coords = c("longitude","latitude"))
ggplot() + 
  ggtitle("Municipios de CDMX") + 
  geom_sf(data= airbnb_mexico_sf, color = "blue")

enter image description here

st_crs(airbnb_mexico_sf) <- st_crs(cdmx3)

But when trying to overlay, it shows this:

ggplot() + 
  geom_sf(data = cdmx3, color = "black", fill = "cyan1") + 
  ggtitle("Municipios de CDMX") + 
  geom_sf(data= airbnb_mexico_sf, color = "blue")

enter image description here

Here is a summary of each sf file:

cdmx3 Simple feature collection with 16 features and 261 fields Geometry type: POLYGON Dimension: XY Bounding box: xmin: 2776218 ymin: 786788.9 xmax: 2820850 ymax: 846749.5 Projected CRS: Mexico_ITRF2008_LCC First 10 features:...

airbnb_mexico_sf Simple feature collection with 19852 features and 14 fields Geometry type: POINT Dimension: XY Bounding box: xmin: -99.33963 ymin: 19.16914 xmax: -98.96297 ymax: 19.55734 Projected CRS: Mexico_ITRF2008_LCC First 10 features:...

**Any feedback or suggestion is greatly appreciated... **

I tried having both sf sets to have the same CRS (Mexico_ITRF2008_LCC) but it did nothing...

Upvotes: 0

Views: 159

Answers (1)

Jindra Lacko
Jindra Lacko

Reputation: 8719

You want to change this line:

    st_crs(airbnb_mexico_sf) <- st_crs(cdmx3)

to

    airbnb_mexico_sf <- airbnb_mexico_sf %>%
       st_transform(crs = st_crs(cdmx3))

The issue is that you are overwriting CRS information without actually reprojecting the shape. Which is what you need to do...

The direct assigment of st_crs() is best reserved for error corrections - situations when information about CRS is present, but known to be wrong.

Also consider replacing

airbnb_mexico_sf <- st_as_sf(x = airbnb_mexico, coords = c("longitude","latitude"))

with

airbnb_mexico_sf <- st_as_sf(x = airbnb_mexico, coords = c("longitude","latitude"), crs = 4326)

The crs = 4326 is important, it tells {sf} how to interpret your coordinates - do the numbers mean degrees of lat & long, or meters of distance? US Survey feet perhaps? The 4326 makes sure the data is interpreted in degrees on a sphere (a good default when dealing with GPS coordinates).

Upvotes: 2

Related Questions