6

I believe this question is slightly different than similar ones asked on here before because of the use of scale_fill_brewer(. I'm working on a choropleth similar to this one https://gist.github.com/233134

That looks like this:

Chloropleth

and the legend like:

legend

I like it but want to change the labels on the legend from cut looking labels ie (2, 4] to something more friendly like '2% to 4%' or '2% - 4%'. I've seen elsewhere it;s easy to change the labels inside of scale_... as seen here. I can't seem to figure out where to put the labels= argument. I of course could re code choropleth$rate_d but that seems to be inefficient. Where should I put the argument labels=c(A, B, C, D...)?

Here's the piece of the code of interest (for the full code use the link above)

choropleth$rate_d <- cut(choropleth$rate, breaks = c(seq(0, 10, by = 2), 35))

# Once you have the data in the right format, recreating the plot is straight
# forward.

ggplot(choropleth, aes(long, lat, group = group)) +
  geom_polygon(aes(fill = rate_d), colour = alpha("white", 1/2), size = 0.2) + 
  geom_polygon(data = state_df, colour = "white", fill = NA) +
  scale_fill_brewer(pal = "PuRd")

Thank you in advance for your assistance.

EDIT: USing DWin's method (should have posted this error as this is what I ran up against before)

> ggplot(choropleth, aes(long, lat, group = group)) +
+   geom_polygon(aes(fill = rate_d), colour = alpha("white", 1/2), size = 0.2) + 
+   geom_polygon(data = state_df, colour = "white", fill = NA) +
+   scale_fill_brewer(pal = "PuRd", labels = lev4)
Error: Labels can only be specified in conjunction with breaks
1
  • You're too fast. I saw the error and fixed it. I got a slightly more informative message: "Error: Labels and breaks must be same length" Commented Feb 17, 2012 at 3:28

1 Answer 1

10

Besides adding a modified version of the levels, you also need to set the 'breaks' parameter to scale_fill_brewer:

lev = levels(rate_d)  # used (2, 4] as test case
 lev2 <- gsub("\\,", "% to ", lev)
 lev3 <- gsub("\\]$", "%", lev2)
 lev3
[1] "(2% to 4%"
lev4 <- gsub("\\(|\\)", "", lev3)
 lev4
[1] "2% to 4%"

ggplot(choropleth, aes(long, lat, group = group)) +
  geom_polygon(aes(fill = rate_d), colour = alpha("white", 1/2), size = 0.2) + 
  geom_polygon(data = state_df, colour = "white", fill = NA) +
  scale_fill_brewer(pal = "PuRd", labels = lev4, , breaks=seq(0, 10, by = 2) )
Sign up to request clarification or add additional context in comments.

3 Comments

I tried it and get the error I was getting before (nice use of gsub though). I edited my post to reflect this error.
After the edits it works perfect. Thanks DWin. It was the breaks! Duh that's what the error said. I just didn't know what to do with it.
@TylerRinker I realise this is slightly annoying behaviour, but I was worried people might mis-set the labels and they'd end up out of alignment with the breaks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.