R: Ggplot2 Error: Aesthetics must be either length 1 or the same as the data (72): fill

R: Ggplot2 Error: Aesthetics must be either length 1 or the same as the data (72): fill

我正在尝试为威斯康星州各县的人口创建一个 ggplot

library(broom)
library(sf)
library(ggplot2)
library(rayshader)
library(viridis)
library(tidyverse)

#Population CSV
population = read_csv("co-est2019-annres-55.csv")

#Import Shapefile
Wi_counties <- st_read(
  "County_Boundaries_24K.shp")

#Plot Shapefile
ggplot() + 
  geom_sf(data = Wi_counties, size = 1, color = "black", fill = "cyan1") + 
  ggtitle("WI_Counties") + 
  coord_sf()

gg_wi = ggplot(Wi_counties) +
  geom_sf(aes(fill = population)) +
  scale_fill_viridis("population") +
  ggtitle("Population by County in Wisconsin") +
  theme_bw()

plot_gg(gg_wi, multicore = TRUE, width = 6 ,height=2.7, fov = 70)

但是我收到这个错误: 错误:美学必须是长度为1或与数据相同(72):fill

这里有数据 https://www.dropbox.com/sh/8zjwt55yg4x1o2h/AADBDuTcIhym9tlrx9JkfVhoa?dl=0

如果您将人口数据转换为数字列并将其连接到您的 shapefile,它应该可以工作。 此代码对我有用:

library(sf)
library(tidyverse)
library(rayshader)
library(viridis)

#Population CSV
population <- read.csv("Wisconsin_Population/co-est2019-annres-55.csv")

#new column COUNTY_NAM matching the county names from shapefile
population$COUNTY_NAM <- substr(as.character(population$V1), 1, nchar(as.character(population$V1))-7)

#convert V2 into numeric column
population$V2 <- as.numeric(population$V2)

#Import Shapefile
Wi_counties <- st_read("Wisconsin_Population/County_Boundaries_24K.shp")

#perform left_join with population data
Wi_counties <- Wi_counties %>% left_join(population, by = c("COUNTY_NAM"))

#Plot Shapefile
ggplot() + 
  geom_sf(data = Wi_counties, size = 1, color = "black", fill = "cyan1") + 
  ggtitle("WI_Counties") + 
  coord_sf()

gg_wi <- ggplot(Wi_counties) +
  geom_sf(aes(fill = V2)) +
  scale_fill_viridis() +
  ggtitle("Population by County in Wisconsin") +
  theme_bw()

plot_gg(gg_wi, multicore = TRUE, width = 6, height=2.7, fov = 70)

您的代码存在几个问题:

  1. 您将数据框 (population) 映射到 fill
  2. 因为你的数据没有header你必须在read_csv
  3. 中设置col_names = FALSE
  4. 您必须将 shapefile 和您的数据框与 population 数据连接起来。否则不能保证您将人口规模映射到正确的县。
  5. 即使在解决这些问题时 plot_gg 也会抛出与 theme_bw 相关的错误 Error in if (whichtype %in% c("text", "line")) { : argument is of length zero。有关最后一个问题的解决方案,请参阅 here.

要解决前三个问题,请使用 col_names = FALSE 或像我在下面的代码中那样命名列。第二。删除县名中添加的 County。第三。按县加入 shapefile 和人口数据。第四,将人口规模映射到 fill。试试这个:

library(sf)
library(ggplot2)
library(viridis)
library(readr)
library(rayshader)
library(dplyr)

#Population CSV
population = read_csv(here::here("Wisconsin/co-est2019-annres-55.csv"), col_names = c("county", "pop")) %>% 
  mutate(county = gsub("\sCounty", "", county))
  
#Import Shapefile
Wi_counties <- st_read(here::here("Wisconsin/County_Boundaries_24K.shp"))

Wi_counties_join <- Wi_counties %>% 
  left_join(population, by = c("COUNTY_NAM" = "county"))

#Plot Shapefile
gg_wi = ggplot(Wi_counties_join) +
  geom_sf(aes(fill = pop)) +
  scale_fill_viridis("population") +
  ggtitle("Population by County in Wisconsin") +
  theme_bw()
gg_wi

#plot_gg(gg_wi, multicore = TRUE, width = 6 ,height=2.7, fov = 70)