- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想使用带有 ggplot
和 geom_sf
的变量为 sf
LINESTRING 的不同部分着色。我可以使用 geom_path
做类似的事情,但在 geom_sf
中,类似的方法似乎不起作用。有人可以提供一种可能的方法吗?
示例数据
library(sf)
library(ggplot2)
library(dplyr)
library(tibble)
df <- tibble(time = seq(1,21),
lon = seq(-50,-30, 1) + rnorm(n = 21),
lat = seq(10, 20, 0.5) + rnorm(n = 21),
type = c(rep('A',5),rep('B',10), rep('A',6)))
使用 tibble/dataframe:
使用 tibble 和基本的 ggplot 我可以做到这一点,合并 group = 1
并且将绘制一条线,不同的部分由 type
着色。这是我想要制作的情节类型,但使用的是 sf
对象。
ggplot() +
geom_path(data = df,
aes(lon, lat, color = type, group = 1))
使用 sf
对象/LINESTRING
如果我使用 group_by
和 type
转换为 LINESTRING,我最终会得到两个 LINESTRING
df_sf <- st_as_sf(df, coords = c('lon','lat')) %>%
st_set_crs(.,value = 4326) %>%
group_by(type) %>%
summarize(do_union = TRUE) %>%
st_cast(.,'LINESTRING')
然后,当我对下面的代码进行变体时,我得到了两条单独的线,并且 type = A 的两个部分是相连的。
ggplot() +
geom_sf(data = df_sf,
aes(color = type, group = 1))
有没有办法使用 geom_sf()
方法实现 ggplot + geom_path()
类型的行为(这样我就可以投影变量等)?
最佳答案
我找到了一种将线分成线段的方法 here ,但这对于您的用例来说可能有点矫枉过正......
试试这个:
df_sf <- df %>%
# ensure data is sorted along x-axis
arrange(lon) %>%
# detect each time type changes, & create a duplicate point with previous type
mutate(change.type = tidyr::replace_na(lag(type) != type, FALSE)) %>%
mutate(type = ifelse(change.type,
paste(lag(type), type, sep = ";"),
type) %>%
strsplit(";")) %>%
tidyr::unnest(cols = c(type)) %>%
# create new group column that increments with every colour change
mutate(change.type = tidyr::replace_na(lag(type) != type, FALSE)) %>%
mutate(new.type = cumsum(change.type)) %>%
st_as_sf(coords = c('lon', 'lat')) %>%
st_set_crs(., value = 4326) %>%
# group by both original type (for colour) & new type (for group)
group_by(type, new.type) %>%
summarize(do_union = TRUE) %>%
st_cast(.,'LINESTRING') %>%
ungroup()
比较结果
cowplot::plot_grid(
ggplot() +
geom_path(data = arrange(df, lon),
aes(lon, lat, color = type, group = 1), size = 1) +
ggtitle("geom_path approach") +
theme(legend.position = "bottom"),
ggplot() +
geom_sf(data = df_sf,
aes(color = type, group = new.type), size = 1) +
ggtitle("geom_sf approach") +
theme(legend.position = "bottom"),
nrow = 1
)
数据:
set.seed(123)
df <- tibble(time = seq(1,21),
lon = seq(-50,-30, 1) + rnorm(n = 21),
lat = seq(10, 20, 0.5) + rnorm(n = 21),
type = c(rep('A', 5), rep('B', 10), rep('A', 6)))
关于r - sf LINESTRING 的颜色部分(按变量),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63570245/
来自 here线性/区域应该工作。但是下面的代码导致编译错误? #include #include #include #include #include #include #include
我对 mySQL 空间函数有疑问。我的目标是查明 LINESTRING 对象是否穿过 POLYGON 对象。为了确定我已经尝试用两个 LINESTRING 对象进行试验以确定它们是否交叉。 SET @
所以我在 PostgreSQL 数据库中有一列同时包含 POINT 对象和 LINESTRING 对象。现在我的最终目标是从这些对象中提取纬度和经度。对于 POINT 对象,我可以通过查询简单地做到这
我一直在 iOS 版谷歌地图中以自定义方式规划路线。 如何解析 LINESTRING 中传入的 JSON? 我的线路: "coordInfo": "LINESTRING (28.64675172929
我有 geojson 文件。此文件包含 LineString 列表。我想从此列表创建多边形。但是每个 LineString 都是一个特征,我不知道哪个 LineString 创建了具体的多边形。注意
我的数据集包含一个 LineString我想过滤掉这个 LineString 的各个线段.更准确地说,每一个街道段。 到目前为止,我已经从数据集中提取了各个点并将它们保存在一个单独的列表中。此外,我想
我有一个渲染 LineStrings 的图层,并尝试对线条应用发光效果。我创建的样式使用自定义渲染器来创建具有垂直于每个线段的渐变的笔划: const glow_style = new Style({
我正在使用 SQL Server 2008 和 Geometry 数据类型来存储英国道路列表,该列表是我从 Ordanance Survey STRATEGI 导入的。数据集。 每条道路被分成多行,每
我有一个渲染 LineStrings 的图层,并尝试对线条应用发光效果。我创建的样式使用自定义渲染器来创建具有垂直于每个线段的渐变的笔划: const glow_style = new Style({
我尝试使用最新的稳定 OpenLayers 库绘制宽度以米为单位的 polyLine(道路车道),但它没有正确的实际宽度(在 Google Maps 图层顶部检查)。 这是对我有用的代码片段: pol
很抱歉打扰你们,但我已经被他的问题困扰了半天。 我想使用 LineString 对象在 OpenLayers 中绘制折线,所以我从文档中复制了示例。它运行正常,但我在屏幕上看不到这条线 代码是这样的
我有一个 LineString 序列,例如 lineString1 和 lineString2 其中 lineString1.getEndPoint() == lineString2.getStart
我使用 LINESTRING() 将路线的 GPS 坐标存储在一行中。当我想处理这些数据时,我需要使用另一个函数,即 X()、Y()、ASTEXT()、 GEOMFROMTEXT()等 我在想,如果再
好的,所以我知道如何将数据从 php 格式转换为 geojson 格式,但我不明白如何将其转换为 lineString 类型这是代码,所以问题是如何将坐标数据转换为一个数组: include(
我有一个名为 locations 的表,其中包含这些行: id uuid NOT NULL, "deviceId" text COLLATE pg_catalog."default", "userId
在 PostGIS 中,您可以使用以下方法使两个几何相交: geometry ST_Intersection (geometry geomA, geometry geomB); 在我的例子中,geom
在使用 shapely 时,我遇到了一个奇怪的问题。有 2 个点 p1 和 p2,第一个属于多边形,第二个不属于。当我试图找到包含这 2 个点的 LineString 与多边形边界线的端点之间的交点时
我有一组 GPS 位置,我用 Mapbox 将其放在 map 上,然后在点之间画一条线。 有没有办法画一条沿着道路的平滑/智能线?而不是我在图纸上得到的? Single marker
我正在尝试创建两个数组的单个 LineString 对象。我已经尝试了多种方法,但我不断得到: >>> array = [(0,0),(1,0),(2,0),(3,0)][(1,3),(1.4,2),
我正在尝试在离其他坐标最近的点处拆分 Shapely LineString。我可以使用 project 和 interpolate 获得直线上的最近点,但此时我无法拆分直线,因为它不是顶点。 我需要沿
我是一名优秀的程序员,十分优秀!