R language draws administrative boundary map

To draw the administrative map of Henan Province in R language, libraries such as `rgdal` and `rgeos` can be used to read and process geographic data. In this example, we will use the `maptools` library to read layered map boundary data (shapefiles) and visualize these graphs using the `ggplot2` library.

The following is a code for drawing the administrative map of Henan Province:


library(maptools)
library(ggplot2)

# Read the shapefile
hena_shapefile <- readShapePoly("path/to/henan.shp") 

# draw map
ggplot(data = fortify(hena_shapefile), aes(x = long, y = lat, group = group)) +
  geom_polygon(fill = "white", color = "grey40") + # draw area outline
  coord_fixed() + # Fixed axis scale
  labs(title = "Henan Provincial Administrative Map") + # Set the graphic title
  theme_void() # Remove redundant elements in the default theme
 

In this code, the shapefile is first read using the `readShapePoly()` function and stored in a variable. Make sure to replace "path/to/henan.shp" with the path where the Henan shapefile is actually saved.

Then, use the `fortify()` function to convert the data into a format suitable for `ggplot2` plotting, and use the `geom_polygon()` function to draw all boundaries with the fill color set to white and the border color to gray. The `coord_fixed()` function can be used to fix the axis scale to make the map more intuitive in terms of curvature.

Also use the `labs()` function to set the figure title, and use the `theme_void()` function to strip redundant elements in the theme, such as backgrounds and gridlines.

Finally, we can get the administrative map of Henan Province. Adjust post detail and other parameters as needed to refine the result.

Guess you like

Origin blog.csdn.net/2301_77925375/article/details/131199834