用R中的sf将美学映射到LINESTRING几何

时间:2017-10-12 19:27:40

标签: r ggplot2 sf

R的new-ish sf包让它很容易处理 R中的地理数据,ggplot2的开发版本有一个新的 用于绘制sf风格地理数据的geom_sf()图层。

在处理数据的sf范例内,是否可以进行映射 ggplot aestheics到LINESTRING几何?

例如,使用标准ggplot,可以重新创建Minard's famous plot of survivors from Napoleon's Grande Armée in 1812 使用ggplot和this data,调整路径大小 幸存者人数的军队:

# Install the dev version of ggplot2 for geom_sf()
# devtools::install_github("tidyverse/ggplot2")
library(tidyverse)

troops <- read_csv("https://gist.githubusercontent.com/andrewheiss/69b9dffb7cca392eb7f9bdf56789140f/raw/3e2a48635ae44837955765b5e7747c429b0b5d71/troops.csv")

ggplot(troops) +
  geom_path(aes(x = long, y = lat, color = direction, 
                group = group, size = survivors),
            lineend = "round")

我们可以通过创建新的sf对象来处理此部队数据 geometry列,如下:

library(sf)
#> Linking to GEOS 3.6.1, GDAL 2.1.3, proj.4 4.9.3

troops_with_geometry <- troops %>%
  st_as_sf(coords = c("long", "lat"))

head(troops_with_geometry)
#> Simple feature collection with 6 features and 3 fields
#> geometry type:  POINT
#> dimension:      XY
#> bbox:           xmin: 24 ymin: 54.5 xmax: 28 ymax: 55
#> epsg (SRID):    NA
#> proj4string:    NA
#> # A tibble: 6 x 4
#>   survivors direction group          geometry
#>       <int>     <chr> <int>  <simple_feature>
#> 1    340000         A     1 <POINT (24 54.9)>
#> 2    340000         A     1 <POINT (24.5 55)>
#> 3    340000         A     1 <POINT (25.5 ...>
#> 4    320000         A     1 <POINT (26 54.7)>
#> 5    300000         A     1 <POINT (27 54.8)>
#> 6    280000         A     1 <POINT (28 54.9)>

如果我们用geom_sf绘制它,ggplot将使用points:

ggplot(troops_with_geometry) +
  geom_sf(aes(color = direction, group = group))

我们可以为每个组和方向创建线串 分组,总结和演员。

troops_lines <- troops_with_geometry %>%
  group_by(direction, group) %>% 
  summarize() %>% 
  st_cast("LINESTRING")

head(troops_lines)
#> Simple feature collection with 6 features and 2 fields
#> geometry type:  LINESTRING
#> dimension:      XY
#> bbox:           xmin: 24 ymin: 54.1 xmax: 37.7 ymax: 55.8
#> epsg (SRID):    NA
#> proj4string:    NA
#>   direction group                       geometry
#> 1         A     1 LINESTRING (24 54.9, 24.5 5...
#> 2         A     2 LINESTRING (24 55.1, 24.5 5...
#> 3         A     3 LINESTRING (24 55.2, 24.5 5...
#> 4         R     1 LINESTRING (24.1 54.4, 24.2...
#> 5         R     2 LINESTRING (28.3 54.2, 28.5...
#> 6         R     3 LINESTRING (24.1 54.4, 24.2...

ggplot然后可以绘制这六条相连的线并正确地着色它们:

ggplot(troops_lines) +
  geom_sf(aes(color = direction, group = group))

然而,幸存者数据现在已经消失,无法映射大小 对新线条的美学。

有没有办法将其他美学(如大小)与sf相关联 LINESTRING数据?或者,换句话说,有没有办法重新创建 ggplot(...) + geom_path(aes(x = long, y = lat, size = something)) 使用geom_sf()和使用地理数据的sf范例?

1 个答案:

答案 0 :(得分:1)

enter image description here您需要从每组中的每对点创建一个线串。结果不是那么漂亮,因为我不知道如何将这些行放在端点上。

import sys
import paramiko
import getpass

def print_menu():
    print 30 * "-", "MENU", 30 * "-"
    print "1. LAB1"
    print "2. LAB2"
    print "3. LAB3"
    print "4. Exit"
    print 67 * "-"

def ssh_command(ssh):
    while True:
        command = raw_input("Enter command or q :")
        ssh.invoke_shell()
        stdin, stdout, stderr = ssh.exec_command(command)
        stdout = stdout.readlines()
        if command == "q":
                break
        for line in stdout:
            if "Size" in line:
                print "found the string"
                break`enter code here`
            else:
                print "There was no output for this command"




def ssh_connect(host, user, password):
    try:
        ssh = paramiko.SSHClient()
        print('Connecting...')
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(hostname=host, username=user, password=password)
        ssh_command(ssh)
    except Exception as e:
        print('Connection Failed')
        print(e)

def ssh_close():
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.close()

def credentials(host):
    user = raw_input("Username:")
    password = getpass.getpass("password for " + user + ":")
    ssh_connect(host, user, password)




loop = True

while loop:  
    print_menu()  
    choice = input("Enter your choice [1-3]: ")
    if choice == 1:
        credentials('x.x.x.x')
    elif choice == 2:
        credentials('x.x.x.x')
    elif choice == 3:
        credentials('x.x.x.x')
    elif choice == 4:
        loop = False
        print "Closing SSH connection"
        print
        ssh_close()
    else:
        raw_input("Wrong option selection. Enter any key to try again..")