首页 > 解决方案 > 使用 R 平滑绘图并添加置信区间(ggplot2 上的平滑和 geom_ribbon 函数)

问题描述

我有一个名为“SP”的数值变量和一个名为“DELAY”的序数变量,有 7 个条件(0.1;0.2;...;1.2)。对于我的每个观察,有 7 个 SP 值,每个延迟条件有 1 个。每个观察也属于一个组,1、2 或 3 个出现在变量“集群”中。您可以在代码部分看到我的数据示例。

我想制作一个图,它将根据每个延迟条件(x 轴)表示 SP 值(y 轴)。它还需要为每个组(集群)显示单独的行。

我的问题是(ggplot2 包):

我认为这来自我在制作情节之前汇总数据的方式,但我真的很难一开始就拥有正确的情节,因为没有任何效果......

有人能解决这些问题吗?很抱歉,我没有足够的“声誉”来发布图片,所以您需要运行代码才能真正看到这些图。

预先感谢您的友好回答!

``` r
# LIBRARIES
library("dplyr")
library("ggplot2")
library("hrbrthemes")

# DATAFRAME
dataSP <- tibble::tribble(
  ~Participant, ~DELAY, ~SP, ~clusters,
  1L, 0.1, 0.788888888888889, 1L,
  1L, 0.2, 0.566666666666667, 1L,
  1L, 0.4, 0.711111111111111, 1L,
  1L, 0.6, 0.8, 1L,
  1L, 0.8, 0.833333333333333, 1L,
  1L, 1, 0.844444444444444, 1L,
  1L, 1.2, 0.866666666666667, 1L,
  2L, 0.1, 0.433333333333333, 1L,
  2L, 0.2, 0.422222222222222, 1L,
  2L, 0.4, 0.488888888888889, 1L,
  2L, 0.6, 0.644444444444444, 1L,
  2L, 0.8, 0.688888888888889, 1L,
  2L, 1, 0.577777777777778, 1L,
  2L, 1.2, 0.611111111111111, 1L,
  3L, 0.1, 0.711111111111111, 1L,
  3L, 0.2, 0.633333333333333, 1L,
  3L, 0.4, 0.611111111111111, 1L,
  3L, 0.6, 0.655555555555556, 1L,
  3L, 0.8, 0.622222222222222, 1L,
  3L, 1, 0.655555555555556, 1L,
  3L, 1.2, 0.644444444444444, 1L,
  4L, 0.1, 0.888888888888889, 3L,
  4L, 0.2, 0.9, 3L,
  4L, 0.4, 0.955555555555556, 3L,
  4L, 0.6, 0.877777777777778, 3L,
  4L, 0.8, 0.855555555555556, 3L,
  4L, 1, 0.922222222222222, 3L,
  4L, 1.2, 0.877777777777778, 3L,
  5L, 0.1, 0.722222222222222, 1L,
  5L, 0.2, 0.577777777777778, 1L,
  5L, 0.4, 0.5, 1L,
  5L, 0.6, 0.588888888888889, 1L,
  5L, 0.8, 0.644444444444444, 1L,
  5L, 1, 0.7, 1L,
  5L, 1.2, 0.711111111111111, 1L,
  6L, 0.1, 0.844444444444444, 3L,
  6L, 0.2, 0.777777777777778, 3L,
  6L, 0.4, 0.744444444444444, 3L,
  6L, 0.6, 0.855555555555556, 3L,
  6L, 0.8, 0.888888888888889, 3L,
  6L, 1, 0.8, 3L,
  6L, 1.2, 0.955555555555556, 3L,
  7L, 0.1, 0.533333333333333, 1L,
  7L, 0.2, 0.688888888888889, 1L,
  7L, 0.4, 0.688888888888889, 1L,
  7L, 0.6, 0.733333333333333, 1L,
  7L, 0.8, 0.844444444444444, 1L,
  7L, 1, 0.8, 1L,
  7L, 1.2, 0.766666666666667, 1L,
  8L, 0.1, 0.455555555555556, 1L,
  8L, 0.2, 0.322222222222222, 1L,
  8L, 0.4, 0.511111111111111, 1L,
  8L, 0.6, 0.788888888888889, 1L,
  8L, 0.8, 0.733333333333333, 1L,
  8L, 1, 0.733333333333333, 1L,
  8L, 1.2, 0.777777777777778, 1L,
  9L, 0.1, 0.388888888888889, 2L,
  9L, 0.2, 0.166666666666667, 2L,
  9L, 0.4, 0.466666666666667, 2L,
  9L, 0.6, 0.477777777777778, 2L,
  9L, 0.8, 0.533333333333333, 2L,
  9L, 1, 0.633333333333333, 2L,
  9L, 1.2, 0.544444444444444, 2L,
  10L, 0.1, 0.477777777777778, 1L,
  10L, 0.2, 0.511111111111111, 1L,
  10L, 0.4, 0.555555555555556, 1L,
  10L, 0.6, 0.633333333333333, 1L,
  10L, 0.8, 0.633333333333333, 1L,
  10L, 1, 0.7, 1L,
  10L, 1.2, 0.6, 1L,
  11L, 0.1, 0.644444444444444, 1L,
  11L, 0.2, 0.544444444444444, 1L,
  11L, 0.4, 0.666666666666667, 1L,
  11L, 0.6, 0.7, 1L,
  11L, 0.8, 0.722222222222222, 1L,
  11L, 1, 0.688888888888889, 1L,
  11L, 1.2, 0.7, 1L,
  12L, 0.1, 0.488888888888889, 1L,
  12L, 0.2, 0.477777777777778, 1L,
  12L, 0.4, 0.566666666666667, 1L,
  12L, 0.6, 0.655555555555556, 1L,
  12L, 0.8, 0.611111111111111, 1L,
  12L, 1, 0.633333333333333, 1L,
  12L, 1.2, 0.555555555555556, 1L,
  13L, 0.1, 0.522222222222222, 2L,
  13L, 0.2, 0.311111111111111, 2L,
  13L, 0.4, 0.5, 2L,
  13L, 0.6, 0.577777777777778, 2L,
  13L, 0.8, 0.533333333333333, 2L,
  13L, 1, 0.611111111111111, 2L,
  13L, 1.2, 0.688888888888889, 2L,
  14L, 0.1, 0.544444444444444, 1L,
  14L, 0.2, 0.377777777777778, 1L,
  14L, 0.4, 0.6, 1L,
  14L, 0.6, 0.7, 1L,
  14L, 0.8, 0.722222222222222, 1L,
  14L, 1, 0.688888888888889, 1L,
  14L, 1.2, 0.7, 1L,
  15L, 0.1, 0.822222222222222, 3L,
  15L, 0.2, 0.777777777777778, 3L,
  15L, 0.4, 0.822222222222222, 3L,
  15L, 0.6, 0.7, 3L,
  15L, 0.8, 0.8, 3L,
  15L, 1, 0.855555555555556, 3L,
  15L, 1.2, 0.744444444444444, 3L,
  16L, 0.1, 0.733333333333333, 1L,
  16L, 0.2, 0.588888888888889, 1L,
  16L, 0.4, 0.633333333333333, 1L,
  16L, 0.6, 0.744444444444444, 1L,
  16L, 0.8, 0.777777777777778, 1L,
  16L, 1, 0.744444444444444, 1L,
  16L, 1.2, 0.722222222222222, 1L,
  17L, 0.1, 0.666666666666667, 2L,
  17L, 0.2, 0.277777777777778, 2L,
  17L, 0.4, 0.411111111111111, 2L,
  17L, 0.6, 0.4, 2L,
  17L, 0.8, 0.611111111111111, 2L,
  17L, 1, 0.588888888888889, 2L,
  17L, 1.2, 0.677777777777778, 2L
)


# CODE

# converting group variable "clusters" into characters (to use of a discrete color scale)
clust <- as.character(dataSP$clusters)
dataSP <- cbind(dataSP, clust)

# Generating the plot
ggplot(dataSP) +
  aes(x = DELAY, group = clust, color = clust, y = SP) +
  stat_summary(fun = mean, geom = "point") +
  stat_summary(fun = mean, geom = "smooth", aes(colour = clust)) +
  scale_color_manual(values = c("#999999", "#E69F00", "#56B4E9")) +
  theme_ipsum()


``` r

# TRIED SOLUTIONS

SP.lo <- min(dataSP$SP)
SP.up <- max(dataSP$SP)

ggplot(dataSP) +
  aes(x = DELAY, group = clust, color = clust, y = SP, ymin = SP.lo, ymax = SP.up) +
  stat_summary(fun = mean, geom = "point") +
  stat_summary(fun = mean, geom = "smooth", aes(colour = clust)) +
  geom_ribbon(alpha = 0.5) +
  scale_color_manual(values = c("#999999", "#E69F00", "#56B4E9")) +
  theme_ipsum()

```

<sup>Created on 2021-07-01 by the [reprex package](https://reprex.tidyverse.org) (v2.0.0)</sup>

标签: rggplot2smoothinggeom-ribbon

解决方案


推荐阅读