【问题标题】:how to write state abbreviations only on shaded states in R如何仅在 R 中的阴影状态上编写状态缩写
【发布时间】:2021-09-01 22:07:57
【问题描述】:

下面是我的代码的一部分,它给出了情节:(阴影状态是紫色的)

 dt <- statepop %>%
    dplyr::mutate(selected = factor(ifelse(full %in% stringr::str_pad(c(s.cls.list[[i]]$State), 5, pad = "0"), "1", "0")))
  
  s.plot <- usmap::plot_usmap(data = dt, values = "selected", color = "grey") +
    ggplot2::scale_fill_manual(values = c("#E5E4E2", "purple"),name = length(c(s.cls.list[[i]]$State)))+
    labs(title = paste("component",i, sep = " : "))+
    theme(plot.title = element_text(color = "purple", size = 14, face = "bold",hjust = 0.5))+
    

有两个我想要的修改,但我不知道怎么做:
1- 如何在中心写阴影状态缩写,字体颜色为黄色。
2- 我怎样才能拥有这样的情节标题:组件:黑色,11 为紫色,如阴影状态。

【问题讨论】:

  • (1) “阴影状态”是您涂成“灰色”的状态吗? (2) 您的问题标题与您的问题有何关联?
  • @MartinGal,阴影状态是紫色状态。虽然我认为这很清楚,但感谢您的注意,我编辑了我的问题。希望现在更清楚了。

标签: r ggplot2 plot


【解决方案1】:
  1. 获取所选州的经度和纬度。我使用了rnaturalearth,但您可以有其他选择。
  2. 使用usmap_transform 将这些值转换为与您的地图一致。
  3. 使用 `geom_text' 添加标签,
  4. 使用annotate 替换title。选择这些值需要一些尝试和错误,因为我没有使用 usmap 包的经验。
library(usmap)
library(tidyverse)
library(rnaturalearth)
select_states <- c("Nebraska", "New Mexico", "Oregon")
dt <- statepop %>%
  dplyr::mutate(selected = ifelse(full %in% select_states, "1", "0"))
s.plot <- usmap::plot_usmap(data = dt, values = "selected", color = "grey") +
  ggplot2::scale_fill_manual(values = c("#E5E4E2", "purple"), 
                             #name = length(c(s.cls.list[[i]]$State))
  )+
  labs(title = paste("component","?", sep = " : "))+
  theme(plot.title = element_text(color = "purple", size = 14, face = "bold",hjust = 0.5))

my_df <-rnaturalearth::ne_states(country = "united states of america") %>% as.data.frame() %>% 
  filter(name %in% select_states) %>% transmute(lon = longitude, lat = latitude, state = name, abbr = postal)%>% 
  usmap_transform()

s.plot +
  geom_text(data = my_df, aes(x = lon.1, y = lat.1, label = abbr)) + 
  labs(title = "") +
  annotate("label", x = 26203.63, y = 874416.52, color = "black", fill = "purple", 
           label = paste("component","?", sep = " : ")) 

reprex package (v2.0.1) 于 2021-09-02 创建

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-16
    • 1970-01-01
    • 1970-01-01
    • 2016-04-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多