Labeling center for polygon maps in R ggplot

I am trying to mark my polygons using ggplot in R. I found a theme here in stackoverflow, which, in my opinion, is very close to what I want, except with dots.

Label points in geom_point

I found several methods on the Internet. Now I first need to find the central location of each figure, and then I have to combine these places with the name. Then bind this to the marking function in geom_text ()

ggplot centered names on the map

Since I tried to do this for a long time, I decided to ask a question and I hope that someone here can give me the last impetus to what I want. My build function:

region_of_interest.fort <- fortify(region_of_interest, region = "score") region_of_interest.fort$id <- as.numeric(region_of_interest.fort$id) region_of_interest.fort$id <- region_of_interest.fort$id region_of_interest.fort1 <- fortify(region_of_interest, region = "GM_NAAM") region_of_interest.fort1$id <- as.character(region_of_interest.fort1$id) region_of_interest.fort1$id <- region_of_interest.fort1$id idList <- unique(region_of_interest.fort1$id) centroids.df <- as.data.frame(coordinates(region_of_interest)) names(centroids.df) <- c("Longitude", "Latitude") randomMap.df <- data.frame(id = idList, shading = runif(length(idList)), centroids.df) ggplot(data = region_of_interest.fort, aes(x = long, y = lat, fill = id, group = group)) + geom_polygon() + geom_text(centroids.df, aes(label = id, x = Longitude, y = Latitude)) + scale_fill_gradient(high = "green", low = "red", guide = "colorbar") + coord_equal() + theme() + ggtitle("Title") 

This gives me an error: ggplot2 does not know how to handle uneval class data

My details

 region_of_interest$GM_NAAM [1] Groningen Haren Ooststellingwerf Assen Aa en Hunze Borger- Odoorn [7] Noordenveld Westerveld Tynaarlo Midden-Drenthe 415 Levels: 's-Gravenhage 's-Hertogenbosch Aa en Hunze Aalburg Aalsmeer Aalten ... Zwolle region_of_interest$score [1] 10 -2 -1 2 -1 -4 -4 -5 0 0 
+8
r maps ggplot2 label
source share
2 answers

Try something like this?

  • Get a frame of data about the centroids of your polygons from the original map object.

  • In the data frame that you are drawing, make sure that there are columns for the identifier you want to label, as well as the longitude and latitude of the Centroid.

  • Use geom_text in ggplot to add tags.

In accordance with this example, I read the world map, extracting ISO3 identifiers for use as my polygonal labels and creating a data frame from the country identifier, and the longitude and latitude of the centroids. Then I draw population data on the world map and add labels to the centroids.

 library(rgdal) # used to read world map data library(rgeos) # to fortify without needing gpclib library(maptools) library(ggplot2) library(scales) # for formatting ggplot scales with commas # Data from http://thematicmapping.org/downloads/world_borders.php. # Direct link: http://thematicmapping.org/downloads/TM_WORLD_BORDERS_SIMPL-0.3.zip # Unpack and put the files in a dir 'data' worldMap <- readOGR(dsn="data", layer="TM_WORLD_BORDERS_SIMPL-0.3") # Change "data" to your path in the above! worldMap.fort <- fortify(world.map, region = "ISO3") # Fortifying a map makes the data frame ggplot uses to draw the map outlines. # "region" or "id" identifies those polygons, and links them to your data. # Look at head(worldMap@data) to see other choices for id. # Your data frame needs a column with matching ids to set as the map_id aesthetic in ggplot. idList <- worldMap@data$ISO3 # "coordinates" extracts centroids of the polygons, in the order listed at worldMap@data centroids.df <- as.data.frame(coordinates(worldMap)) names(centroids.df) <- c("Longitude", "Latitude") #more sensible column names # This shapefile contained population data, let plot it. popList <- worldMap@data$POP2005 pop.df <- data.frame(id = idList, population = popList, centroids.df) ggplot(pop.df, aes(map_id = id)) + #"id" is col in your df, not in the map object geom_map(aes(fill = population), colour= "grey", map = worldMap.fort) + expand_limits(x = worldMap.fort$long, y = worldMap.fort$lat) + scale_fill_gradient(high = "red", low = "white", guide = "colorbar", labels = comma) + geom_text(aes(label = id, x = Longitude, y = Latitude)) + #add labels at centroids coord_equal(xlim = c(-90,-30), ylim = c(-60, 20)) + #let view South America labs(x = "Longitude", y = "Latitude", title = "World Population") + theme_bw() 

Population map of south america

A minor technical note: in fact, the coordinates in the sp package do not quite find the centroid, but it usually should give a reasonable location for labeling . Use gCentroid in the rgeos package if you want to stick on a real centroid in more complex situations as non-contiguous shapes .

+12
source share

The accepted answer here may work, but in the most specific question it is specifically noted that there is an error: "ggplot2 does not know how to handle data from the uneval class".

The reason it gives you an error is because the inclusion of centroids.df must be a named variable (for example, followed by "data =")

Currently

 ggplot(data = region_of_interest.fort, aes(x = long, y = lat, fill = id, group = group)) + geom_polygon() + geom_text(centroids.df, aes(label = id, x = Longitude, y = Latitude)) + scale_fill_gradient(high = "green", low = "red", guide = "colorbar") + coord_equal() + theme() + ggtitle("Title") 

Should be (note: "data = centroids.df"):

 ggplot(data = region_of_interest.fort, aes(x = long, y = lat, fill = id, group = group)) + geom_polygon() + geom_text(data=centroids.df, aes(label = id, x = Longitude, y = Latitude)) + scale_fill_gradient(high = "green", low = "red", guide = "colorbar") + coord_equal() + theme() + ggtitle("Title") 

This problem has been addressed here: How to deal with the "uneval class data" error from ggplot2?

+2
source share

All Articles