How to Add Title to a GGPlot



How to Add Title to a GGPlot

This tutorial shows how to add title to a ggplot. The function labs() is used.

Related Book

GGPlot2 Essentials for Great Data Visualization in R

Prerequisites

Load required packages and set the theme function theme_minimal() as the default theme:

library(ggplot2) # For data visualization
theme_set(theme_minimal())

Add title, subtitle and caption

# Default plot
library(ggplot2)
p <- ggplot(ToothGrowth, aes(x = factor(dose), y = len)) + 
  geom_boxplot()
print(p)


# Add titles
p <- p + labs(title = "Effect of Vitamin C on Tooth Growth",
              subtitle = "Plot of length by dose",
              caption = "Data source: ToothGrowth")
p

Version: Français





No Comments

Give a comment