为不同的数据添加单独的geom_smooth

问题描述 投票:0回答:1

我想使用上限和下限列为ggplot创建一个'gray'geom_smooth(或其他变体)。这是数据:

data <- read.table(text="
Student    Month       TestScore    LowestScore     HighestScore    MeanTest
001           1        65           44              97              77
001           2        75           38              92              78
001           3        80           58              99              79
002           1        83           44              97              77
002           2        76           38              92              78
002           3        85           58              99              79", header=TRUE)

因此,我想每月绘制受试者的考试成绩,然后在背景中为总考试成绩设置单独的“灰色”置信度带(不是这两个受试者)。

到目前为止,我对这两个主题有以下情节:

ggplot(data = data, aes(x = Month, y = TestScore, group = TestScore, color = Student)) + 
  geom_point() + 
  geom_line() + 
  theme_bw()

任何见识将不胜感激。

r ggplot2 graph confidence-interval
1个回答
0
投票

您描述的不是置信区间,而是每个月的数据范围。如果要以geom_smooth的置信区间样式绘制此范围,则可以执行以下操作:(我不确定您建议的绘图中按TestScore进行的分组应该做什么。)

ggplot(data = data, aes(x = Month, y = TestScore, color = Student, group = Student)) + 
  geom_ribbon(aes(ymin = LowestScore, ymax = HighestScore), fill = "grey70", alpha = 0.3, color = NA) +
  geom_point() + 
  geom_line() + 
  theme_bw()

此图的结果:enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.