R ggplot2 Warning: Scale for ‘fill’ is already present – Replace existing

 

In this tutorial you’ll learn how to handle the ggplot2 warning message “Scale for ‘fill’ is already present. Adding another scale for ‘fill’, which will replace the existing scale.” in R.

The table of content looks as follows:

Let’s get started.

 

Example Data, Packages & Basic Graph

I’ll use the following data as basement for this R tutorial:

data <- data.frame(group = LETTERS[1:3],    # Create example data
                   value = 1:9)
data                                        # Print example data

 

table 1 data frame r ggplot2 warning scale fill already present

 

Table 1 illustrates the output of the RStudio console and shows that our example data is constructed of nine rows and two variables.

We also have to install and load the ggplot2 package to R, if we want to use the corresponding functions:

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

Now, we can draw a plot (i.e. a boxplot) of the data as shown below:

ggp <- ggplot(data, aes(x = group,          # Create default ggplot2 graph
                        y = value,
                        fill = group)) +
  geom_boxplot()
ggp                                         # Draw default ggplot2 graph

 

r graph figure 1 r ggplot2 warning scale fill already present

 

In Figure 1 you can see that we have created a ggplot2 box-and-whisker plot by executing the previous syntax.

 

Example 1: Reproduce the Warning Message: Scale for ‘fill’ is already present

In this example, I’ll explain how to replicate the ggplot2 warning “Scale for ‘fill’ is already present. Adding another scale for ‘fill’, which will replace the existing scale.” in the R programming language.

Let’s assume that we want to change the filling color and the ordering of the legend of our ggplot2 plot. Then, we might try to apply the scale_fill_manual and scale_fill_discrete functions as shown below:

ggp +                                       # Applying two fill functions
  scale_fill_manual(values = c("#1b98e0", "yellow", "#353436")) +
  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.

Unfortunately, the RStudio console has returned the warning “Scale for ‘fill’ is already present. Adding another scale for ‘fill’, which will replace the existing scale.” after executing the previous R syntax.

Let’s have a look at the ggplot2 graphic that we have created:

 

r graph figure 2 r ggplot2 warning scale fill already present

 

The output of the previous R syntax is shown in Figure 2 – The ordering of the ggplot2 legend was changed, but the color of the boxes were kept the same. Why did this happen?

The reason for this problem is that we have used multiple “fill” functions simultaneously. The ggplot2 package specifies all colors at the same time. So if we add multiple functions of the same type, ggplot2 gets confused.

But how can we debug this? Keep on reading!

 

Example 2: Fix the Warning Message: Scale for ‘fill’ is already present

This example shows how to avoid the warning message “Scale for ‘fill’ is already present. Adding another scale for ‘fill’, which will replace the existing scale.”.

For this task, we have to specify all fill options within the same function, i.e. scale_fill_manual:

ggp +                                       # Applying only one fill function
  scale_fill_manual(values = c("#1b98e0", "yellow", "#353436"),
                    guide = guide_legend(reverse = TRUE))

 

r graph figure 3 r ggplot2 warning scale fill already present

 

After executing the previous syntax the ggplot2 boxplot shown in Figure 3 has been created. As you can see, both the fill color and the ordering of the legend were changed.

 

Video & Further Resources

If you need further info on the R programming codes of this article, I can recommend watching the following video tutorial of my YouTube channel. I’m explaining the examples of the present post in the video.

 

The YouTube video will be added soon.

 

In addition, you could have a look at the other tutorials on this website.

 

This tutorial has explained 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.

Please note that similar warning messages can appear for other ggplot2 settings (e.g. color). Furthermore, please note that this warning message can also occur when drawing other types of graphics using the ggplot2 package (e.g. barcharts).

If you have further questions, let me know in the comments section.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top