1

I am trying to creat a ggplot for the population by county for Wisconsin

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)

But am getting this error: Error: Aesthetics must be either length 1 or the same as the data (72): fill

Data Here https://www.dropbox.com/sh/8zjwt55yg4x1o2h/AADBDuTcIhym9tlrx9JkfVhoa?dl=0

2 Answers2

1

It should work if you convert your population data into a numeric column and join it to your shapefile. This code worked for me:

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)
jusc15
  • 23
  • 5
  • I get this error #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) Error in `$<-.data.frame`(`*tmp*`, COUNTY_NAM, value = character(0)) : replacement has 0 rows, data has 71 – Jacob Benzaquen Jul 29 '20 at 20:34
0

There are several issues with your code:

  1. You map a dataframe (population) on fill
  2. As your data has no header you have to set col_names = FALSE in read_csv
  3. You have to join the shapefile and your dataframe with the population data. Otherweise it is not guarenteed that you the population sizes are mapped to the right county.
  4. Even when solving these problems plot_gg throws an error Error in if (whichtype %in% c("text", "line")) { : argument is of length zero related to theme_bw. For a solution to this last issue see here.

To solve the first three issues use col_names = FALSE or name the cols as I do in my code below. Second. Get rid of the added County in the county names. Third. Join the shapefile and the population data by county. Fourth, map the population size on fill. Try this:

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)

enter image description here

stefan
  • 90,330
  • 6
  • 25
  • 51
  • @JacobBenzaquen Hm. When I read the population data on my machine the population size is read as a numeric. However, from the error message I would guess that the population sizes are not numeric. Check the datatypes e.g. using `str(population)`. If pop is of type `chr` you have to convert it to a numeric. – stefan Jul 29 '20 at 22:08