Titles (ggplot2)

Problem

You want to set the title of your graph.

Solution

An example graph without a title:

  1. library(ggplot2)
  2. bp <- ggplot(PlantGrowth, aes(x=group, y=weight)) + geom_boxplot()
  3. bp

plot of chunk unnamed-chunk-2

With a title:

  1. bp + ggtitle("Plant growth")
  2. ## Equivalent to
  3. # bp + labs(title="Plant growth")
  4. # If the title is long, it can be split into multiple lines with \n
  5. bp + ggtitle("Plant growth with\ndifferent treatments")
  6. # Reduce line spacing and use bold text
  7. bp + ggtitle("Plant growth with\ndifferent treatments") +
  8. theme(plot.title = element_text(lineheight=.8, face="bold"))

plot of chunk unnamed-chunk-3plot of chunk unnamed-chunk-3plot of chunk unnamed-chunk-3