"Discrete value supplied to continuous scale" error

I've been plotting one continuous variable as a function of another without difficulty for a project -- until now. I expect

scale_x_continuous(breaks = seq(1, 13, 1))

to produce an x-axis ranging from 1 to 13 with labeled tic marks at each integer position. Instead, I get

Error:
! Discrete value supplied to continuous scale

The data frame containing the values I'm plotting was produced by ezStats, and I'm plotting Mean as a function of CR. Its first few entries look like this:

##              CR      N    Mean       SD        FLSD
## 1   1.81625441696113 8  98.43750  4.4194174 4.361602
## 2   1.85964912280702 8  96.25000  7.4402381 4.361602
## 3   1.88288743059061 8  98.75000  3.5355339 4.361602
## 4   1.90014130946773 8  98.43750  4.4194174 4.361602

A few of the values for CR are integers, with no decimal point -- don't know if that's relevant.

If I omit the scale_x_continuous statement, leaving

  coord_cartesian(xlim = c(1, 13), ylim = c(0, 110))

to affect the axis scaling, I get this:


So it appears that ggplot2 thinks CR is a categorical variable. It plots the first 13 (out of 176) entries and tries to label each tic mark with the value of CR for the associated data point.

I' don't understand why this problem occurs only in this case or how to fix it.

It does look like CR is a character or factor. What is the type of the data? Use the function str() on the data frame. My guess is that it is a character so try something like this:

current_data %>%
    mutate(CR=parse_number(CR)) %>%
   ggplot() ...

I get

Error in `mutate()`:
ℹ In argument: `CR = parse_number(CR)`.
ℹ In group 1: `SubjectID = 2`, `Backlight = 550`, `TargetColor = "AQUA"`.
Caused by error in `parse_vector()`:
! is.character(x) is not TRUE

so CR doesn't appear to be a string. It's calculated like so:

summary_df = summary_df %>%
  group_by(SubjectID, Backlight, TargetColor, BackgroundColor) %>%
  summarise(
    FGlum = xlateFGtoY(TargetColor) * Backlight + 60,
    BGlum = xlateBGtoY(BackgroundColor) * Backlight + 60,
    CR = FGlum / BGlum,
  left_join(groups_df)

where my xlate functions are Case statements that return the correct multiplier, given the color name. I expected CR to be floating point.

I found the problem, shortly after my summarise section:

summary_df$CR = as.factor(summary_df$CR)

Don't recall when or why I inserted that statement, but deleting it cured my problem. Of course.

Thanks for your help. I'm upping my dosage of anti-idiocy medication. :slight_smile:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.