Change Labels of GGPLOT2 Facet Plot in R
In this article, we will see How To Change Labels of ggplot2 Facet Plot in R Programming language.
To create a ggplot2 plot, we have to load ggplot2 package. library() function is used for that. Then either create or load dataframe. Create a regular plot with facets. The labels are added by default.
Now let us explore different approaches to change the labels.
Method 1: Combine Label Variable with Facet Labels
If we want to combine Label Variable (LBLs) to Facet Labels (Values of LBLs), then for that we simply have to use labeller parameter of facet_grid() and set label_both to its value.
Method 2: Using levels()
As we discussed previously the LBLs variable of DataFrame is responsible for Labels of Facet, so now we will create a new DataFrame and assign old DataFrame DF to it. Then simply change the Values of Labels variable of the new DataFrame. Here we use levels() function to set the attribute of a variable in a new manner.
Syntax : levels(x) <- value
- x : A Data Object eg. Vector.
- Value : Named List specifying how to rename the levels.
Return : Set (Rename) the value of vector.
Method 3: Using labeller parameter
The labeller can be a function, or it can be a named character vector (i.e. parameter), which can take either value or variable. labeller() function, when assigned to labeller parameter of facet_grid(), takes named arguments of the form ‘variable = value’ and changes the labels manually.