Error Continuous Value Supplied to Discrete Scale
R ggplot2 Error: Continuous value supplied to discrete scale (2 Examples)
In this article, I'll show how to handle the "Error: Continuous value supplied to discrete scale" in the R programming language.
The tutorial will consist of these topics:
Let's dig in…
Exemplifying Data, Packages & Default Graph
In the first place, I'll have to create some example data:
set . seed ( 834759 ) # Create example data data <- data. frame (x = rnorm( 100 ), group = 1 : 5 ) head(data) # Head of example data
set.seed(834759) # Create example data data <- data.frame(x = rnorm(100), group = 1:5) head(data) # Head of example data
Table 1 shows the structure of our example data – It is constituted of 100 rows and two columns.
The variable x contains the values we want to display in a boxplot and the variable group defines the corresponding groups of our data.
We also need to install and load the ggplot2 package, in order to use the corresponding functions and commands:
install. packages ( "ggplot2" ) # Install & load ggplot2 package library( "ggplot2" )
install.packages("ggplot2") # Install & load ggplot2 package library("ggplot2")
Example 1: Reproduce the ggplot2 Error: Continuous value supplied to discrete scale
In this example, I'll illustrate how to replicate the error message "Continuous value supplied to discrete scale" when drawing a graphic using the ggplot2 package in R.
Have a look at the following R code:
ggplot(data, aes(x, group = group, fill = group ) ) + # Try to modify colors geom_boxplot( ) + scale_fill_manual(values = c( "red", "yellow", "blue", "orange", "green" ) ) # Error: Continuous value supplied to discrete scale
ggplot(data, aes(x, group = group, fill = group)) + # Try to modify colors geom_boxplot() + scale_fill_manual(values = c("red", "yellow", "blue", "orange", "green")) # Error: Continuous value supplied to discrete scale
As you can see, the RStudio console returns the "Error: Continuous value supplied to discrete scale" after executing the previous R syntax.
The reason for this is that our grouping variable has the numeric data class. However, to properly color the boxes of our groups, the grouping variable needs to be a factor.
So how can we solve this problem? Keep on reading!
Example 2: Fix the ggplot2 Error: Continuous value supplied to discrete scale
The following syntax shows how to avoid the "Error: Continuous value supplied to discrete scale".
For this, we have to convert our grouping variable to the factor class using the factor() function.
Compare the last part of line one in the following code with the corresponding part in the previous example. As you can see, we are using the factor function to convert our grouping column from numeric to factor.
ggplot(data, aes(x, group = group, fill = factor( group ) ) ) + # Properly adding colors geom_boxplot( ) + scale_fill_manual(values = c( "red", "yellow", "blue", "orange", "green" ) )
ggplot(data, aes(x, group = group, fill = factor(group))) + # Properly adding colors geom_boxplot() + scale_fill_manual(values = c("red", "yellow", "blue", "orange", "green"))
After executing the previous syntax the boxplot illustrated in Figure 1 has been created. As you can see, we have drawn each boxplot of our graphic in a different color.
Video & Further Resources
Do you need more information on the R programming syntax of this tutorial? Then I recommend having a look at the following video of my YouTube channel. I'm explaining the examples of this article in the video:
The YouTube video will be added soon.
In addition, you might have a look at the other articles on this website. A selection of posts is shown below:
- ggplot2 Error: Discrete Value Supplied to Continuous Scale
- Error & Warning Messages in R
- R Programming Examples
In summary: You have learned in this article how to deal with the "Error: Continuous value supplied to discrete scale" in the R programming language. In case you have further questions, let me know in the comments. Furthermore, don't forget to subscribe to my email newsletter for regular updates on the newest articles.
Source: https://statisticsglobe.com/r-error-continuous-value-supplied-to-discrete-scale
0 Response to "Error Continuous Value Supplied to Discrete Scale"
Post a Comment