【问题标题】:GPS Heatmap with ggplot (geom_Path)GPS 热图与 ggplot (geom_Path)
【发布时间】:2023-04-03 03:08:01
【问题描述】:

我想创建类似的东西:

我使用 ggplot2 运行:

恕我直言,实现这一目标的最佳方法是使用 geom_path,但我遇到了一些问题。 我的数据库有 3 列: ID(运行的id,对数据分组有用) 纬度 朗 这里有一个示例: https://docs.google.com/spreadsheets/d/1lUUwVUzt4wCvbDLSrusfH9pVFXw-8icC8mLJ0QKBYKU/edit?usp=sharing

附言。抱歉,数据太大,无法输入

1:如何计算路径的强度。我正在考虑计算一个范围内(例如 50m)内的坐标数(纬度,经度)。 所以几个纬度,经度附近有很多坐标。这是一个强度很高的点。问题在于数据的维度。我有很多行,我的计算能力无法计算出这个大矩阵(著名的错误:无法分配向量)。 我克服错误的解决方案是计算每行距离的循环:

npoints_2<-c()
for (i in 1:nrow(db)){
  vector_distance<-distm(db[i,2:3], db[-i,2:3], fun = distHaversine)  #for each row calculate the distance between the point and the dataset
  npoints<-sum(vector_distance<50) #count points within 50m
  npoints_2<-c(npoints_2,npoints)
}

因为它是一个循环,所以这个解决方案非常慢(而且不可能)。 必须有一个更聪明的解决方案,但可能我太菜鸟,所以请帮助我。

这种方法经过几次运行的结果是这样的:

ggplot(db, aes(lon,lat, group=id,color = log(n_punti_2))) + 
geom_path() +
scale_colour_continuous(low = "darkblue", high = "cyan")

这就引入了第二个问题:相似的路线,坐标差异不大。

将相似路线组合在一起的最佳方式是什么?恕我直言,出于图形原因,最好使用一条强度高的单行,而不是使用 2/3/n 条具有相似强度的非常相似的行

提前感谢您的帮助。非常感谢!!!

【问题讨论】:

标签: r ggplot2 gps geospatial heatmap


【解决方案1】:

我尝试过更好地模仿 Strava 的外观 - 人们意识到这些地图的创建者所做的工作是多么出色。如果您放大这些地图,您会发现这些线条渲染得非常好。

library(tidyverse)
library(ggpointdensity)

runs <- read.csv("your_path.csv", stringsAsFactors = FALSE)

ggplot(runs, aes(lon, lat)) +
  geom_pointdensity(size = .8) +
  scale_color_gradient2(low = "#340707", mid = "#d95e23", high = "#fffdee",
                        midpoint = 3500
                        ) +
  theme(panel.background = element_rect(fill = '#160e16'),
        panel.grid = element_blank())

reprex package (v0.3.0) 于 2020-04-07 创建

【讨论】:

    【解决方案2】:

    使用您包含的数据很难看到,但这是可能的。

    使用sf 包和ggplot2,您可以绘制两次点并在第二次调用中更改颜色和alpha。

    library(tidyverse)
    library(sf)
    
    #Read File 
    x <- read_csv(#Your file location here)
    
    #Change file to sf object
    x <- st_as_sf(x, coords = c('lon', 'lat')) %>% st_set_crs(4326)
    
    # Plot the paths twice.  Once in white and again with a different color.
    #  Set alpha level of second plot to allow a 'bleed through' of the
    #  first color.
    ggplot(x) + 
      geom_sf(color = 'orangered4', size = .1) +
      geom_sf(alpha = .03, color = 'white', size = .1) + 
      theme_void() + 
      theme(panel.background = element_rect(fill = 'black'))
    

    因为那里有很多点,所以您跑的赛道显示得更接近白色,而沿着海岸跑步的起点和终点在点较少的地方更暗。

    【讨论】:

      猜你喜欢
      • 2020-03-10
      • 2020-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-28
      相关资源
      最近更新 更多