简体   繁体   中英

How can I add names of cities and centroids in maps in R with maptools, ggplot or other packages?

I am trying to incorporate labels of provinces, and cities with labels and dots into a South African map.I can get a map with provincial subdivisions from a spdf file, from website https://gadm.org/download_country_v3.html

SA1spdf <- readRDS("gadm36_ZAF_1_sp.rds")

And I am trying to use the information in the slot:

SA1spdf@data@ADM1_EN

that contains the label of the 9 provinces I want to place the labels in the center of the polygons, if possible, so I got the centroids of the 9 provinces:

centroids_SAdf <- as.data.frame(coordinates(SA1spdf))    

and change names V1 and V2 to Longitude and Latitude

names(centroids_SAdf) <- c("Longitude", "Latitude")

Also I want to add a few cities to the plot:

coordinates_cities <- data.frame(city = c("Cape Town", "Durban", "Johannesburg", "Port Elizabeth"),lat = c(-33.930, -29.870, -26.190, -33.960 ), lon = c(18.460, 30.990, 28.040,25.590 )        

but I cannot link all these together.

I can get a map with:

qtm(SA1spdf, fill = "white")       

Using maptools and the following codes all return error messages

qtm(SA1spdf, fill = "white") + tm_text("SA1spdf$data#ADMN1_EN")    

tm_shape(SA1spdf) + tm_polygons() + tm_fill(col = "white")    

I dont get a white filled map, its filled with grey colour

the following codes return error messages:

tm_shape(SA1spdf) + tm_polygons() + tm_text(c("Eastern Cape","Free State" , "Gauteng","KwaZulu-Natal", "Limpopo","Mpumalanga","North West","Nothern Cape","Western Cape"))    

ggplot(SA1spdf, aes(x = lon, y = lat), group = group) + geom_polygon(fill = "white") + geom_point(data = coordinates_cities, aes(x = lat, y = lon)) + geom_text(data= coordinate_cities, aes(label = city))    
library(sp)
library(ggplot2)
library(tmap)
library(rgeos)

I'll use the same object SA1spdf you created with data from ZAF as sp object from GADM

coordinate_cities <- data.frame(
  city = c("Cape Town", "Durban", "Johannesburg", "Port Elizabeth"),
  lat = c(-33.930, -29.870, -26.190, -33.960),
  long = c(18.460, 30.990, 28.040,25.590))        

base plot

plot(SA1spdf)
points(coordinate_cities$lon, coordinate_cities$lat, pch = 16, col = "red", cex = 1)
text(coordinate_cities$lon, coordinate_cities$lat, coordinate_cities$city, adj = c(0, 0), cex = 0.7)
title(main = "South Africa", sub = "Main cities")

ggplot2

sa_df <- fortify(SA1spdf)
#> Regions defined for each Polygons

ggplot(sa_df, aes(long, lat)) +
geom_polygon(aes(group = group), fill = "white", colour = "black") +
geom_point(data = coordinate_cities, aes(x = long, y = lat), colour = "red") +
geom_text(data = coordinate_cities, aes(label = city), vjust = -1) +
coord_equal()

tmap

# convert coordinate_cities to sp object
sa_cities_spdf <- SpatialPointsDataFrame(coords = coordinate_cities[, c("long", "lat")],
  data = coordinate_cities[, "city", drop = FALSE],  
  proj4string = CRS("+proj=longlat +datum=WGS84"))

tm_shape(SA1spdf) +
  tm_borders() +
  tm_layout(main.title = "South Africa regions") +
  tm_text("NAME_1", size = 0.7) +
  tm_shape(sa_cities_spdf) + tm_symbols(size = 0.5, col = "red")

Created on 2021-06-17 by the reprex package (v0.3.0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM