plotflow (version 0.2.1)

ggfaxt: Add Text to a Faceted ggplot2 Plot

Description

A ggplot2 wrapper for adding text to facets.

Usage

ggfaxt(ggplot2.object, x.coord = NULL, y.coord = NULL, labels = NULL, ...)

Arguments

ggplot2.object

a faceted ggplot2 object or an object returned from qfacet_text

x.coord

a single x coordinate to be repeated or a vector of x coordinates equal to the number of facets

y.coord

a single y coordinate to be repeated or a vector of y coordinates equal to the number of facets

labels

a vector of labels to place on each facet

additional arguments accepted by geom_text

Value

Returns a plot of class "gg" "ggplot" with annotations. Also invisibly returns a list object of the class qfacet with the following items:

  • original the Original ggplot2 object

  • new the new ggplot object

  • dat the mini data frame created for the text

See Also

geom_text

Examples

Run this code
# NOT RUN {
#alter mtcars to make some variables factors
mtcars2 <- mtcars
mtcars2[, c("cyl", "am", "gear")] <- lapply(mtcars[,
    c("cyl", "am", "gear")], as.factor)

p <- ggplot(mtcars2, aes(mpg, wt, group = cyl)) +
    geom_line(aes(color=cyl)) +
    geom_point(aes(shape=cyl)) +
    facet_grid(gear ~ am) +
    theme_bw()

z <- ggfaxt(ggplot2.object = p, x.coor = 33, y.coor = 2.2,
    labels = 1:6, color="red")
#approach 1 (alter the text data frame and pass the qfacet object)
z$dat[5, 1:2] <- c(15, 5)
ggfaxt(z, color="red")

#approach 2 (alter the original ggplot object)
ggfaxt(p, x = c(33, 33, 33, 33, 15, 33),
    y = c(2.2, 2.2, 2.2, 2.2, 5, 2.2), 1:6, color="red")

#use "" to not add a label to a facet
ggfaxt(ggplot2.object = p, x.coor = 33, y.coor = 2.2,
    labels = c("", letters[1:4], ""), color="red")

#all the same things you can pass to geom_text qfacet_text takes
ggfaxt(z, labels = paste("beta ==", 1:6),
    size = 3, color = "grey50", parse = TRUE)

#two labels: same plot
p <- ggplot(CO2, aes(conc, uptake, group = Plant)) +
    geom_line(aes(color=Plant)) +
    facet_grid(Type ~ Treatment) +
    theme_bw()

#plot first text layer
z <- ggfaxt(ggplot2.object = p, x.coor = 250, y.coor = 10,
     labels = 1:4, color="red")

#plot second text layer
ggfaxt(ggplot2.object = z$new, x.coor = 900, y.coor = 10,
    labels = paste("beta ==", 11:14), color="blue", parse = TRUE)
# }

Run the code above in your browser using DataCamp Workspace