如何根据州级数据制作美国地图?

时间:2019-03-25 19:18:04

标签: r ggplot2

我正在尝试使用ggplot2根据某些州级别的数据制作美国地图,并根据一个变量的值为每个州着色。

State   loan
AL      25310770
AK      45310770
AZ      35310770
AR      25682770
   ...

编辑:感谢@Hector Haffenden,dput(head(your_data))给出了:

structure(list(state = c("AL", "AK", "AZ", "AR", "IL", "MA"), 
     loan = c(25310770, 21230922, 15055436, 15212963, 12796921, 20311736), 
     row.names = c(NA, 6L), class = "data.frame")

由于我有一个州名变量,是否可以根据州名缩写自动将每一行与地图匹配?这是我预期输出的示例:

https://i.imgur.com/0CD4fOx.png

1 个答案:

答案 0 :(得分:2)

尝试一下,首先像这样定义我们的数据,

import socket
import select
import msvcrt

IP = "192.168.1.154"
port = 123


sockets = []


def write():
    pass


def main():
    sock = socket.socket()
    sock.connect((IP, port))
    while True:
        rlist, wlist, xlist = select.select(sockets, sockets, [])
        for current_socket in rlist:
            if current_socket is sock:
                data = current_socket.recv(1024)
                print(data)
            else:
                sockets.append(current_socket)
        write()


if __name__ == '__main__':
    main()

导入软件包dat <- data.frame(state = c("AL", "AK", "AZ", "AR"), Loan = c(25310770, 45310770, 35310770, 25682770)) usmap,然后使用更完整的数据填充整个地图,但是使用提供的示例,我们可以看到

ggplot2

请注意,由于提供的数据样本很少,某些状态为灰色。

the map