Home > OS >  Changing scale to lat/long using sf objects in R
Changing scale to lat/long using sf objects in R

Time:04-19

I'm afraid I just cannot figure this out. I am trying to plot regions in states/US territories using collections of counties; wrong units

I do not understand the units on the axes, but I would like them to be in lat/long -- I thought that was the purpose of the coord_sf() function. I've tried adding x and y limits (using the approximate boundaries of the state), but when I do that, the image falls off the page.

In other cases, the output appears rotated/tilted (see image with Oregon/Idaho/Montana) - I'm hoping that the conversion to lat/long addresses this as well.rotated states

CodePudding user response:

These numbers are latitude / longitude, but they are in a different co-ordinate system from the one you want. You need to re-project the data using an appropriate crs:

library(sf)
library(ggplot2)
library(ggrepel)

url <- "https://raw.githubusercontent.com/Doc-Midnight/Test_Dir/main/AK_Test2"

ak_test <- source(url)

ak_test <- ak_test$value

ak_test$geometry <- st_transform(ak_test$geometry, "WGS84")

g <- ggplot(ak_test)
g <- g   geom_sf(aes(geometry = geometry, fill = Region), 
                 color = "black", show.legend = FALSE)
g <- g   geom_sf_label(aes(geometry = geometry, label = County))
g   labs(x = "longitude", y = "latitude")

enter image description here

Note, I have no idea where geom_sf_label_repel comes from, so I have used non-repeling labels since they are incidental to the question.

  • Related