从 sf 对象在地图上绘制数字

问题描述 投票:0回答:2

我正在尝试绘制一张带有数字(风速)的地图,而不是每个点中带有数据的符号, 类似的东西
wind gusts

我用 GIS 应用程序制作的

问题是我在

sf
对象中绘制了数字,这样 enter image description here

数字位于名为

Rmax

的变量中

我尝试过

plot(points['Rmax'])

但显然它不起作用,它只是绘制符号,没有数字

有什么想法吗?

提前非常感谢, 拉蒙

r plot numbers point
2个回答
0
投票

有很多方法可以实现这一目标。

如果你想在 R 基础上执行此操作,你可以使用绘图和文本,如下所示:

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)

但是,如果您想要交互式可滚动地图,您可以使用传单库,如下所示:

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

使用

?addCircleMarkers
?labelOptions
查看有关如何设置标签样式的文档。


0
投票

这就是最终的解决方案,感谢@P.Luchs, 它应用于存储为名为“点”的 sf 对象的点形状,其形式与我的问题的第二张图片中显示的形式相同(要绘制的值在变量 Rmax 中,即 24 小时内的最大阵风)

我从@P.Luchs答案中更改了一些内容,例如坐标系,即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)

这段代码的输出是

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

再次感谢,

拉蒙

© www.soinside.com 2019 - 2024. All rights reserved.