Learn R Programming

cowplot (version 0.5.0)

draw_label: Draw a text label or mathematical expression.

Description

This function can draw either a character string or mathematical expression at the given coordinates. It works both on top of ggdraw and directly with ggplot, depending on which coordinate system is desired (see examples).

Usage

draw_label(label, x = 0.5, y = 0.5, hjust = 0.5, vjust = 0.5,
  fontfamily = "", fontface = "plain", colour = "black", size = 14,
  angle = 0, lineheight = 0.9, alpha = 1)

Arguments

label
String or plotmath expression to be drawn.
x
The x location of the label.
y
The y location of the label.
hjust
Horizontal justification
vjust
Vertical justification
fontfamily
The font family
fontface
The font face ("plain", "bold", etc.)
colour
Text color
size
Point size of text
angle
Angle at which text is drawn
lineheight
Line height of text
alpha
The alpha value of the text

Details

By default, the x and y coordinates specify the center of the text box. Set hjust = 0, vjust = 0 to specify the lower left corner, and other values of hjust and vjust for any other relative location you want to specify.

Examples

Run this code
p <- ggplot(mtcars, aes(mpg, disp)) + geom_line(colour = "blue") + background_grid(minor='none')
c <- cor.test(mtcars$mpg, mtcars$disp, method='sp')
label <- substitute(paste("Spearman ", rho, "= ", estimate, ", P = ", pvalue),
                    list(estimate = signif(c$estimate, 2), pvalue = signif(c$p.value, 2)))
# adding label via ggdraw, in the ggdraw coordinates
ggdraw(p) + draw_label(label, .7, .9)
# adding label directly to plot, in the data coordinates
p + draw_label(label, 20, 400, hjust = 0, vjust = 0)

Run the code above in your browser using DataLab