As easy as programming has become over the years, it’s still full of random bugs and glitches that keep popping up from time to time. A missed semi-colon here, the wrong syntax there and the entire code comes crashing down.
In this article, we’re talking about how to fix the “discrete value supplied to continuous scale” in the R programming language.
What causes this error?
The error mostly triggers due to incorrect use of the Scale function in R, along with the ggplot() function. Since discrete variables contain character vectors or categorical variables, and not continuous data (which requires a numeric vector), you get the error.
This isn’t exactly a bug in the function, just a mathematical matter of using the wrong vector for a particular data frame. Correcting this one mistake will resolve the error with both the scale and ggplot functions.
How to fix this?
The easiest way to resolve the issue is by ensuring that at least one of the position scales being used to plot the graph has a continuous value instead of a discrete vector (or scale) and then using the scale function.
a = data.frame(x = 1:10, y = c("This", "is", "a", "code", "snippet")) ggplot(a, aes(x, y)) + geom_point() + scale_x_continuous(limits = c(0, 15))
The aforementioned example has a numeric vector and a discrete vector. Using x in place of y with the ggplot function resolves the error for us as x is a numeric vector.
The second approach would be to convert at least one of the discrete vectors into a numeric vector, hence gaining a continuous value for plotting. Keep in mind that this applies to all other values as well. If you’re experiencing the error with other data types and/or values, try using a numeric vector to fix the issue.
Also read: What is ErP ready?
Someone who writes/edits/shoots/hosts all things tech and when he’s not, streams himself racing virtual cars. You can reach out to Yadullah at [email protected], or follow him on Instagram or Twitter.