调整 geom_point 的颜色以反映样本均值的差异?

Adjusting color of geom_point to reflect difference in sample means?

我正在尝试用 R 中的 ggplot 可视化我的配对 t 检验。我想使用 ggplot + geom_point() 将两个样本相互绘制,但我想调整点的颜色显示样本均值差异的严重性。有了这个,我的意思是如果该点在配对群体的均值上有 0 差异,它将位于回归线上并且只是一个黑点。如果该点有很大的差异并且位于图形的左上或右下象限,远离回归线,我希望它是红色的。在梯度尺度上。我希望这是有道理的!我将附上我的图表图片以显示:

现在我唯一的美学是 alpha,所以点更明显。

如果对此有任何帮助,那就太好了!谢谢!

你没有给我们任何样本数据来处理,所以我模拟了你的近似值:

set.seed(69)
measurement1 <- 1:1500
measurement2 <- 1:1500 + c(rnorm(50, 1200, 50), rnorm(1400, 0, 99), rnorm(50, -1400, 50))

df <- data.frame(measurement1, measurement2)
df <- df[df$measurement1 > 0 & df$measurement2 > 0, ]

所以现在我们只需要将每对测量值之间的绝对差异作为我们的着色变量:

library(ggplot2)

ggplot(df, aes(measurement1, measurement2)) +
  geom_line(aes(y = measurement1), size = 2, colour = "gold") +
  geom_point(aes(colour = abs(measurement2 - measurement1))) +
  scale_colour_gradient(low = "orange", high = "red", name = "Difference") +
  labs(y = "Adjusted.Function.Points", x = "Functional.Size")

reprex package (v0.3.0)

于 2020 年 6 月 8 日创建