How can I resolve the error “Discrete value supplied to continuous scale” in R? 2

How can I resolve the error “Discrete value supplied to continuous scale” in R?

The error “Discrete value supplied to continuous scale” in R occurs when there is a mismatch between the type of data being used and the scale being applied. To resolve this error, you can check the type of data being used and make sure it is appropriate for the scale being used. If necessary, you can also convert the data to the correct type before applying the scale. Additionally, checking for any missing or incorrect values in the data can also help resolve this error.

Fix R Error: Discrete value supplied to continuous scale


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:

Cite this article

stats writer (2024). How can I resolve the error “Discrete value supplied to continuous scale” in R?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-resolve-the-error-discrete-value-supplied-to-continuous-scale-in-r/

stats writer. "How can I resolve the error “Discrete value supplied to continuous scale” in R?." PSYCHOLOGICAL SCALES, 5 May. 2024, https://scales.arabpsychology.com/stats/how-can-i-resolve-the-error-discrete-value-supplied-to-continuous-scale-in-r/.

stats writer. "How can I resolve the error “Discrete value supplied to continuous scale” in R?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-resolve-the-error-discrete-value-supplied-to-continuous-scale-in-r/.

stats writer (2024) 'How can I resolve the error “Discrete value supplied to continuous scale” in R?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-resolve-the-error-discrete-value-supplied-to-continuous-scale-in-r/.

[1] stats writer, "How can I resolve the error “Discrete value supplied to continuous scale” in R?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.

stats writer. How can I resolve the error “Discrete value supplied to continuous scale” in R?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top