How to

Fixed – Error: Discrete Value Supplied To Continuous Scale

Avatar

Published

on

Error: Discrete Value Supplied to Continuous Scale

“error: discrete value supplied to continuous scale” keeps showing up? Relax, there is nothing to worry about at all. This is a very common error amongst R language programmers and learners. However, there aren’t many guides that show how this error is solved. So, we have taken it as our duty to ease your worries and get rid of this error.

There are so many R language users who get stuck due to this error. R language is not as easy as Python, and I greatly respect you for learning this computer language. Now without further ado, let’s jump right into the guide.

Error: Discrete Value Supplied To Continuous Scale

Stressed programmer

Let’s first briefly understand the main reasons for the “error: discrete value supplied to continuous scale” message to show up. R is a difficult computer language; it does not adhere to the standard conventions of other programming languages. Along with that, R users also commonly encounter the “error in file(file, “rt”): cannot open the connection.”

The most common reason why this error message appears is when you try to make a boxplot where the data is discrete.  Discrete variables are numeric variables with a finite number of values that can be counted in between two values. A continuous scale is made on a numeric variable, and when the variable is a factor, a discrete scale is made. Using a factor-type variable to create a continuous scale is usually the reason for this error.

People often make mistakes in identifying whether the variable they are using is discrete or continuous. As mentioned before, discrete variables are only numeric and contain values that can be counted. On the other hand, continuous variables can be numeric or integer. These variables may have no limit to them and can be infinite. An example of a constant variable can be temperature.

As of now, you must have gotten an understanding of what could have caused the problem, so let us look at the solution.

How to Solve the Error: discrete value supplied to continuous scale?

The error is easily solved by identifying which of the variable you have misused. Before we start, make sure to install the ggplot2 package using the code install.packages(“ggplot2”). Then, run it using library(“ggplot2”).

You need to keep in mind the basics and verify that your data makes sense. After recognizing the discrete value (factor) you have used in the place of a constant value (numeric), it is time to fix it.

There are multiple ways and methods to fix this error.

Converting the Factor to Numeric

To fix the error you can convert the factor to numeric by the code as.numeric(levels(factor))[factor] or you can also use  the code, as.numeric(as.character(factor)). The code as.numeric(levels(factor))[factor] is said to be more effective, therefore, it’s better to use this.

E.g.  meltDF = melt(df, id.vars = ‘MW’)

  1.                 ggplot(meltDF[meltDF$value == 1,]) + geom_point(aes(x = MW, y = variable)) +
  2.                    scale_x_continuous(limits=c(0, 1200), breaks=c(0, 400, 800, 1200)) +
  3.                    scale_y_continuous(limits=c(0, 1200), breaks=c(0, 400, 800, 1200))

This is the plotting code someone used and encountered the error. Let’s learn how to fix this.

Firstly, convert the factor to numeric like this:

meltDF$variable=as.numeric(levels(meltDF$variable))[meltDF$variable] 

Now we will execute the ggplot command again using:

ggplot(meltDF[meltDF$value == 1,]) + geom_point(aes(x = MW, y =   variable)) +     scale_x_continuous(limits=c(0, 1200), breaks=c(0, 400, 800, 1200)) +  scale_y_continuous(limits=c(0, 1200), breaks=c(0, 400, 800, 1200))                    

Your chart will be generated with no errors!

Finding Fault in Your Command

Programming languages requires a lot of concentration and patience. The erroneous use of a single command can cause your program to stop working. It is very common to make a small mistake while programming but it is essential to find them before it frustrates you out.

In this case, you might have mistakenly confused the variable that is supposed to be a discrete value with a continuous value. If that is the reason all you will need to do is change the variable and place them in their correct position.

For example, the plot below shows an error:

# discrete value supplied to continuous scale r error

  1. a = data.frame(x = 1:10, y = c(“blue”, “red”, “green”, “purple”, “orange”))
  2. ggplot(a, aes(x, y)) + geom_point() + scale_y_continuous(limits = c(0, 15))Error: Discrete value supplied to continuous scale

Can you spot the problem? No? It is fine; this guide is made for a reason. The OC is trying to make a continuous scale with y coordinate while the ‘y’ is a discrete value and can not be used to make a continuous scale. Hence, to solve this problem, we will replace the ‘y’ scale command with the ‘x’ scale command. As shown below:

# discrete value supplied to continuous scale solution

  1. a = data.frame(x = 1:10, y = c(“blue”, “red”, “green”, “purple”, “orange”))
  2. ggplot(a, aes(x, y)) + geom_point() + scale_x_continuous(limits = c(0, 15))

This will successfully solve your problem and fix the issue.

Conclusion

Encountering such errors while programming is nothing new. If you are a beginner, do not worry and keep trying. Even though programming is not easy and consumes a lot of energy, do not stop trying. Indeed, programming has a huge scope. Good luck!

 

Continue Reading
Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Trending