banner



How To Change Axis Labels In Ggplot

How to Change GGPlot Labels: Title, Axis and Legend

Introduction

In this article, you will larn how to change ggplot labels, including main championship, subtitle, centrality labels, caption, fable titles and tag.

  • Plot championship and subtitle provides insights into the chief findings
  • Caption are more often than not used to describe the information source
  • Tag tin be used for differentiating between multiple plots.

Related Volume

GGPlot2 Essentials for Great Information Visualization in R

Prerequisites

Load required packages and set the theme office theme_classic() every bit the default theme:

                    library(ggplot2)  theme_set(theme_classic())                  

Create a basic plot using the dataset ToothGrowth:

                    # Convert the variable dose from numeric to factor variable ToothGrowth$dose <- as.cistron(ToothGrowth$dose)  # Create a boxplot colored by dose group levels bxp <- ggplot(ToothGrowth, aes(x = dose, y = len)) +    geom_boxplot(aes(color = dose)) +   scale_color_manual(values = c("#00AFBB", "#E7B800", "#FC4E07")) bxp                  

Key R functions

                    labs(..., championship = waiver(), subtitle = waiver(), caption = waiver(),   tag = waiver())  xlab(label)  ylab(label)  ggtitle(label, subtitle = waiver())                  
  • ...: A list of new proper name-value pairs. The name should be an aesthetic. For example p + labs(title = "Chief title", ten = "Ten axis label", y = "Y axis characterization") changes main title and axis labels.
  • title: plot main title.
  • subtitle: the text for the subtitle for the plot which volition be displayed below the title.
  • caption: the text for the caption which will be displayed in the bottom-right of the plot by default.
  • tag: the text for the tag label which will be displayed at the superlative-left of the plot by default.
  • label: the title of the respective axis (for xlab() or ylab()) or of the plot (for ggtitle()).

Add together titles and centrality labels

In this section, we'll use the function labs() to alter the master title, the subtitle, the axis labels and captions.

It's also possible to utilize the functions ggtitle(), xlab() and ylab() to alter the plot title, subtitle, x and y axis labels.

Add a title, subtitle, caption and change axis labels:

                    bxp <- bxp + labs(title = "Result of Vitamin C on Molar Growth",               subtitle = "Plot of length by dose",               caption = "Information source: ToothGrowth",               x = "Dose (mg)", y = "Teeth length",               tag = "A") bxp                  

Modify fable titles

You can apply labs() to changes the legend title for a given aesthetics (fill, color, size, shape, . . . ). For example:

  • Use p + labs(fill = "dose") for geom_boxplot(aes(fill up = dose))
  • Apply p + labs(color = "dose") for geom_boxplot(aes(colour = dose))
  • and then on for linetype, shape, etc
                    bxp + labs(color = "Dose (mg)")                  

Split long titles

If the title is too long, you lot tin can split it into multiple lines using \n. In this case you can accommodate the space between text lines by specifying the statement lineheight in the theme function element_text():

                    bxp + labs(championship = "Issue of Vitamin C on Molar Growth \due north in Republic of guinea Pigs")+   theme(plot.title = element_text(lineheight = 0.9))                  



Version: Français

Source: https://www.datanovia.com/en/blog/how-to-change-ggplot-labels/

Posted by: donaghyhtful1945.blogspot.com

0 Response to "How To Change Axis Labels In Ggplot"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel