0
votes

Following is an example of the data frame I have that was obtained from a publicly available crime data set for St. Louis. The documentation related to the data states that the Xcoord and Ycoord are in State Plane North American Datum 1983 (NAD83) format

   CodedMonth                                    Description   XCoord    YCoord
1:    2019-09        AUTO THEFT-PERM RETNT/UNRECOV OVER 48HR 908297.3 1018623.0
2:    2019-09        ASSLT-AGGRAV-OTH-WPN-2ND-CHILD-DOMESTIC 903995.7 1014255.0
3:    2019-09 FORGERY-ISSUING FALSE INSTRUMENT OR CERTIFICAT      0.0       0.0
4:    2019-09           STLG BY DECEIT/IDENTITY THEFT REPORT 890704.7 1010659.0
5:    2019-09          STALKING (HARASSMENT ONLY, NO THREAT) 881105.8 1008297.0
6:    2019-09               LARCENY-MTR VEH PARTS UNDER $500 882929.6  992941.3

how do I convert these into Xcoord and Ycoord columns into lon and lat format so that I can plot this using ggmap

I have found a couple of answers Convert latitude/longitude to state plane coordinates

But I cant seem to get it to work for my data

2

2 Answers

2
votes

You can use the sf package to convert it to a simple features geography.
In order to get this to work you need to know what coordinate system you are working with, and based on the description you provide (State Plane NAD83 and are near St. Louis), My first guess was EPSG 26996 (NAD83 / Missouri East USFT), but that plotted in the middle of lake Huron, so I tried ESRI: 102696. You can look up projections at spatialreference.org.

library(sf)
library(tidyverse)
library(ggmap)

my_df <- read_csv("C:/Users/Brian/Documents/temp.csv")
my_sf_df <- st_as_sf(my_df, coords = c("XCoord", "YCoord"), crs = 102696) 

This sets the x and y to spatial coordinates. You need to re-project into a geographic system like WGS84 to convert to lat long. st_transform does this for us using crs = 4326, which is the WGS 84 coordinate system


my_latlon_df <- st_transform(my_sf_df, crs = 4326 ) 
my_latlon_df <- my_latlon_df%>%
      mutate( lat= st_coordinates(my_latlon_df)[,1],
              lon = st_coordinates(my_latlon_df)[,2])
my_latlon_df

# Simple feature collection with 6 features and 5 fields
# geometry type:  POINT
# dimension:      XY
# bbox:           xmin: -93.26566 ymin: 35.80151 xmax: -90.19163 ymax: 38.63065
# epsg (SRID):    4326
# proj4string:    +proj=longlat +datum=WGS84 +no_defs
# # A tibble: 6 x 6
# X1    CodedMonth Description                                                geometry   lat   lon
# * <chr> <chr>      <chr>                                                   <POINT [°]> <dbl> <dbl>
#       1 1:    2019-09    AUTO THEFT-PERM RETNT/UNRECOV OVER 48HR        (-90.19163 38.63065) -82.2  44.7
# 2 2:    2019-09    ASSLT-AGGRAV-OTH-WPN-2ND-CHILD-DOMESTIC         (-90.20674 38.6187) -82.3  44.7
# 3 3:    2019-09    FORGERY-ISSUING FALSE INSTRUMENT OR CERTIFICAT (-93.26566 35.80151) -93.3  35.8
# 4 4:    2019-09    STLG BY DECEIT/IDENTITY THEFT REPORT           (-90.25329 38.60893) -82.4  44.6
# 5 5:    2019-09    STALKING (HARASSMENT ONLY, NO THREAT)           (-90.2869 38.60251) -82.5  44.6
# 6 6:    2019-09    LARCENY-MTR VEH PARTS UNDER $500               (-90.28065 38.56034) -82.5  44.5

We now have geographic coordinates with lat and long as columns of our data frame. The no location information is going to cause problems, since it will plot at the origin of the state plane coordinate plane, which is down in Arkansas somewhere. Let's remove it so we can focus on the good points

# let's exclude point 3 for now
my_latlon_df <- my_latlon_df[-3,]
box <- st_bbox(my_latlon_df) # bounding box
names(box) <- NULL # removing non-complient labels
buffer = .2
box2 <- box + c(-buffer, -buffer, buffer, buffer) # buffering
base_map <- get_map(location = box2, source = "osm")  # getting base map
# plotting
ggmap(base_map)+
      geom_sf(data = my_latlon_df,
              color = "red",
              size = 2
              )+
      scale_x_continuous(limits = c(-90.35, -90.1))+
      scale_y_continuous(limits = c(38.5, 38.7))

enter image description here

Unfortunately, if you don't know what coordinate system your x and y points are in, it can become a frustrating game of trial and error. The projected coordinate systems basically create a Cartesian plane on the surface of the globe, and the choice of origin, scale and other parameters are specific to each projection. There isn't nearly as much difference in geographic coordinate systems such as WGS84.

-1
votes

you can use CrimeoMeter API (https://www.crimeometer.com/docs), and get crime data from St. Louis with latitude and longitude.