控制 ggparcoord 中的 alpha(来自 GGally 包)

Controlling alpha in ggparcoord (from GGally package)

我正在尝试从 构建(我从中借用了 self-contained 示例和标题灵感)。我正在尝试将透明度单独应用于 ggparcoord 的每一行,或者以某种方式在另一层之上添加两层 ggparcoord。下面提供了问题的详细描述和我为使解决方案起作用而拥有的数据格式。


我有一个包含数千行的数据集,我们称之为 x

library(GGally)
x = data.frame(a=runif(100,0,1),b=runif(100,0,1),c=runif(100,0,1),d=runif(100,0,1))

对这些数据进行聚类后,我还得到了一组 5 行,我们称这个数据集为 y

y = data.frame(a=runif(5,0,1),b=runif(5,0,1),c=runif(5,0,1),d=runif(5,0,1))

为了看到质心 y 覆盖 x 我使用以下代码。首先,我将 y 添加到 x,使 5 行位于最终数据框的底部。这确保 ggparcoord 将它们放在最后,因此位于所有数据之上:

df <- rbind(x,y)

接下来,我按照我提到的问题建议为 df 创建一个新列,这样我就可以用不同的颜色对质心进行着色,从而可以将其与数据区分开来:

df$cluster = "data"
df$cluster[(nrow(df)-4):(nrow(df))] <- "centroids"

最后我画出来了:

p <- ggparcoord(df, columns=1:4, groupColumn=5, scale="globalminmax", alphaLines = 0.99) + xlab("Sample") + ylab("log(Count)")
p + scale_colour_manual(values = c("data" = "grey","centroids" = "#94003C"))

我遇到的问题是从这个阶段开始的。在我的原始数据上,单独绘制 x 不会带来太多洞察力,因为它是大量的线(在这个数据上,这相当于在 x 而不是 df 上使用上面的 ggparcoord:

通过显着减少 alphaLines (0.05),由于线条重叠,我自然可以看到一些簇(这又是 运行 ggparcoord on x 减少 alphaLines):

在第二个图而非第一个图的顶部观察添加到 df 的质心更有意义。

但是,由于所有内容都在单个数据帧上,因此对 alphaLine 应用如此高的值会使质心线消失。我唯一的选择是在 df 上使用 ggparcoord(如上所述)而不减少 alphaValue:

我的目标是让红线(质心线)位于第二个图形的顶部,具有非常低的 alpha。到目前为止,我想到了两种方法但无法正常工作:

(1) 有没有什么方法可以在数据框上创建一个列,类似于为颜色所做的,这样我就可以为每一行指定 alpha 值?

(2) 我最初尝试创建两个不同的 ggparcoords 和 "sum them up" 希望叠加但出现错误。

问题可能包含太多细节,但我认为这可以更好地激发答案的适用性,以满足其他读者的兴趣。

我正在寻找的答案将使用当前格式提供的数据变量并生成我正在寻找的图。重建数据的更好方法也受到欢迎,但首选使用当前结构。

在这种情况下,我认为只使用 ggplot 并自己构建图表会更容易。我们对数据的表示方式稍作调整(我们将其置于长格式),然后制作平行坐标图。我们现在可以将任何属性映射到您喜欢的 cluster

library(dplyr)
library(tidyr)

# I start the same as you
x <- data.frame(a=runif(100,0,1),b=runif(100,0,1),c=runif(100,0,1),d=runif(100,0,1))
y <- data.frame(a=runif(5,0,1),b=runif(5,0,1),c=runif(5,0,1),d=runif(5,0,1))

# I find this an easier way to combine the two data.frames, and have an id column
df <- bind_rows(data = x, centroids = y, .id = 'cluster')
# We need to add id's, so we know which points to connect with a line
df$id <- 1:nrow(df)

# Put the data into long format
df2 <- gather(df, 'column', 'value', a:d)

# And plot:
ggplot(df2, aes(column, value, alpha = cluster, color = cluster, group = id)) +
  geom_line() +
  scale_colour_manual(values = c("data" = "grey", "centroids" = "#94003C")) +
  scale_alpha_manual(values = c("data" = 0.2, "centroids" = 1)) +
  theme_minimal()