Read origin of this answer at end before trying
#==================================================
In R, you can convert your coordinates from degrees, minutes, and seconds (DMS) to decimal degrees using the sp
package. The convention you want to use, where negative longitude values are in the western hemisphere and negative latitude values are in the southern hemisphere, is the standard in geographic coordinate systems[5].
Here's a simple function to convert DMS to decimal degrees:
dms_to_decimal <- function(degrees, minutes, seconds, hemisphere) {
decimal <- degrees + minutes / 60 + seconds / 3600
if (hemisphere %in% c('S', 'W')) {
decimal <- -decimal
}
return(decimal)
}
You can apply this function to your data frame columns accordingly. For example, if you have a data frame df
with columns Lat_Deg
, Lat_Min
, Lat_Sec
, Lat_Hem
, Long_Deg
, Long_Min
, Long_Sec
, Long_Hem
, you can create new columns Lat
and Long
in decimal degrees as follows:
df$Lat <- with(df, mapply(dms_to_decimal, Lat_Deg, Lat_Min, Lat_Sec, Lat_Hem))
df$Long <- with(df, mapply(dms_to_decimal, Long_Deg, Long_Min, Long_Sec, Long_Hem))
Once you have your coordinates in decimal degrees, you can use geom_sf
in ggplot2 to plot your data. The geom_sf
function automatically handles the projection of your data, so you don't need to worry about converting your coordinates to a different system[3].
Remember to ensure that your data is in the correct format before plotting. For instance, the sf
package in R allows you to create spatial objects from data frames, which can then be used with geom_sf
[3]. If your data is not already in this format, you can use the st_as_sf
function from the sf
package to convert it[3].
Citations:
[1] r - Converting latitude and longitude - Stack Overflow
[2] Standard Latitudes and Longitudes | R-bloggers
[3] Introduction to geospatial data analysis in R
[4] r - Converting DMS to coordinates with +/- signs - Geographic Information Systems Stack Exchange
[5] Chapter 2 Spatial data in R | Spatial Statistics for Data Science: Theory and Practice with R
Comment: This is from a bot. It looks reasonable, but I haven't tested.