I am working with some public datasets, which can be accessed here inside the 'stack_question' folder).
I am trying to create a map of all the supermarkets within the state of Kansas using the tmap
library in R
. Using ggplot
, I generated a map using the following code:
library(pacman)
p_load(sf, tidyverse, tmap)
#Read in data
super <- readRDS('supermarket_pt.RDS')
ct<-st_read("US_tract_2010.shp")
food <- read_csv('FoodAtlas.csv')
#Reproject so datasets are projeted in the same way
super <- st_transform(super, st_crs(ct))
#Change data structure to 'numeric'
ct$GEOID10 <- as.numeric(ct$GEOID10)
food$CensusTract <- as.numeric(food$CensusTract)
#Selct for relevant columns to combine the dataset
food <- food %>%
select(CensusTract:County)
#Left join ct and modified food dataset
sup <- left_join(ct, food, by = c('GEOID10' = 'CensusTract'))
#Filter dataset to desired state
ks <- sup %>%
filter(State == "Kansas")
#Find the supermarkets in Kansas
super_ks <- super[ks, op = st_within]
#Plot the data
ggplot() +
geom_sf(data = ks) +
geom_sf(data = super_ks, color = 'steelblue', shape = 18, size = 2)
The following image is output, which is correct:
I need to make the same image with tmap
. The problem is that I am getting an error when I run the following code:
#Set tmap mode
tmap_mode('plot')
#Spatially join data
super_state <- st_join(ks, super_ks, join = st_contains, left = TRUE)
na.omit(super_state)
#Generate KS map
qtm(super_state)+
tm_bubbles('name', col = 'steelblue')
The error:
Error: size argument of tm_symbols/tm_dots is not a numeric variable
When I try this code, I get an image that produces what I don't want:
tm_shape(super_state) +
tm_polygons('name')
How can I make my tmap
map look like the first one?