rplotnumberspoint

plot numbers on a map from an sf object


I am trying to plot a map with a number (wind velocity) instead of a symbol in each point with data, something like
wind gusts

which I produced with a GIS app

The thing is I have the plotted numbers in an sf object, this way enter image description here

the numbers are in the variable called Rmax

I have tried with plot(points['Rmax'])

But obviously it does nor work, it just plot symbols, no the numbers

Any idea?

thanks a lot in advance, Ramón


Solution

  • There are a lot of ways to achieve this.

    If you want to do this in base R you can use plot and text like so:

    library(sf)
    
    # Some coordinates in Rome in WGS84
    coords <- data.frame(
      lon = c(12.4964, 12.5113, 12.5002),  #Longitude
      lat = c(41.9028, 41.8919, 41.9134)   #Latitude
    )
    
    # Create an sf table 
    data <- st_as_sf(coords, coords = c("lon", "lat"), crs = 4326)
    
    # Add some wind speeds
    data$wind_speed <- c(10, 15, 17.5)
    
    # Create a base plot with the points
    plot(st_geometry(data), type = "p")
    
    # Extract coordinates from sf object
    coords <- st_coordinates(data)
    
    # Add numbers to the plot
    text(coords[,1], coords[,2], labels = data$wind_speed, pos = 3)
    
    

    However, if you want an interactive scrollable map you can use the leaflet library for example like so:

    library(leaflet)
    
    leaflet(data = data) %>%
      addTiles() %>% # Add a background map
      addCircleMarkers(label = ~as.character(wind_speed), labelOptions = list(permanent = T), radius = 10) # Add the circle markers with a label
    

    use ?addCircleMarkers and ?labelOptions to check out the documentation on how to style your labels.