banner



How To Change The Legend In Ggplot2

Change Legend Title in ggplot2 (ii Instance Codes) | Modify Text of ggplot Legends

This article explains how to alter the text of a ggplot2 legend in R. The tutorial is structured every bit follows:

  • Example Information
  • Change Legend Text with scale_color_discrete (Example 1)
  • Alter Fable Text with labs (Example 2)
  • Further Resources for the Modification of ggplot2 plots

Permit's move on to the examples!

Create Instance Data

Kickoff, we have to create some data for the examples of this tutorial:

                prepare                .                seed                (                2468                )                # Set seed for reproducibility                data                <-                data.                frame                (ten                =                c(runif(                200,                0,                2                ), runif(                100,                1,                3                )                ),                # Create 10 variable                y                =                c(runif(                200,                1,                iii                ), runif(                100,                2,                4                )                ),                # Create y variable                grouping                =                c(rep(                "g1",                200                ), rep(                "g2",                100                )                )                )                # Create group variable                head(data)                # Print first half dozen rows of data                #           x        y group                # 1 0.9251444 2.510920    g1                # 2 i.1482466 1.688871    g1                # iii 0.7495806 i.476128    g1                # 4 1.8013962 1.462917    g1                # 5 0.4498683 2.673079    g1                # 6 0.6840810 2.449923    g1              

set.seed(2468) # Set seed for reproducibility data <- data.frame(ten = c(runif(200, 0, 2), runif(100, ane, 3)), # Create 10 variable y = c(runif(200, 1, three), runif(100, 2, 4)), # Create y variable group = c(rep("g1", 200), rep("g2", 100))) # Create group variable head(information) # Impress first 6 rows of data # x y group # 1 0.9251444 2.510920 g1 # 2 1.1482466 1.688871 g1 # iii 0.7495806 i.476128 g1 # four 1.8013962 i.462917 g1 # five 0.4498683 2.673079 g1 # half dozen 0.6840810 2.449923 g1

Our example data frame contains an ten variable, a y variable, besides every bit a grouping variable.

Now let's see how the fable title looks by default. First, nosotros need to install and load the ggplot2 parcel in R…

install.                packages                (                "ggplot2"                )                # Install ggplot2 packet                library(                "ggplot2"                )                # Load ggplot2 bundle              

install.packages("ggplot2") # Install ggplot2 packet library("ggplot2") # Load ggplot2 package

…then we can print a ggplot2 scatterplot equally follows:

my_ggplot                <-                ggplot(data, aes(10, y,                grouping                =                grouping                )                )                +                # ggplot2 with 2 legends                                geom_point(aes(10, y, color                =                group                )                )                my_ggplot                # Impress plot with default legend              

my_ggplot <- ggplot(data, aes(x, y, group = group)) + # ggplot2 with 2 legends geom_point(aes(x, y, color = group)) my_ggplot # Print plot with default legend

ggplot2 Default Legend Title

Figure 1: ggplot2 with Default Specification.

As you can come across based on Figure 1, the default specification of the ggplot2 package shows the column name of our grouping variable as legend title.

In the following examples, I'll testify you two alternatives how to change the text of this legend title in R.

Let's swoop in!

Example 1: Change Text of ggplot Fable Title with scale_color_discrete

This example shows how to modify a ggplot legend championship with scale_color_discrete:

my_ggplot                +                scale_color_discrete(name                =                "My Fable Title No. i"                )                # Manual legend championship              

my_ggplot + scale_color_discrete(name = "My Legend Title No. ane") # Transmission legend title

Modify ggplot2 Legend Title with scale discrete

Figure ii: ggplot2 with Fable Title Modified past scale_color_discrete.

The previous R syntax changed the title to "My Legend Title No. one".

Annotation: Nosotros used the function scale_color_discrete, because our legend is created based on the color specification of our graphic. If we would have created our legend based on other aesthetics, nosotros would have to employ the respective scale_…_discrete function (e.k. scale_fill_discrete for fill). This is by the mode true for all types of plots (eastward.chiliad. histogram, barchart, dotplot, pairs plot and so on).

At that place's another alternative, which is also quite popular for irresolute fable titles of ggplots. Then keep on reading…

Case two: Change Text of ggplot Legend Championship with labs

The second culling that I desire to show y'all is based on the labs function:

my_ggplot                +                labs(color                =                "My Legend Championship No. 2"                )                # Manual legend championship              

my_ggplot + labs(colour = "My Legend Championship No. 2") # Transmission fable championship

Modify ggplot2 Legend Title with labs

Effigy 3: ggplot2 with Legend Title Modified past labs.

Every bit you can see, the second case resulted in the aforementioned legend text every bit the lawmaking of example 1 (besides calling the fable championship No. 2 at the end).

Note: Nosotros used the specification color = "…", because our fable is created based on the color specification of our plot (see explanation above).

By the style, the labs office tin likewise be used to change both legend titles in a ggplot2 graph with two legends. You simply would have to specify multiple arguments within the labs function (eastward.g. shape and color).

Further Resources for ggplot2

The ggplot2 surround provides many different possibilities for the formatting of our plots. If you want to learn more than nigh the ggplot2 package, I can recommend the following YouTube video of Roger Peng:

Besides the video, you might likewise want to have a look at the other R programming tutorials of this website:

  • Remove ggplot2 Legend Entirely
  • Remove Legend Title from ggplot2 Plot in R
  • Add Common Legend to Combined ggplot2 Plot
  • Create Fable in ggplot2 Plot
  • Change Position of ggplot Title
  • R Graphics Gallery
  • Listing of R Commands (+ Examples)
  • The R Programming Language

This tutorial showed how to format the text of legends in ggplot2. If yous take any feedback or comments, don't hesitate to let me know in the comments!

Source: https://statisticsglobe.com/change-legend-title-ggplot-r

Posted by: jacksonsains1958.blogspot.com

0 Response to "How To Change The Legend In Ggplot2"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel