How to Fix R Error: Discrete value supplied to continuous scale?

The R programming language allows for the use of certain functions that accept either discrete or continuous inputs. When an error is encountered that states “Discrete value supplied to continuous scale,” it means that a discrete value was supplied to a function that only accepts continuous inputs. The solution to this error is to convert the discrete value into a continuous value before passing it to the function.


One error you may encounter in R is:

Error: Discrete value supplied to continuous scale

This error occurs when you attempt to apply a continuous scale to an axis in ggplot2, yet the variable on that axis is not numeric.

This tutorial shares exactly how to fix this error.

How to Reproduce the Error

Suppose we have the following data frame in R:

#create data frame
df = data.frame(x = 1:12,
                y = rep(c('1', '2', '3', '4'), times=3))

#view data frame
df

    x y
1   1 1
2   2 2
3   3 3
4   4 4
5   5 1
6   6 2
7   7 3
8   8 4
9   9 1
10 10 2
11 11 3
12 12 4

Now suppose we attempt to create a scatterplot with a custom y-axis scale using the scale_y_continuous() argument:

library(ggplot2)

#attempt to create scatterplot with custom y-axis scale
ggplot(df, aes(x, y)) +
  geom_point() +
  scale_y_continuous(limits = c(0, 10))

Error: Discrete value supplied to continuous scale

We receive an error because our y-axis variable is a character instead of a numeric variable.

We can confirm this by using the class() function:

#check class of y variable
class(df$y)

[1] "character"

How to Fix the Error

The easiest way to fix this error is to convert the y-axis variable to a numeric variable before creating the scatterplot:

library(ggplot2) 

#convert y variable to numeric
df$y <- as.numeric(df$y)

#create scatterplot with custom y-axis scale
ggplot(df, aes(x, y)) +
  geom_point() +
  scale_y_continuous(limits = c(0, 10))

Notice that we don’t receive any error because we used scale_y_continuous() with a numeric variable instead of a character variable.

The following tutorials explain how to perform other common plotting functions in ggplot2:

x