Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add labels to a choropleth map created using ggplot2?

Tags:

r

ggplot2

gis

I am attempting and currently failing to add text annotations to a choropleth map I created in ggplot2. I am seeking to label each of the polygons (a local government area) with its name.

Before I go on, I know that a similar questions have been asked both on SO and detailed in a (very good) tutorial here. However, I have tried several methods unsuccessfully and think I may have stumbled across a different issue. I suspect that one of the reasons that my code is failing is that I am trying to annotate a geom_polygon() whereas other methods I have seen have detailed how to annotate a geom_map object. That said, I can't think why this shouldn't be possible with a geom_polygon.

I have included my code below. You can download my data from here. The data frame includes my data joined to a fortified shapefile. The labels I am attempting to append are in the column "LGA_NAME11".

## LOAD PACKAGES
require(ggplot2)
require(rgdal)
require(dplyr)

## SET GGPLOT THEME
theme_clean <- function(base_size = 12) {
    require(grid)
    theme_grey(base_size) %+replace%
            theme(
                    axis.title = element_blank(),
                    axis.text = element_blank(),
                    panel.background = element_blank(),
                    panel.grid = element_blank(),
                    axis.ticks.length = unit(0,"cm"), 
                    axis.ticks.margin = unit(0,"cm"),
                    panel.margin = unit(0,"lines"),
                    plot.margin = unit(c(0, 0, 0, 0), "lines"),
                    complete = TRUE
            )}

## SET COLOUR PALETTES
palette1 <- c("#f2f0f7", "#dadaeb", "#bcbddc", "#9e9ac8", "#756bb1", "#54278f")

## SET LABEL NAMES
lgaNamesSydney <- aggregate(cbind(long, lat) ~ LGA_NAME11, data=sydneyMapData, FUN = function(x) mean(range(x)))
lgaNamesSydney <- lgaNamesSydney %>% rename(lga = LGA_NAME11)
lgaNamesSydney$angle <- 0

## ATTEMPT TO PLOT MAP WITH LABELS
ggplot(sydneyMapData) +
    aes(long, lat, group=group, fill=Factor1) +
    geom_polygon() +
    geom_text(data=lgaNamesSydney, aes(long, lat, label = LGA_NAME11, angle=angle, map_id =NULL), size=2.5) +
    scale_fill_manual(values = palette1) +
    labs(fill="Drop Bears \nper 1000 population") +
    coord_map(projection = "mercator") +
    theme_clean()

If anyone has any suggestions, I would be extremely grateful if anyone could point where I am going wrong. Thanks in advance.

As an aside, I recognise that labeling choropleths can detract from the visual experience, but the boss has requested them specially!

Here is my plot sans labels

like image 980
vengefulsealion Avatar asked Mar 10 '15 11:03

vengefulsealion


People also ask

How do you label a Choropleth map?

In symbol and choropleth maps, you can add labels that help your readers orientate themselves. To do so, go to step 3: Visualize and then to the Annotate tab. You'll find the option Map labels right below the description fields. Turn on Show labels.

What kind of data is used to make Choropleth maps?

Choropleth maps use the Counts and amounts (Color) smart mapping symbol type to show normalized data as shaded points, lines, or areas.

What is Choropleth map in R?

A choropleth map displays divided geographical areas or regions that are coloured in relation to a numeric variable. This section provides many examples build with R. It focuses on the leaflet package for interactive versions, and the ggplot2 for static ones.

What is Choropleth map Python?

Choropleths are geographic maps that display statistical information encoded in a color palette.


1 Answers

You could add labels like this

# http://stackoverflow.com/questions/9441778/improve-centering-county-names-ggplot-maps
centroids <- setNames(do.call("rbind.data.frame", by(sydneyMapData, sydneyMapData$group, function(x) {Polygon(x[c('long', 'lat')])@labpt})), c('long', 'lat')) 
centroids$label <- sydneyMapData$LGA_NAME11[match(rownames(centroids), sydneyMapData$group)]

ggplot(sydneyMapData, aes(long, lat, group=group, fill=Factor1)) +
  geom_polygon(colour = "white") +
  with(centroids, annotate(geom="text", x = long, y=lat, label = label, size = 2.5)) 

enter image description here

like image 107
lukeA Avatar answered Oct 24 '22 00:10

lukeA