plot numbers on a map from an sf object

62 Views Asked by At

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

2

There are 2 best solutions below

0
P. Luchs On BEST ANSWER

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.

0
ramon vazquez On

this is the final solution, thanks to P.Luchs, it is applied to a point shape stored as a sf object called 'points' with the same form as the one show in the second image of my question (the values to be plotted are in the variable Rmax, maximum wind gust in 24 hours)

I have changed a couple of things from P.Luchs answer, for example the coordinate system, which is UTM30

  coors <- st_coordinates(points)

  data <- st_as_sf(as.data.frame(coors), coords = c("X", "Y"), crs = 25830)
  
  plot(st_geometry(data), type = "p", cex=.4, add=TRUE)

  text(coors[,1], coors[,2], labels = points$Rmax, pos = 1, cex=.4, offset=0.1)

the output of this code is

wind speed in km/h 2024/01/02 Galicia

thanks again,

Ramón