Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change ggplot coloured lines in legend to squares or circles

Instead of the coloured lines on a grey background that currently make up the legend key, I would like squares or circles of colour next to the key label in order for the colors to be easily visible. How can I do that? Here is a code snippet to use as an example:

 mry <- do.call(rbind, by(movies, round(movies$rating), function(df) {
   nums <- tapply(df$length, df$year, length)
   data.frame(rating=round(df$rating[1]), year = as.numeric(names(nums)),
   number=as.vector(nums))
 }))

ggplot(mry, aes(x=year, y=number, colour=factor(rating))) + geom_line() +
scale_colour_brewer(palette="YlGnBu")
like image 841
George Dontas Avatar asked Sep 07 '12 08:09

George Dontas


1 Answers

one hack coming up to make circles....

ggplot(mry, aes(x=year, y=number, colour=factor(rating))) +
  scale_colour_brewer(palette="YlGnBu") +  
  geom_point()  +      
  geom_point(size=5,colour="white",show_guide=FALSE)  +
  opts(
    panel.background = theme_rect(fill =  "transparent"), 
    panel.grid.minor = theme_blank(),
    panel.grid.major = theme_blank(),
    plot.background = theme_rect(fill = "transparent",colour = NA)
  ) +  geom_line(show_guide=FALSE)

enter image description here

like image 70
user1317221_G Avatar answered Sep 29 '22 19:09

user1317221_G