Hi there,
I am trying to graphically represent where our survey respondents came from according to their postcode. However, I get the following error code, which I can't figure out how to fix:
Error in `check_required_aesthetics()`:
! stat_sf requires the following missing aesthetics: geometry
I tried to use reprex but get the following error:
ℹ Rendering reprex...
Error in parse(text = x, keep.source = TRUE) :
<text>:5:7: unexpected 'in'
4:
5: Error in
^
Here is my code:
#load spatial packages
library(plyr)
library(dplyr)
library(ggplot2)
library(rgdal)
library(tmap)
library(ggmap)
library(dplyr)
library(sf)
library(ggspatial)
library(rlang)
library(broom)
library(tidyverse)
library(readxl)
library(raustats)
library(purrr)
library("Census2016")
library(reprex)
# Import shapefiles -------------------------------------------------------
#Select the location where the ASGS files are saved
poa <- read_csv("australian_postcodes.csv")
#Check the data imported correctly
head(poa)
#filter for Queensland only
qld_pc <- poa %>%
filter(state == "QLD")
#Convert column postcode to interger
qld_pc$postcode <- as.integer(qld_pc$postcode)
#Check the postcode column is interger
head(qld_pc)
# Get pc data from survey results -----------------------------------------
data <- read.csv("reprex_survey_data.csv")
head(data,n=20)
#Select the key demographic columns (first 8 columns)
data_pc <- data[,2:5]
head(data_pc,n=20)
# Join the survey data to the shapefile -----------------------------------
#Join the two sf by their common field.
QLD_resp_pc <- inner_join(qld_pc,data_pc,
by = c("postcode" = "D3..What_is_the_postcode_where_you_live_.i.e._home_postcode.."))
qld_geometry <- qld_pc %>%
st_as_sf(coords = c("long", "lat"), crs = 4326)
#check the format
head(QLD_resp_pc)
#Plot a map that uses census data
ggplot() +
geom_sf(data=qld_geometry, aes(geometry = geometry))+
geom_sf(data = QLD_resp_pc, aes(fill = postcode)) +
ggtitle("Respondents' distribution by postcode") +
xlab("long") +
ylab("lat") +
theme_bw() +
theme(legend.position = "right",
legend.title = element_text("Postcode"))
reprex()
I added the qld_pc$postcode <- as.integer(qld_pc$postcode)
because otherwise I get the error:
Error in `inner_join()`:
! Can't join on `x$postcode` x `y$postcode` because of incompatible types.
ℹ `x$postcode` is of type <character>>.
ℹ `y$postcode` is of type <integer>>.
I think the above conversion may be the cause of my troubles based on geom_sf() "missing aesthetics: geometry" with tibble but not data frame · Issue #3391 · tidyverse/ggplot2 · GitHub but without the conversion I cannot join the two datasets...or can I?