Next →

Output:


      

Plots:

Advanced ggplot2: Customization and Themes

Once you know the basics, ggplot2 allows you to customize almost every aspect of a plot, from colors and shapes to labels and themes.

Adding Titles and Labels

library(ggplot2)

df <- data.frame(
  x = 1:5,
  y = c(2, 4, 6, 8, 10)
)

ggplot(df, aes(x = x, y = y)) +
  geom_line(color = "blue") +
  ggtitle("Line Plot of X vs Y") +
  xlab("X Axis") +
  ylab("Y Axis")

Using Themes

ggplot(df, aes(x = x, y = y)) +
  geom_line(color = "red") +
  theme_minimal()

Themes help give your plots a polished look, and you can customize colors, fonts, grid lines, and more.