Re: change labels of legend in geom_polygon

1,389 views
Skip to first unread message

Brandon Hurr

unread,
Dec 20, 2012, 9:45:24 AM12/20/12
to Kyle Peyton, ggplot2
I think you want to use labels= c(...). Easier to know for sure with a reproducible example. 


On Thu, Dec 20, 2012 at 8:21 AM, Kyle Peyton <kpe...@gmail.com> wrote:
Hi all,

I'm working on a chloropleth and trying to change the default legend. As it stands, the legend outputs value in scientific notation. Instead of having (2e+03,4e+04] I would prefer to have "2k to 4k". My code is below. 

#ggplot code  
m2$exp <- cut(as.numeric(m2[,13]), breaks = c(seq(0, 14000, by = 2000)))  
cbbPalette <- brewer.pal(6, "Greens")

map = ggplot(m2, aes(long, lat, group = group)) +  
  geom_polygon(aes(fill = exp), colour = I("black"), size = 0.2) +  
  scale_fill_manual(values=cbbPalette, name = "Expenditure/EGM") +  
  ylab("Latitude") +   
  xlab("Longitude") + 
  theme(panel.background = element_blank()) 
#breaks=c("(2k to 4k"),("4k to 6k"), ("6k to 8k"),("8kto 10K"),("10k to 12k"), ("12k to 14k"))
map


The 'breaks' line I commented out used to be after the "exp" in aes(fill=exp) so it was aes(fill=exp, breaks=c("(2k to 4k"),("4k to 6k"), ("6k to 8k"),("8kto 10K"),("10k to 12k"), ("12k to 14k"))) but unfortunately this didn't do the trick. I've also put a picture of the work in progress below to help. 

Thanks very much for any advice. 

cheers,
Kyle 


--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

Kyle Peyton

unread,
Dec 20, 2012, 6:36:34 PM12/20/12
to Brandon Hurr, ggplot2
thanks Brandon! It was the labels, and I also needed to include this w/ 'scale_fill_manual' rather than geom_polygon: 

#ggplot code  
map = ggplot(m2, aes(long, lat, group = group)) +  
  geom_polygon(aes(fill = exp), colour = I("black"), size = 0.2) +  
  scale_fill_manual(values=cbbPalette, name = "Expenditure/EGM", labels = c("2k to 4k","4k to 6k", "6k to 8k","8k to 10K","10k to 12k")) +  
  ylab("Latitude") +   
  xlab("Longitude") + 
  theme(panel.background = element_blank(), plot.title= element_text(lineheight=2.8, face="bold")) 
map
Reply all
Reply to author
Forward
0 new messages