Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R中使用sf将美学映射到线串几何图形_R_Ggplot2_Sf - Fatal编程技术网

在R中使用sf将美学映射到线串几何图形

在R中使用sf将美学映射到线串几何图形,r,ggplot2,sf,R,Ggplot2,Sf,新的ishsfR软件包使它非常容易处理 R中的地理数据,并且ggplot2的开发版本具有新的 geom_sf()用于打印sf样式地理数据的图层 在使用数据的sf范式中,是否可以映射 GG将美学绘制到LINESTRING几何图形 例如,使用标准ggplot,可以重新创建 使用ggplot和,调整路径大小 按生还者人数计算的军队人数: #为geom_sf()安装ggplot2的开发版本 #devtools::install_github(“tidyverse/ggplot2”) 图书馆(tidyve

新的ish
sf
R软件包使它非常容易处理 R中的地理数据,并且
ggplot2
的开发版本具有新的
geom_sf()
用于打印sf样式地理数据的图层

在使用数据的
sf
范式中,是否可以映射 GG将美学绘制到
LINESTRING
几何图形

例如,使用标准ggplot,可以重新创建 使用ggplot和,调整路径大小 按生还者人数计算的军队人数:

#为geom_sf()安装ggplot2的开发版本
#devtools::install_github(“tidyverse/ggplot2”)
图书馆(tidyverse)
部队连接到GEOS 3.6.1、GDAL 2.1.3、项目4.9.3
部队\u与\u几何体%
st_as_sf(坐标=c(“长”、“横向”))
头部(带几何体的部队)
#>具有6个功能和3个字段的简单功能集合
#>几何图形类型:点
#>尺寸:XY
#>bbox:xmin:24ymin:54.5xmax:28ymax:55
#>epsg(SRID):NA
#>proj4string:NA
#>#tibble:6 x 4
#>方向群几何
#>               
#>1340000 A 1
#>2 340000 A 1
#>3 340000 A 1
#>432万A 1
#>5 30万A 1
#>62800000 A 1
如果我们使用
geom_sf
绘制此图,ggplot将使用点:

ggplot(带几何图形的部队)+
geom_sf(aes(颜色=方向,组=组))

我们可以通过以下方式为每个组和方向创建线字符串: 分组、总结和铸造

部队\u线%
分组人(方向、分组)%>%
汇总()%>%
st_铸造(“线串”)
团长(部队线)
#>具有6个功能和2个字段的简单功能集合
#>几何图形类型:线条字符串
#>尺寸:XY
#>bbox:xmin:24ymin:54.1xmax:37.7ymax:55.8
#>epsg(SRID):NA
#>proj4string:NA
#>方向组几何
#>1 A 1行字符串(24 54.9、24.5 5。。。
#>2 A 2线串(24 55.1,24.5 5。。。
#>3 A 3线串(24 55.2,24.5 5。。。
#>4 R 1线串(24.1 54.4,24.2。。。
#>5 R 2线串(28.3 54.2,28.5。。。
#>6 R 3线串(24.1 54.4,24.2。。。
然后,ggplot可以绘制这六条连接线,并将其正确着色:

ggplot(部队线)+
geom_sf(aes(颜色=方向,组=组))

然而,幸存者的数据现在已经不存在了,也没有办法绘制地图的大小 新线条的美学

是否有一种方法可以将其他美学(如尺寸)与基于sf的
LINESTRING
data?或者,换句话说,有没有重新创建的方法
ggplot(…)+geom_路径(aes(x=long,y=lat,size=something))
使用
geom_sf()
和处理地理数据的sf范式?

您需要从每组中的每对点创建一个线字符串。结果不太好,因为我不知道如何在端点周围绘制线

# within each group repeat each point 
# then slice the first and last out and 
# add a variable called linegroup, which provides grouping for start and endpoints of each line
troops %<>% group_by(group) %>%
  slice(rep(1:n(), each = 2)) %>%
  slice(-c(1, n())) %>%
  mutate(linegroup = lapply(1:(n()/2), function(x) rep(x, 2)) %>% unlist) %>% 
  ungroup

# create linestring sf object by summarizing the points, 
# grab the last survivor and direction value of each group (i.e. the 'endpoint' value)
troops_line <- st_as_sf(troops, coords = c("long", "lat"), crs = 4326) %>%
  group_by(group, linegroup) %>%
  summarise(survivors = last(survivors), direction = last(direction), do_union = FALSE) %>%
  st_cast("LINESTRING")

gp <- ggplot(troops_line) +
  geom_sf(aes(color = direction, size = survivors), show.legend = "line") 
#在每组内重复每一点
#然后把第一个和最后一个切成薄片
#添加一个名为linegroup的变量,该变量为每条线的起点和终点提供分组
部队%%groupby(group)%%>%
切片(rep(1:n(),每个=2))%>%
切片(-c(1,n())%>%
mutate(linegroup=lappy(1:(n()/2),函数(x)rep(x,2))%%>%unlist)%%>%
解组
#通过汇总点创建linestring sf对象,
#抓取每组的最后一个幸存者和方向值(即“端点”值)
部队(单位)
分组依据(分组、线组)%>%
总结(幸存者=最后一个(幸存者),方向=最后一个(方向),do_union=FALSE)%>%
st_铸造(“线串”)

gp
geom\u sf
似乎只在ggplot2()的开发版本中可用。在复制之前,我必须从github(
devtools::install\u github(“tidyverse/ggplot2”)
)进行安装。哦,是的,就是这样。您需要开发版本:)