R:填充多行之间的区域,没有明确的最大和最小行

时间:2017-11-02 13:06:58

标签: r ggplot2

我的数据框df如下所示。 x(10,20,30,40,50)有五个不同的值,settings有四个不同的值。

 x                   settings   coherence_mean
10      alpha=0.05, beta=0.01        -119.1121
10       alpha=0.1, beta=0.01        -118.5555
10       alpha=0.05, beta=0.1        -119.3970
10        alpha=0.1, beta=0.1        -118.6293
20      alpha=0.05, beta=0.01        -127.0716
20       alpha=0.1, beta=0.01        -127.0320

我创建了一个线图,其中数据按settings列分组:

ggplot(data = df, aes(x=t, y=coherence_mean, group = settings)) + 
  geom_line(aes(colour=settings))

enter image description here

如何在所有线的最大值和最小值之间填充区域?

2 个答案:

答案 0 :(得分:2)

最简单的方法是创建第二个数据框,汇总数据集。

以下是通用数据集的示例:

set.seed(123)
df <- data.frame(t = c(rep(10,4), rep(20,4), rep(30,4)), settings = rep(c("Group 1", "Group 2", "Group 3", "Group 4"),3), mean = rnorm (12, 10))

这看起来像:

    t settings      mean
1  10  Group 1  9.439524
2  10  Group 2  9.769823
3  10  Group 3 11.558708
4  10  Group 4 10.070508
5  20  Group 1 10.129288
6  20  Group 2 11.715065
7  20  Group 3 10.460916
8  20  Group 4  8.734939
9  30  Group 1  9.313147
10 30  Group 2  9.554338
11 30  Group 3 11.224082
12 30  Group 4 10.359814

aggregate函数可用于按组汇总列表:

# Find range
rangemin <- aggregate(df$mean, by = list(df$t), min)
rangemax <- aggregate(df$mean, by = list(df$t), max)
range <- merge(rangemin, rangemax, by = "Group.1")
names(range) <- c("Group", "min", "max")

绘制结果。

ggplot() + 
  geom_ribbon(data = range, aes(ymin = min, ymax = max, x = Group), fill = "skyblue2", alpha = 0.5) +
  geom_line(data = df, aes(x=t, y=mean, group = settings, colour=settings))

enter image description here

答案 1 :(得分:0)

我觉得这是另一个答案。之前答案的问题在于它没有正确地遵循这些路线。这是因为它取得了10,20和30点的最大值。这意味着如果中途相互拦截,公式就不会跟踪线。

为了使一条线更好地跟踪最小值,我们需要插入点:

library(data.table)
library(ggplot2)    

set.seed(123)
df <- data.frame(t = c(rep(10,4), rep(20,4), rep(30,4)), settings = rep(c("Group 1", "Group 2", "Group 3", "Group 4"),3), mean = rnorm (12, 10))


# Creates a new, interpolated dataset
df_dt <- data.table(df)
df_dt_int <- df_dt[, approx(x = t,y = mean, xout=seq(from = 10, to = 30, by = 0.1)), by= settings]

# Calculates the range of the interpolated dataset
rangemin <- aggregate(df_dt_int$y, by = list(df_dt_int$x), min)
rangemax <- aggregate(df_dt_int$y, by = list(df_dt_int$x), max)
range <- merge(rangemin, rangemax, by = "Group.1")
names(range) <- c("Group", "min", "max")
range

ggplot() + 
  geom_ribbon(data = range, aes(ymin = min, ymax = max, x = Group), fill = "skyblue2", alpha = 0.5) +
  geom_line(data = df, aes(x=t, y=mean, group = settings, colour=settings))

enter image description here

如您所见,该线现在正确地跟随图的外部。当曲线插值时,它仍然有可能在它穿过的确切点处存在不准确性。但是,通过减小seq(from = 10, to = 30, by = *CHANGE THIS*)

中的近似值的步长,可以在很大程度上避免这种情况