【问题标题】:Display values corresponding to the USA states over the state name在州名上显示与美国州相对应的值
【发布时间】:2020-08-20 19:56:42
【问题描述】:

我试图使用包“choroplethr”并使用简单的 df2 数据集(它具有相同的区域和值列)创建美国各州地图,并且我使用了包文档中提供的代码。

require (choroplethr)

data("df_pop_state")

df2 <- read.csv("ShareDF-chro.csv", header=TRUE, stringsAsFactors=FALSE)



# here is the data ShareDF-chro

region = c("alabama", "alaska", "arizona", "arkansas", 
"california", "colorado", "connecticut", "delaware", "district of columbia", 
"florida", "georgia", "hawaii", "idaho", "illinois", "indiana", 
"iowa", "kansas", "kentucky", "louisiana", "maine", "maryland", 
"massachusetts", "michigan", "minnesota", "mississippi", "missouri", 
"montana", "nebraska", "nevada", "new hampshire", "new jersey", 
"new mexico", "new york", "north carolina", "north dakota", "ohio", 
"oklahoma", "oregon", "pennsylvania", "rhode island", "south carolina", 
"south dakota", "tennessee", "texas", "utah", "vermont", "virginia", 
"washington", "west virginia", "wisconsin", "wyoming"), 

value = c(1.15, 0.11, 6.21, 2.41, 8.42, 13.57, 3.57, 4.55, 7.08, 9.42, 5.21, 
0.108, 9.09, 2.56, 4.51, 9.65, 6.76, 3.54, 0.17, 1.99, 6.66, 
3.88, 7.31, 4.86, 4.85, 2.39, 0.25, 0.05, 0.21, 0.11, 3.86, 0.05, 
7.31, 1.91, 0.41, 4.55, 0.002, 2.65, 3.14, 0.71, 1.94, 0.13, 
2.2, 12.65, 0.05, 0.074, 5.79, 7.5, 0.12, 2.6, 0.33)

df_pop_state$value <- df2$value

state_choropleth(df_pop_state,title = "US State's X-Capital share data",num_colors = 2,legend = "Capital Share")

我的问题是:如何在地图中插入相应的 X-capital 份额值以及州的首字母缩略词(同时希望保持首字母缩略词的字体更小)。谢谢,感谢您的帮助。

【问题讨论】:

  • 请编辑您的代码以共享实际数据。您可以通过在 R 中键入 dput(df_pop_state) 然后复制该代码来执行此操作。这将允许人们运行和修改您的实际代码,并查看输出。如果您的数据无法共享,请修改您的代码以使用公共数据。
  • 您在这篇文章中提出了三个不同的问题(插入相应的值,控制字体大小,以及专门关于夏威夷的问题)。这是一个问答网站。请注意,question 是单数,而不是复数。

标签: r ggplot2 maps choropleth choroplethr


【解决方案1】:

这里只有ggplot2 的解决方案。

  • 使用usmap::us_map 获取多边形数据。 (和你一样)
  • Left join 与您的共享数据(首先大写您的区域名称)
  • Create centroids for the text annotation
  • 最好将这些质心和份额放​​入单独的数据框中
  • geom_polygon绘制多边形
  • 使用粘贴在geom_text 中绘制您的标签(州缩写和共享)。(您也可以使用annotate
  • 将数据分别传递给每一层。 (空的 ggplot 主调用)

优点是使用 ggplot 语法使得颜色/填充美学的控制非常容易,您还可以非常轻松地自定义线条粗细和文本大小。

至于州的缩写,我只用了第一个字母——这可能不是官方的缩写。肯定有一些向量可以轻松转换。

library(usmap)
library(tidyverse)

us <- usmap::us_map()

region <- str_to_title(region)

share_df <- data.frame(region, share)

us_val <- 
  left_join(us, share_df, by = c("full" ="region")) 
#> Warning: Column `full`/`region` joining character vector and factor, coercing
#> into character vector

us_centroids <- 
  us_val %>%
  group_by(full) %>% 
  summarise(centroid.x = mean(range(x)), 
            centroid.y = mean(range(y)),
            label = unique(toupper(str_sub(full,1,2))),
            share = unique(share))

ggplot() + 
  geom_polygon(data = us_val, 
               aes(x,y, group = group, fill = share > 3), 
               color = "black",
               size = .1) +
  geom_text(data = us_centroids, 
            aes(centroid.x, centroid.y, label = paste(label, "\n", share)),
            size = 5/14*8) +
  scale_fill_brewer(name = "State Share", 
                    palette = "Blues", 
                    labels = c(`TRUE`="More than 3",`FALSE`="Less than 3")) +
  theme_void()

reprex package (v0.3.0) 于 2020-05-06 创建

更新 话虽如此,但请查看 ?datasets::state。它包含这些缩写 (state.abb) 和州名 (state.name)。它还包含质心 (state.center) 上的数据。所以,很多数据已经内置了:)

数据

region =  c("alabama", "alaska", "arizona", "arkansas", 
           "california", "colorado", "connecticut", "delaware", "district of columbia", 
           "florida", "georgia", "hawaii", "idaho", "illinois", "indiana", 
           "iowa", "kansas", "kentucky", "louisiana", "maine", "maryland", 
           "massachusetts", "michigan", "minnesota", "mississippi", "missouri", 
           "montana", "nebraska", "nevada", "new hampshire", "new jersey", 
           "new mexico", "new york", "north carolina", "north dakota", "ohio", 
           "oklahoma", "oregon", "pennsylvania", "rhode island", "south carolina", 
           "south dakota", "tennessee", "texas", "utah", "vermont", "virginia", 
           "washington", "west virginia", "wisconsin", "wyoming")

share = c(1.15, 0.11, 6.21, 2.41, 8.42, 13.57, 3.57, 4.55, 7.08, 9.42, 5.21, 
          0.108, 9.09, 2.56, 4.51, 9.65, 6.76, 3.54, 0.17, 1.99, 6.66, 
          3.88, 7.31, 4.86, 4.85, 2.39, 0.25, 0.05, 0.21, 0.11, 3.86, 0.05, 
          7.31, 1.91, 0.41, 4.55, 0.002, 2.65, 3.14, 0.71, 1.94, 0.13, 
          2.2, 12.65, 0.05, 0.074, 5.79, 7.5, 0.12, 2.6, 0.33)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多