8wDlpd.png
8wDFp9.png
8wDEOx.png
8wDMfH.png
8wDKte.png

在 R 中创建一个州地图来显示各州的调查回复数量,我使用的是 tidyverse,我的地图确实可以显示,但形状很奇怪

lobati 2月前

39 0

我正在使用以下代码,但我没有获得变量“Group”或“Group2”的各州密度图,而是获得了包含各种奇怪角度和三角形的美国地图......

我正在使用以下代码,但我没有获得变量“Group”或“Group2”的各州密度图,而是获得了美国地图,其中嵌入了各种跨越州界的奇怪角度和三角形。

library(tidyverse)
theme_set(theme_bw(base_size = 16))

install.packages("foreign")
library("foreign")

data1 <- read.spss("C:/afp/density2.sav", to.data.frame = TRUE)

head(data1)
state_initial                    states Group Group2 Sum2023
1            AL alabama                     2.1      1       3
2            AK alaska                      0.0      0       0
3            AZ arizona                     1.0      3       6
4            AR arkansas                    4.0      3       2
5            CA california                  1.0     20      15
6            CO colorado                    6.0      4       5

us_states <- map_data("state")

us_states %>%
  left_join(data1, by = c("region" = "states")) %>%
  ggplot(aes(x = long, y = lat, group = Group, fill = Group)) +
  geom_polygon(color = "gray90", size = 0.1) +
  coord_map(projection = "albers", lat0 = 45, lat1 = 55) +
  scale_fill_continuous(type = "gradient") +
  theme(
    legend.position = "right",
    axis.line = element_blank(),
    axis.text = element_blank(),
    axis.ticks = element_blank(),
    axis.title = element_blank(),
    panel.background = element_blank(),
    panel.border = element_blank(),
    panel.grid = element_blank()
  )

enter image description here

帖子版权声明 1、本帖标题:在 R 中创建一个州地图来显示各州的调查回复数量,我使用的是 tidyverse,我的地图确实可以显示,但形状很奇怪
    本站网址:http://xjnalaquan.com/
2、本网站的资源部分来源于网络,如有侵权,请联系站长进行删除处理。
3、会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。
4、本站一律禁止以任何方式发布或转载任何违法的相关信息,访客发现请向站长举报
5、站长邮箱:yeweds@126.com 除非注明,本帖由lobati在本站《r》版块原创发布, 转载请注明出处!
最新回复 (0)
  • 正如我在评论中提到的,问题在于您将 Group 数据集中的列 group aes 上,而不是 group 数据映射中的列:

    state_initial <- c("AL", "AK", "AZ", "AR", "CA", "CO")
    states <- c("alabama", "alaska", "arizona", "arkansas", "california", "colorado")
    Group <- c(2.1, 0.0, 1.0, 4.0, 1.0, 6.0)
    Group2 <- c(1, 0, 3, 3, 20, 4)
    Sum2023 <- c(3, 0, 6, 2, 15, 5)
    
    data1 <- data.frame(state_initial, states, Group, Group2, Sum2023)
    
    library(tidyverse)
    
    us_states <- map_data("state")
    
    us_states %>%
      left_join(data1, by = c("region" = "states")) %>%
      ggplot(aes(x = long, y = lat, group = group, fill = Group)) +
      geom_polygon(color = "gray90", size = 0.1) +
      coord_map(projection = "albers", lat0 = 45, lat1 = 55) +
      scale_fill_continuous(type = "gradient") +
      theme(
        legend.position = "right",
        axis.line = element_blank(),
        axis.text = element_blank(),
        axis.ticks = element_blank(),
        axis.title = element_blank(),
        panel.background = element_blank(),
        panel.border = element_blank(),
        panel.grid = element_blank()
      )
    #> Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
    #> ℹ Please use `linewidth` instead.
    #> This warning is displayed once every 8 hours.
    #> Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
    #> generated.
    

返回
作者最近主题: