I am looking to create a map using the following dataset:
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
CodePudding user response:
You need to bring your spatial data in with
localauthorities2 <- st_read("C:/Users/amanm/Desktop/localauthorities2.shp")
CodePudding user response:
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)