【问题标题】:How to graph two variables onto a US county map in R?如何在 R 中将两个变量绘制到美国县地图上?
【发布时间】:2023-03-17 05:43:02
【问题描述】:

df1:

score1   score2 FIPS
0.1230    4     1001
0.4503    1     2001
0.5642    0     4001
0.2011    3     5001
0.2012    2     6001
0.1093    0     8001
0.3422    10    1005
0.1033    3     17001
0.2432    30    19001
0.1002    7     1017

我想根据score1 的权重对美国的每个县进行着色,然后我想根据score2 在县周围制作半透明圆圈。我该怎么做?

输出:

【问题讨论】:

  • 我在想 par 中的 add = TRUE 但我认为 @johan-rosa 有你的答案。

标签: r dataframe graph maps fips


【解决方案1】:

我做了我认为你想要的如下。

  1. 我从 this url 获得了美国 shapefile。
  2. 您需要sfdplyrggplot2 包。

由于您没有提供可重现的数据示例,我不得不模拟 score1score2 变量。

# packages --------------------------------------
library(sf)
library(dplyr)
library(ggplot2)

# Import shape files
# (You have to unpack the zip with the shape files)
usa_map <- read_sf("USA_States.shx")

# Adding the variables you need. 
# In your case you can left_join your variables 

usa_map <- usa_map %>% 
    mutate(
        score1 = sample(1:1000, 51),
        score2 = sample(1:500, 51)
    ) %>% 
    filter(!STATE_NAME %in% c("Alaska", "Hawaii"))

# Use ggplot2 to make the plot
usa_map %>% 
    ggplot() +
    geom_sf(aes(fill = score1)) +
    geom_sf(data = st_centroid(usa_map), aes(size = score2), alpha = 0.4, color = "red") +
    guides(size = FALSE) +
    theme_void()

【讨论】:

    猜你喜欢
    • 2020-07-12
    • 1970-01-01
    • 2011-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多