【问题标题】:geom_smooth() doesn't show while geom_point() worksgeom_smooth() 在 geom_point() 工作时不显示
【发布时间】:2020-09-07 17:52:51
【问题描述】:

我正在尝试使用 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)、每个测量的token(有两个级别)。

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

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

【问题讨论】:

  • 看起来您的 x 变量是分类变量。鉴于此,这种关系应该是什么样的?分类变量是否应该被视为序数或数字或其他?这可能会帮助您确定平滑是否 a.、合理和 b.、可行。我搜索了“geom_smooth categorical”,this answer 也可能对您有用。

标签: r ggplot2 smoothing


【解决方案1】:

我会建议下一个方法。您将非数值用作 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)

输出:

使用的一些数据:

#Data
df1 <- structure(list(participantID = c("CC", "DD", "FF", "FH", "GG", 
"HH"), token = c("Teil", "Teil", "Teil", "Teil", "Teil", "Teil"
), language = c("German", "German", "German", "German", "German", 
"German"), measurement = c("F2_0", "F2_0", "F2_0", "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"))

【讨论】:

  • 谢谢!它通过在 geom_smooth 的 aes() 中添加 group=token 来工作
猜你喜欢
  • 1970-01-01
  • 2022-11-27
  • 2023-04-06
  • 2013-05-09
  • 1970-01-01
  • 1970-01-01
  • 2017-11-21
  • 1970-01-01
  • 2021-10-24
相关资源
最近更新 更多