discrete values supplied to continuous scale

Viewed 29

I am plotting a linear mixed effects model using ggplot2 in R. I keep receiving this error with regards to including the mean rating per trial.

Error: Continuous value supplied to discrete scale

I have localized the problem to geom_point as geom_line and geom_ribbon work just fine. Here is the code I am currently using

p2 <- ggplot(td_mean_pref_plot_groups, aes(x, td_mean_pref_plot_groups$predicted, col = group)) +
      geom_line(size=1.5) +
      scale_color_manual(values = c("blue","red")) +
      geom_ribbon(aes(ymin=conf.low,ymax=conf.high, fill=group),alpha=.2,colour=NA) +
      scale_fill_manual(values = c("blue","red")) +
      geom_point(data=summStats_td_mean,aes(trial,mean, col = condition),size=2) +
      scale_color_manual(values = c("blue","red")) +
      theme_bw() +
      xlab('Trial') +
      ylab('Prediction Error') +
      ylim(1,2.2) +
      ggtitle('TD learning about TD vs. TD \n learning about ASD') +
      theme(text=element_text(size=20),
        plot.title = element_text(hjust = 0.5),
        panel.border = element_blank())
p2

geom_point reads the data below

structure(list(condition = c(1L, 1L, 1L, 1L, 1L, 1L), trial = 1:6, 
    n = c(80L, 93L, 92L, 94L, 94L, 94L), mean = c(1.225, 1.39784946236559, 
    1.25, 1.40425531914894, 1.24468085106383, 1.29787234042553
    ), sd = c(1.01849976541573, 1.08487411558084, 1.00137268424261, 
    1.11025666834073, 1.00199983058361, 1.09573746202196)), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -6L), groups = structure(list(
    condition = 1L, .rows = structure(list(1:6), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L), .drop = TRUE))

As you can see, the options for condition for condition are 1 or 2 but R seems to be reading those as continuous. I have used this code many times before and never run into this issue so I'm not sure why it's suddenly acting up. Thank you!

1 Answers

As mentioned in the comment the condition in your data is an integer therefore the scale in continuous, you can easily change this by calling as.factor() on your condition:

library(ggplot2)
p1 = ggplot()+
  geom_point(data= df, aes(trial, mean, col = condition))+
  labs(subtitle = "Continuous scale")

p2 = ggplot()+
  geom_point(data= df, aes(trial, mean, col = as.factor(condition)))+
  scale_color_manual(values = c("blue","red"))+
  labs(subtitle = "Discrete scale")

library(ggpubr)
ggarrange(p1,p2)

enter image description here

Related