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: https://www.gis-support.pl/downloads/Powiaty.zip?_ga=2.76556755.796824239.1647028537-1312541903.1646793893
library(sf)
# Load shapefile
poland_Powiaty <- st_read("~/Powiaty.shp")
### Krakow's coordinates
krakow <- data.frame(x=19.940000819941382,
y=50.06197893970872)
point <- st_as_sf(krakow, coords = c("x", "y"))
# Set the CRS of shapefile
st_crs(point) <- "+proj=tmerc +lat_0=0 +lon_0=19 +k=0.9993 +x_0=500000 +y_0=-5300000 +ellps=GRS80
+towgs84=0,0,0,0,0,0,0 +units=m +no_defs " # assign CRS to point
plot(poland_Powiaty[2])
plot(point,col="black",pch=19, cex=3,add=T)
Does anyone know how I can be able to add Krakow to the map?
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)