Missing Aesthetics Geometry

I am looking to create a map using the following dataset:

with the shapefile of:

Here is my R code:

library("tidyverse")
library("dplyr")
library("sf")
library("ggplot2")
library("tmap")
library("tmaptools")
library("RColorBrewer")


data = readr::read_csv("C:/Users/amanm/Desktop/annualincomedata2.csv")


localauthorities2 <- read_sf("C:/Users/amanm/Desktop/localauthorities2.dbf")

mymap <- merge(data,localauthorities2)

ggplot(mymap) + geom_sf(aes(fill = Total)) + scale_fill_viridis_c()

Upon running the code I am given an error code:

> ggplot(mymap) + geom_sf(aes(fill = Total)) + scale_fill_viridis_c()
Error in `check_required_aesthetics()`:
! stat_sf requires the following missing aesthetics: geometry
Run `rlang::last_error()` to see where the error occurred.

Any advice would be helpful. Thanks

1

2 Answers

You should merge the data to the localauthorities2, not the other way around. This way, you add the data.frame object (data) to an sf object (localauthorities2) as attributes, thus, the mymap object is still an sf object with geometry.

mymap <- merge(localauthorities2, data)
1

You need to bring your spatial data in with

localauthorities2 <- st_read("C:/Users/amanm/Desktop/localauthorities2.shp")


3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Robert Thorne

Robert Thorne

Automotive & Future Transportation Editor

Robert Thorne covers electric vehicle innovations, autonomous driving systems, global mobility trends, and automotive engineering developments.

Share this article
Twitter Facebook Pinterest