Home > Net >  Point not plotting over a shapefile
Point not plotting over a shapefile

Time:03-12

I am trying to plot a shapefile with Poland's Powiaty (counties) and a dot where Krakow is. This is the code that I'm using. For some reason, that dot is not appearing in the plot.

This is where I got the shapefile of Poland's Powiaty: Output

Does anyone know how I can be able to add Krakow to the map?

CodePudding user response:

You're close, but the crs seems to be the problem.

*edited for full reprex:

library(sf)
library(dplyr) #for the pipe %>%
library(ggplot2)

# data from:  https://gis-support.pl/baza-wiedzy-2/dane-do-pobrania/granice-administracyjne/
# file downloaded:  https://www.gis-support.pl/downloads/Powiaty.zip

# read shapefile, retain only geometry column
poland_Powiaty <- read_sf('Powiaty.shp') %>%  #change filename as needed
                    st_geometry()

# In latitude/longitude
krakow <- data.frame(x=19.940000819941382, 
                     y=50.06197893970872) %>%
  st_as_sf(coords = c('x', 'y'))

# set crs to 4326 for lat/lon
krakow <- st_set_crs(krakow, 4326)
# transform krakow to have same crs as poland_Powiaty
krakow <- st_transform(krakow, st_crs(poland_Powiaty))

# base plot
plot(poland_Powiaty)
plot(krakow, add = T, pch = 15, col = 'red')


##  ggplot2
ggplot()   
  geom_sf(data = poland_Powiaty)  
  geom_sf(data = krakow, color = 'red', size = 3)

Created on 2022-03-11 by the reprex package (v0.3.0)

  • Related