7

I have a ggplot graph that I would like to insert custom string below 0 as "Within" and above 0 as "Breached".

I am doing this:

ggplot(z, aes(Date, Breach1/60, group=Jobs, label=c("Within SLA", "Breached SLA"))) + 
 geom_line(size=1) + 
 theme_bw() + ylab("Hours") + xlab("Date") + opts(title="Jobs") + 
 geom_hline(yintercept=0, color="red", size=2) + geom_text(hjust=0, vjust=3)

This seems to put text all over the place. I like to put one text above the zero and one text below the zero value. Any ideas?

1
  • 1
    You should include sample data and include your current graph so that we can recreate your problem and provide a solution. Commented Aug 16, 2012 at 19:35

1 Answer 1

23

You are after annotate:

ggplot(z, aes(Date, Breach1/60, group=Jobs)) + 
 geom_line(size=1) + 
 theme_bw() + ylab("Hours") + xlab("Date") + opts(title="Jobs") + 
 geom_hline(yintercept=0, color="red", size=2) + 
 annotate("text", label = "Within SLA", x = 1, y = 2) +
 annotate("text", label = "Breached", x = 1, y = -2) 
Sign up to request clarification or add additional context in comments.

3 Comments

this is fine but when I put the x and y values, my graphs go back in date. How do I put x and y values so that my chart does not change?
@user1471980 Well since you provided no data I had to guess, just change the x in annotate to whatever you need.
Is it possible to use annotate when the plot is empty and thus there are no x and y coordinates to define the placement? I would just want a centered text!

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.