简体   繁体   English

提供给连续刻度的离散值

[英]discrete values supplied to continuous scale

I am plotting a linear mixed effects model using ggplot2 in R.我正在使用 R 中的 ggplot2 绘制线性混合效果 model。 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.我已将问题定位到geom_point ,因为geom_linegeom_ribbon工作得很好。 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 geom_point读取下面的数据

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.如您所见,condition 的选项是 1 或 2,但 R 似乎将它们读取为连续的。 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!谢谢!

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:如评论中所述,您的数据中的condition是 integer 因此比例是连续的,您可以通过调用as.factor()根据您的条件轻松更改:

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)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM