根据缩写的州名在美国地图中绘制数值 [英] Plot a numerical values in United states map based on Abbreviated state names

查看:241
本文介绍了根据缩写的州名在美国地图中绘制数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下所有状态的数据.在此处提供示例

I have a data as below that has all the states. Providing a sample here,

state_name value_x
AL 250
AK 900
.
.

MT 1000
NJ 10000
.
WY 500

现在我需要根据状态缩写在美国地图上绘制这些数字(值_x).

Now I need to plot these numbers(value_x) in US map according to state abbreviations.

我尝试在线阅读有关此软件包fiftystater的信息.但是我在绘制这个图时没有任何进展.

I tried reading about this package fiftystater online. But i am not getting anywhere in plotting this.

我也阅读了这个stackoverflow帖子.但是我不知道如何应用于我的特定数据集.

I read this stackoverflow post too. But i don't know how to apply to to my specific dataset.

请帮助!我想像这样可视化我的图.我从stackoverflow帖子中获取了这张图片.

Please help! I want to visualize my graph something like this. I took this image from the stackoverflow post.

推荐答案

您的问题与链接的问题几乎相同,除了您的数据不存在小写状态名称.

Your question is almost identical to the linked question except for the fact that your data does not have the lowercase state names already present.

library(tidyverse)
library(fiftystater)
library(viridis)
#> Loading required package: viridisLite

使用内置数据集构建样本数据集.您无需执行此部分,只需调用您的值以绘制tbl

Construct a sample dataset using built in datasets. You do not need to do this part, just call your values to plot tbl

tbl <- state.x77 %>%
  as_tibble(rownames = "state") %>%
  bind_cols(state_name = state.abb) %>%
  rename(value_x = Income) %>%
  select(state_name, value_x)

创建一个将状态名称与状态缩写相关联的表:

Create a table that relates state names to state abbreviations:

state_abbs <- tibble(state = str_to_lower(state.name), abb = state.abb)

使用left_join将完整的状态名称连接到仅包含缩写的数据表中:

Use left_join to join the full state names to the data table that only has abbreviations:

tbl_m <- left_join(tbl, state_abbs, by = c("state_name" = "abb")) %>%
  rename(id = state)

然后基本上从链接的问题的答案中的fiftystater复制示例..这里的第一个关键部分是geom_map,它需要您使用选项map = fifty_states,其中fifty_statesfiftystater的数据表,并且还要提供aes(map_id = id),以便它知道哪一组点对应于哪一组点数据中的值.第二个关键部分是geom_text,它将在您指定的坐标处添加所需的文本.在这里,我们创建一个数据框,使坐标来自fifty_states的每个状态的经纬度或经度的平均值,然后将其与tbl_m中的数据连接.其余的只是格式化!

And then basically copy the example from fiftystater or from the answer to the linked question. Here the first key part is geom_map, which needs you to use option map = fifty_states where fifty_states is the data table from fiftystater, and also to supply aes(map_id = id) so that it knows which group of points corresponds to which values in the data. The second key part is geom_text, which adds the text you want at coordinates you specify. Here, we create a dataframe that makes the coordinates the mean of the lat or long for each state from fifty_states, and then join it to the data in tbl_m. The rest is just formatting!

ggplot(tbl_m) +
  geom_map(map = fifty_states, aes(map_id = id, fill = value_x)) + 
  expand_limits(x = fifty_states$long, y = fifty_states$lat) +
  coord_map() +
  geom_text(
    data = fifty_states %>%
      group_by(id) %>%
      summarise(lat = mean(c(max(lat), min(lat))),
                long = mean(c(max(long), min(long)))) %>%
      mutate(state = id) %>%
      left_join(tbl_m, by = c("state" = "id")),
    aes(x = long, y = lat, label = value_x )
    ) +
  scale_fill_viridis() +
  scale_x_continuous(breaks = NULL) +
  scale_y_continuous(breaks = NULL) +
  labs(x = "", y = "") + theme(legend.position = "bottom", 
                               panel.background = element_blank())
#> Warning: Removed 1 rows containing missing values (geom_text).

reprex软件包(v0.2.0)创建于2018年2月16日.

Created on 2018-02-16 by the reprex package (v0.2.0).

这篇关于根据缩写的州名在美国地图中绘制数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆