R Scale for fill is already present – Replace existing scale (2 Examples)
In this tutorial, I’ll explain how to deal with “Scale for ‘fill’ is already present. Adding another scale for ‘fill’, which will replace the existing scale.” in the R programming language.
Preparing the Examples
data(iris) # Load example data head(iris) # Display head of example data # Sepal.Length Sepal.Width Petal.Length Petal.Width Species # 1 5.1 3.5 1.4 0.2 setosa # 2 4.9 3.0 1.4 0.2 setosa # 3 4.7 3.2 1.3 0.2 setosa # 4 4.6 3.1 1.5 0.2 setosa # 5 5.0 3.6 1.4 0.2 setosa # 6 5.4 3.9 1.7 0.4 setosa |
data(iris) # Load example data head(iris) # Display head of example data # Sepal.Length Sepal.Width Petal.Length Petal.Width Species # 1 5.1 3.5 1.4 0.2 setosa # 2 4.9 3.0 1.4 0.2 setosa # 3 4.7 3.2 1.3 0.2 setosa # 4 4.6 3.1 1.5 0.2 setosa # 5 5.0 3.6 1.4 0.2 setosa # 6 5.4 3.9 1.7 0.4 setosa
install.packages("ggplot2") # Install & load ggplot2 library("ggplot2") |
install.packages("ggplot2") # Install & load ggplot2 library("ggplot2")
my_plot <- ggplot(iris, # Basic ggplot2 plot aes(x = Species, y = Sepal.Width, fill = Species)) + geom_bar(stat = "identity") my_plot |
my_plot <- ggplot(iris, # Basic ggplot2 plot aes(x = Species, y = Sepal.Width, fill = Species)) + geom_bar(stat = "identity") my_plot
Example 1: Replicating the Warning – Scale for ‘fill’ is already present. Adding another scale for ‘fill’.
my_plot + # Applying two fill functions scale_fill_manual(values = c("purple", "yellow", "pink")) + scale_fill_discrete(guide = guide_legend(reverse = TRUE)) # Scale for 'fill' is already present. Adding another scale for 'fill', which will replace the existing scale. |
my_plot + # Applying two fill functions scale_fill_manual(values = c("purple", "yellow", "pink")) + scale_fill_discrete(guide = guide_legend(reverse = TRUE)) # Scale for 'fill' is already present. Adding another scale for 'fill', which will replace the existing scale.
Example 2: Solving the Warning – Scale for ‘fill’ is already present. Adding another scale for ‘fill’.
ggp + # Applying only one fill function scale_fill_manual(values = c("purple", "yellow", "pink"), guide = guide_legend(reverse = TRUE)) |
ggp + # Applying only one fill function scale_fill_manual(values = c("purple", "yellow", "pink"), guide = guide_legend(reverse = TRUE))