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

时间:2019-11-04 15:35:26

标签: r ggplot2 graph confidence-interval

我想使用上限和下限列为ggplot创建一个“灰色” 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()

任何见识将不胜感激。

1 个答案:

答案 0 :(得分: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

相关问题