Description
When working with very complex plots, with many different values, mappings, and scales, it often happens that I mix something up and accidentally supply a continuous value to a discrete scale, or vice versa. In this case, while ggplot does tell me which way the mix-up occurred, it does not tell me which value was specifically wrongly supplied to which scale. Instead, I have to figure this out myself, often by frustrating trial-and-error if it is not immediately obvious from the code.
I would love it if the error message included:
- the name of the scale which a continuous/discrete value was wrongly supplied to,
- the aesthetic which is mapped to this scale,
- and the value which is mapped to this aesthetic.
- Possibly even more useful information that is available internally, that I am not aware of?
Here is some reproducible code to illustrate my problem:
library(tidyverse)
set.seed(0)
df = tibble(col1 = rnorm(6) * 100,
col2 = factor(c(100, 100, 101, 101, 102, 102)),
col3 = rnorm(6),
col4 = factor(c(1, 2, 1, 3, 5, 2)))
df %>% ggplot(mapping = aes(x = col1, y = col2, fill = col3, colour = col4)) +
geom_point(pch = 21, size = 10, stroke = 3) +
scale_fill_viridis_d(option = "viridis") +
scale_colour_viridis_d(option = "magma")
When I run this, I get the dreaded:
Error: Continuous value supplied to discrete scale
However, finding out which scale would be changed—in this case, to be continuous—requires reading and understanding the code. (Of course, in this case, it is the fill
scale, but can you see this at first glance?) It is even harder to do if:
- the plot is assembled at distant code locations; for example when a "barebones" plot only with geoms is constructed, and then "finished off" with scales/labs/etc. further down in the script; especially if the same plot can be "finished off" in multiple different ways
- the same aesthetic has multiple values mapped to multiple scales (using something like
ggnewscale
) - there are scales involved where it is not immediately obvious whether they are discrete or continuous, particularly if they are supplied by another external module
- binned scales are involved
- you are new to
ggplot2
and don't even know what the error message is trying to tell you in the first place... - and so on
Now my simple suggestion would be to amend the error message, so it reads for example:
Error: Continuous value (col3) supplied to discrete scale ("viridis") for aesthetic "fill"
I'm sure the exact wording can be improved; this is just an idea. I don't know how much effort this would be to implement, but it would certainly improve my quality of life a lot, and perhaps even that of others.