0

I am working with a dataset that features chemical analyses from different locations within a cave, with each analysis ordered by a site number and that sites latitude and longitude. This first image is what I had done originally simply using ggplot. Map of site data, colored by N concentration

But what I want to do is use the shapefile of the cave system from which the data is sourced from and do something similar by plotting the points over the system and then coloring them by concentration. This below is the shapefile that I uploaded Cave system shapefile Cave system shapefile

So basically I want to be able to map the chemical data from my dataset used to map the first figure, but on the map of the shapefile. Initially it kept on saying that it could not plot on top of it. So I figured I had to convert the latitude and longitude into spatial coordinates that could then be mapped on the shapefile.

    Master_Cave_data <- Master_cave_data %>%
    st_as_sf(MastMaster_cave_data, agr = "identity", coord = Lat_DD)

This was what I had thought to use in order to convert the numerical Latitude cooridnates into spatial data.

JackWassik
  • 31
  • 4

1 Answers1

3

I assume your coordinates are in WSG84 projection system (crs code 4326). You can create your sf object the following way:

Master_Cave_data <- st_as_sf(MastMaster_cave_data, coords = c('lon', 'lat'), crs = 4326)

Change lon and lat columns to relevent names. To plot your points with your shapefile, you need them both in the same projection system so reproject if needed:

Master_Cave_data <- Master_cave_data %>% st_transform(st_crs(shapefile))

Example

Borrowed from there

df <- data.frame(place = "London", 
       lat = 51.5074, lon = 0.1278,
       population = 8500000) # just to add some value that is plotable
crs <- 4326
df <- st_as_sf(x = df,                         
           coords = c("lon", "lat"),
           crs = crs)

And you can have a look at the map:

library(tmap)
data("World")    
tm_shape(World[World$iso_a3 == "GBR", ]) + tm_polygons("pop_est") + 
    tm_shape(df) + tm_bubbles("population")
linog
  • 4,673
  • 2
  • 9
  • 20
  • Hi thank you for the help. However when I was trying to run it I was given the followed error: Error in st_sf(x, ..., agr = agr, sf_column_name = sf_column_name) : no simple features geometry column present – JackWassik Apr 28 '20 at 20:02
  • I corrected, I think the pipe was unecessary. If it does not work, could you edit the answer to share the structure of the data? (e.g. with `dput()`) – linog Apr 29 '20 at 06:18
  • So the transformation seems not to work for me. keep getting this error Error in UseMethod("st_transform") : no applicable method for 'st_transform' applied to an object of class "data.frame" – JackWassik Apr 29 '20 at 14:14
  • What is `class(Master_cave_data)` after `Master_Cave_data – linog Apr 29 '20 at 15:14
  • So [1] "sf" "tbl_df" "tbl" "data.frame" is what is returned – JackWassik Apr 29 '20 at 16:11
  • Could you add to your question `dput` for `Master_Cave_data` and for your shapefile ? That would help – linog Apr 29 '20 at 16:43
  • Im unfamiliar with that function? Where would I place that in the code. Sorry for so many questions. – JackWassik Apr 29 '20 at 16:45
  • No problem. It's a function that gives the structure of your data. You can give us the output of `dput` before transforming the dataframe `Master_Cave_data ` to `sf` and give the `dput` of your shapefile also. Such that, anybody wanting to answer your question will be able to reproduce your problem – linog Apr 29 '20 at 16:48
  • So it is a very long dataset but with the coordinate data before I do the transformation I get 'Lat_DD = c(38.2344 and Long_DD = c(-90.1389' and then after I do the transformation the same columns are brought up but at the bottom has 'geometry = structure(list(structure(c(-90.1389, 38.2344), class = c("XY", "POINT", "sfg")' – JackWassik Apr 29 '20 at 16:53