Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

inserting custom text to ggplot2

Tags:

r

ggplot2

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?

like image 273
user1471980 Avatar asked Aug 16 '12 19:08

user1471980


People also ask

What is annotate in ggplot2?

The annotate() function allows to add all kind of shape on a ggplot2 chart. The first argument will control what kind is used: rect or segment for rectangle, segment or arrow.

How do I annotate a plot in R?

If you want to annotate your plot or figure with labels, there are two basic options: text() will allow you to add labels to the plot region, and mtext() will allow you to add labels to the margins. For the plot region, to add labels you need to specify the coordinates and the label.

How do you add labels to a graph in R?

Use the title( ) function to add labels to a plot. Many other graphical parameters (such as text size, font, rotation, and color) can also be specified in the title( ) function. # labels 25% smaller than the default and green.

Which argument of Ggplot can be used to add customization to plots?

To customize the plot, the following arguments can be used: alpha, color, linetype, shape, size and fill.


1 Answers

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) 
like image 98
Luciano Selzer Avatar answered Nov 04 '22 08:11

Luciano Selzer