R ggplot2 Error: Continuous value supplied to discrete scale (2 Examples)


In this article, I’ll show how to handle the “Error: Continuous value supplied to discrete scale” in the R programming language.

The tutorial will consist of these topics:

Let’s dig in…


Exemplifying Data, Packages & Default Graph

In the first place, I’ll have to create some example data:

set.seed(834759)                                             # Create example data
data <- data.frame(x = rnorm(100),
                   group = 1:5)
head(data)                                                   # Head of example data


table 1 data frame r error continuous value supplied discrete scale


Table 1 shows the structure of our example data – It is constituted of 100 rows and two columns.

The variable x contains the values we want to display in a boxplot and the variable group defines the corresponding groups of our data.

We also need to install and load the ggplot2 package, in order to use the corresponding functions and commands:

install.packages("ggplot2")                                  # Install & load ggplot2 package


Example 1: Reproduce the ggplot2 Error: Continuous value supplied to discrete scale

In this example, I’ll illustrate how to replicate the error message “Continuous value supplied to discrete scale” when drawing a graphic using the ggplot2 package in R.

Have a look at the following R code:

ggplot(data, aes(x, group = group, fill = group)) +          # Try to modify colors
  geom_boxplot() + 
  scale_fill_manual(values = c("red", "yellow",
                               "blue", "orange",
# Error: Continuous value supplied to discrete scale

As you can see, the RStudio console returns the “Error: Continuous value supplied to discrete scale” after executing the previous R syntax.

The reason for this is that our grouping variable has the numeric data class. However, to properly color the boxes of our groups, the grouping variable needs to be a factor.

So how can we solve this problem? Keep on reading!


Example 2: Fix the ggplot2 Error: Continuous value supplied to discrete scale

The following syntax shows how to avoid the “Error: Continuous value supplied to discrete scale”.

For this, we have to convert our grouping variable to the factor class using the factor() function.

Compare the last part of line one in the following code with the corresponding part in the previous example. As you can see, we are using the factor function to convert our grouping column from numeric to factor.

ggplot(data, aes(x, group = group, fill = factor(group))) +  # Properly adding colors
  geom_boxplot() + 
  scale_fill_manual(values = c("red", "yellow",
                               "blue", "orange",


r graph figure 1 r error continuous value supplied discrete scale


After executing the previous syntax the boxplot illustrated in Figure 1 has been created. As you can see, we have drawn each boxplot of our graphic in a different color.


Video & Further Resources

Do you need more information on the R programming syntax of this tutorial? Then I recommend having a look at the following video of my YouTube channel. I’m explaining the examples of this article in the video:


The YouTube video will be added soon.


In addition, you might have a look at the other articles on this website. A selection of posts is shown below:


In summary: You have learned in this article how to deal with the “Error: Continuous value supplied to discrete scale” in the R programming language. In case you have further questions, let me know in the comments. Furthermore, don’t forget to subscribe to my email newsletter for regular updates on the newest articles.


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.

6 Comments. Leave new

  • Thank you for the great explanation! It immediatly worked with the added factor argument.

  • Hello Jacob. Thank you very much for your good content.
    I want to use discrete legend instead of continuous legend for raster maps. Can you guide me?

    • Hey,

      Thank you for the kind words! Could you share your code and describe your data?


      • Hello, Joachim.
        Yes, Sure. I have a raster data with TIFF format, which is a continuous data. Now I want to create a discrete legend with five classes for this data using ggplot. I tried to do it but failed.
        the code:

        # Load packages
        # Load data
        sample= raster(“sample.tif”)

        reclass <- c(0, 0.2, 1,
        0.2, 0.4, 2,
        0.4, 0.6, 3,
        0.6, 0.7, 4,
        0.8, 1, 5)
        reclass_m <- matrix(reclass,
        ncol = 3,
        byrow = TRUE)

        sample_classified <- reclassify(sample, reclass_m)

        sample_classified[sample_classified == 0] <- NA

        val <- getValues(sample_classified)
        xy <- as.data.frame(xyFromCell(sample_classified,1:ncell(sample_classified)))

        xy <- cbind(xy,val)

        ggplot(na.omit(xy), aes(x=x, y=y, fill=val)) +

        scale_fill_distiller(palette = "Set3", labels= c("Vl", "L", "M", "H","Vh"), name= "Class")+

        annotation_scale(location ="br", width_hint= 0.5, pad_x = unit(1, "cm") )+
        annotation_north_arrow (location="tl", which_north="true", pad_x= unit(0.4, "in"), pad_y= unit(0.3, "in"),
        style= north_arrow_fancy_orienteering)+

        theme(legend.title = element_text(colour="blue", size=10, face="bold"))+

        theme(legend.text = element_text(colour="blue", size=10, face="bold"))+

        theme(legend.background = element_rect(fill="lightblue",size=0.3, linetype="solid", colour ="darkblue"))+

        theme(legend.position="right", legend.box = "horizontal")+


Leave a Reply

Your email address will not be published.

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