Learn R Programming

cowplot (version 0.9.2)

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 (origin) of the label.

y

The y location (origin) of the label.

hjust

Horizontal justification. Default = 0.5 (centered on x). 0 = flush-left at x, 1 = flush-right.

vjust

Vertical justification. Default = 0.5 (centered on y). 0 = baseline at y, 1 = ascender at y.

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.

See Also

ggdraw

Examples

Run this code
# NOT RUN {
# setup plot and a label (regression description)
p <- ggplot(mtcars, aes(mpg, disp)) + geom_line(color = "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)))

# Add label to plot, centered on {x,y} (in data coordinates)
p + draw_label(label, x = 20, y = 400)
# Add label to plot in data coordinates, flush-left at x, baseline at y.
p + draw_label(label, x = 20, y = 400, hjust = 0, vjust = 0)

# Add label to plot. Data coordinates, drawing rightward
# from x, with ascenders of text touching y.
p + draw_label(label, x = 20, y = 400, hjust = 0, vjust = 1)

# Add labels via ggdraw. Uses ggdraw coordinates.
# ggdraw coordinates default to xlim = c(0, 1), ylim = c(0, 1).
ggdraw(p) + draw_label("centered on 70% of x, 90% of y height", x = 0.7, y = 0.9)
labstr = "bottom left at {0%, 0%} of the SHEET, not the plot!"
p = ggdraw(p) + draw_label(labstr, x = 0, y = 0, hjust = 0, vjust = 0)
p = p + draw_label("top right at {1,1}", x = 1, y = 1, hjust = 1, vjust = 1)
p = p + draw_label("bottom left at {.4,.4}", x = 0.4, y = 0.4, hjust = 0, vjust = 0)
p + draw_label("centered on at {.5,.5}", x = 0.5, y = 0.5, hjust = 0.5, vjust = 0.5)
# }

Run the code above in your browser using DataLab