R Coding Errors Suck: Continuous Value Supplied to Discrete Scale

Data Scientist Dude
3 min readFeb 7, 2023
Photo by Luke Peters on Unsplash
Error: Continuous value supplied to discrete scale

What this Error is.

The “Continuous value supplied to discrete scale” error in R typically occurs when you try to use a continuous variable in a plot with a discrete aesthetic, such as fill or color. This error occurs because ggplot2 is expecting a categorical or discrete variable for these aesthetics, and not a continuous variable.

Here is an example:

library(ggplot2)

# Create data frame
df <- data.frame(x = 1:10, y = 1:10, z = rnorm(10))

# Create scatter plot with a continuous variable for the fill aesthetic
ggplot(df, aes(x = x, y = y, fill = z)) +
geom_point()

# this will definitively throw the error

How to Solve this Error.

To resolve this error, you will need to convert the continuous variable to a categorical or discrete variable, either by discretizing it into a set of intervals or by using a color palette that maps the continuous variable to a set of discrete colors.

  1. Discretizing the continuous variable:
# Create data frame
df <- data.frame(x = 1:10, y = 1:10, z = rnorm(10))

# Discretize the continuous variable into three intervals
df$z_discrete <- cut(df$z…

--

--

Data Scientist Dude

Data Scientist, Linguist and Autodidact - My mission is to help people understand and use data models.-