The code you provided doesn't seem to reproduce - the tanzania
object you refer to in the first line of the ggplot call is not exported from any of the packages you import.
Which is a problem that can be helped, but please give us an idea what kind of areas you want to color code; specifically: do you need to color code polygons of some admin units (I am unsure how these are called in Tanzania - states? districts?)
In the following code I will assume you want to color code the level 1 admin areas - Tanzania has about 30 of these, so it will feel about OK for a map.
The technique that the code uses is spatial join - combining polygons as admin areas and cities as points based on intersection (= which point is in which polygon). This will give admin area polygons the atributes of cities - such as city_name. The function to perform a spatial join is sf::st_join()
.
In order for the spatial join to work both the admin area polygons have to be in {sf} package format and in compatible Coordinate Reference System; which is where the sf::st_as_sf()
comes from.
I have also taken the liberty of removing code not directly related to joining the cities to admin areas + plotting the result for the sake of clarity and concise code.
library(sf) #'simple features' package
library(ggplot2) # general purpose plotting
library(rnaturalearth) # map data
city_data <- data.frame(city_name=c("Dar es Salaam", "Mwanza", "Arusha", "Dodoma", "Mbeya"))
city_data$lat <- c(-6.7924,-2.5164,-3.3869,-6.1630,-8.9094)
city_data$lon <- c(39.2083,32.9175,36.6830,35.7516,33.4608)
# convert city_data from regular data frame to sf data frame
city_data_sf <- st_as_sf(city_data, crs = 4326, coords = c("lon", "lat"))
# declare tanzania object as admin areas of Tanzania
tanzania <- rnaturalearth::ne_states(iso_a2 = 'TZ', returnclass = 'sf')
# spatially join tanzania admin areas to cities as sf object
tanzania_joined <- st_join(tanzania, city_data_sf)
ggplot() +
geom_sf(data = tanzania_joined, aes(fill = city_name)) +
xlab("Longitude") + ylab("Latitude") +
ggtitle("Major Cities in the United Republic of Tanzania")