2

I would like to modify the legend in ggplot2 using the text geom. Example is shown below. I want to change the a's to points (circles) and instead of 1, 2, 3 use custom names such as low, medium, high. Any suggestions would be greatly appreciated.

Example Data:

   x  y Freq colors
1 -2 32    2      1
2 -2 36    1      1
3 -2 37    1      1
4 -2 40    2      1
5 -1 32    2      1
6  0 29    2      1

Code:

  fit=ggplot(a1,aes(x,y,color=factor(colors)),col=colors)+
    geom_text(aes(label=Freq),size=5)+
    theme_bw()+
    opts(legend.position='top',
         legend.title=theme_blank(),
         legend.key=theme_rect(fill="white",colour="white"))
  print(fit)

enter image description here

1

1 Answer 1

2

Thanks to Tyler's comments I found a solution (note the grid library needs to be loaded):

  fit=ggplot(a1,aes(x,y,color=factor(colors)),col=colors)+
    geom_text(aes(label=Freq),size=5)+
    theme_bw()+
    scale_color_hue(breaks=c("1", "2", "3"),
                      labels=c("Low", "Medium", "High"))+
    opts(legend.position='top',
         legend.title=theme_blank(),
         legend.key=theme_rect(fill="white",colour="white"))
  print(fit)
  grid.gedit("^key-[-0-9]+$", label = "*")

I wish I could make the "*" bigger in the legend, I understand the next version of ggplot2 will have more legend controls.

Sign up to request clarification or add additional context in comments.

Comments

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.