How to Change GGPlot Legend Size



How to Change GGPlot Legend Size

This article describes how to change ggplot legend size. You will learn how to modify the legend title and text size.

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) 
theme_set(theme_minimal())

Basic plot

Start by creating a box plot using the ToothGrowth data set. Change the box plot fill color according to the grouping variable dose.

ToothGrowth$dose <- as.factor(ToothGrowth$dose)
p <- ggplot(ToothGrowth, aes(x = dose, y = len))+ 
  geom_boxplot(aes(fill = dose)) + 
  scale_fill_viridis_d()
p

Chage legend size

The following R code modifies the size of the legend title and text:

p + theme(
  legend.title = element_text(color = "blue", size = 14),
  legend.text = element_text(color = "red", size = 10)
  )



Version: Français





No Comments

Give a comment

Want to post an issue with R? If yes, please make sure you have read this: How to Include Reproducible R Script Examples in Datanovia Comments