geom_point工作时,geom_smooth不显示

问题描述

我正在尝试用ggplot2绘制geom_smooth线,但是它没有出现。 geom_point工作正常,但没有收到任何错误消息。

  participantID token language measurement value
1            CC  Teil   German        F2_0   906
2            DD  Teil   German        F2_0  1638
3            FF  Teil   German        F2_0  1781
4            FH  Teil   German        F2_0  1195
5            GG  Teil   German        F2_0  1796
6            HH  Teil   German        F2_0  1695

这3个变量是:测量(N = 21:F2_0,F2_5,F2_10,...,F2_95,F2_100),每次测量的令牌(具有两个级别)。

我的ggplot行是 ggplot(mydata,aes(x=measurement,y=value,color=token))+ geom_point()+ geom_smooth()+labs(title="F2 trajectories")

已经有几个关于此问题的线程,但是它们似乎不适用于我的数据集。 任何帮助将不胜感激。谢谢。

解决方法

我建议采用下一种方法。您正在使用非数字值作为x轴,这就是geom_smooth()不起作用的原因。我建议为x轴创建一个参考值,然后为measurement使用构面。这里的代码:

library(ggplot2)
library(dplyr)
#Id variable and plot
df1 %>% group_by(measurement) %>% mutate(id=1:n()) %>%
  ggplot(aes(x=id,y=value,color=token,group=measurement))+
  geom_point()+
  geom_smooth()+labs(title="F2 trajectories")+
  facet_wrap(.~measurement)

输出:

enter image description here

使用了一些数据:

#Data
df1 <- structure(list(participantID = c("CC","DD","FF","FH","GG","HH"),token = c("Teil","Teil","Teil"
),language = c("German","German","German"),measurement = c("F2_0","F2_0","F2_0"),value = c(906L,1638L,1781L,1195L,1796L,1695L)),class = "data.frame",row.names = c("1","2","3","4","5","6"))