I'm quite new to R and am trying to create a map of my country with points overlayed on it (representing cities, with points sized by population). I've already created the map with geom_sf() using lat, long boundaries, and have stored it as map.
This is an example of the code I'm trying to replicate, where cities is a df with columns state, pop, lat and long (with no missing values):
map <- map +
geom_point(aes(x = cities$long[1], y = cities$lat[1], size = cities$pop[1])) +
geom_point(aes(x = cities$long[2], y = cities$lat[2], size = cities$pop[2])) +
geom_point(aes(x = cities$long[3], y = cities$lat[3], size = cities$pop[3])) +
geom_point(aes(x = cities$long[4], y = cities$lat[4], size = cities$pop[4])) +
geom_point(aes(x = cities$long[5], y = cities$lat[5], size = cities$pop[5]))
This is the loop that I thought would work, but it seems to be overwriting the geom_point with each iteration as my map just ends up with the last point. When I output a map with each iteration, it just shows the most recent point.
or(i in 1:nrow(cities)){
if(!is.na(cities$lat[i]) && !is.na(cities$long[i])){
map <- map +
geom_point(aes(x = cities$long[i], y = cities$lat[i], size = cities$pop[i]))
}
}
I hope that I've provided enough info - please let me know if I haven't. Thank you!