6

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

Community
  • 1
  • 1
vengefulsealion
  • 756
  • 11
  • 18

1 Answers1

6

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

lukeA
  • 53,097
  • 5
  • 97
  • 100
  • Hey @lukeA, thanks very much for this. I feel silly for missing the existing question. I noticed that this method can produce multiple labels for each polygon? I was able to remove them manually this time around, but I wonder if it could be tweaked so that only one showed each time? – vengefulsealion Mar 11 '15 at 10:58
  • You're welcome. Good point BTW - one should use `annotate` instead of `geom_text` here. I edited the post. – lukeA Mar 11 '15 at 12:07