R Error: discrete value supplied to continuous scale

The “discrete value supplied to continuous scale” error message is one that results from a simple coding mistake. It is among the simplest mistakes to find and correct. Unfortunately, it is also an easy mistake to make in the first place. However, once you understand it it is also easy to avoid.

Special note: all the code examples used in this article require the ggplot2 package. If you do not have it installed you can use the following code to install it.

install.packages("ggplot2")

Once it is installed you will need to run the following code before running any projects that use the ggplot2 package.

library("ggplot2")

The circumstances of this error.

The “discrete value supplied to continuous scale” message occurs when using the ggplot() function along with the scale__continuous()scale function to specify a specific scale for a graph.

# r error discrete value supplied to continuous scale
> a = data.frame(x = 1:10, y = c("cat", "dog", "bat", "cow", "rat"))
> ggplot(a, aes(x, y)) + geom_point()

In this example of the basic usage of ggplot(), the scale function is not used. However, it does produce a successful graph of the data frame. Getting this message requires including the scale function and doing so incorrectly. That turns out to be the key to fixing the continuous variable problem and that is using the scale function correctly.

What is causing this error?

The “discrete value supplied to continuous scale” message is caused by erroneous use of the scale function along with ggplot().

# discrete value supplied to continuous scale r error
> a = data.frame(x = 1:10, y = c("cat", "dog", "bat", "cow", "rat"))
> ggplot(a, aes(x, y)) + geom_point() + scale_y_continuous(limits = c(0, 15))
Error: Discrete value supplied to continuous scale

The mistake in this example that leads to the error message is trying to create a continuous scale for the “y” axis. The problem is that the “y” vector is a discrete variable consisting of words, i.e. a character vector or categorical variable, not a numeric vector, which would create continuous data. This means that you are trying to create a continuous value for a discrete value axis. It does not work resulting in our message.

How to fix this error.

The “discrete value supplied to continuous scale” message can be fixed but making sure that at least one of the position scales in the data frame is a continuous value rather than a discrete scale and applying the scale function to that character vector to make it a numeric vector.

# discrete value supplied to continuous scale solution
> a = data.frame(x = 1:10, y = c("cat", "dog", "bat", "cow", "rat"))
> ggplot(a, aes(x, y)) + geom_point() + scale_x_continuous(limits = c(0, 15))

As you can see from this example using “x” instead of “y” in the scale function fixes the problem by supplying a numeric value list instead of characters. This factor makes all the difference.

# discrete value continuous scale r solution
> a = data.frame(x = 1:10, y = c(1:5))
> ggplot(a, aes(x, y)) + geom_point() + scale_y_continuous(limits = c(0, 7))

The other option as seen above is to turn “y” into a numeric list as illustrated above. The one problem with this solution is that the dataset graph, scatter plot, box plot, or bar chart is losing some of its meaning. This is because the model no longer has the categorical variable labels that it previously did.

The “discrete value supplied to continuous scale” error message is more of a minor nuisance than a serious problem. It is simply a matter of using the wrong vector from the data frame. Correcting that one continuous data mistake in your plotting code solves the problem in a simple and easy manner.

R Error: discrete value supplied to continuous scale