gpt4 book ai didi

r - 如何在ggplot中为平滑线设置alpha

转载 作者:行者123 更新时间:2023-12-01 09:48:02 28 4
gpt4 key购买 nike

我使用 alpha 为 ggplot 中的平滑线设置透明度,但我只在拟合线周围的误差带中获得透明度。

我的代码如下:

z1 <- rnorm(10)

z2 <- z1 ^ 2

error <- rnorm(10, 0.25)

y <- 1 + 0.5 * z1 + error

data1 <- data.table(y, z1, z2)

ggplot(data1) +
geom_point(aes(x = z1, y = y), color = "blue", size = 3) +
geom_point(aes(x = z2, y = y), color = "red", size = 3) +
geom_smooth(method = lm, aes(x = z1, y = y), color = "blue", size = 2, alpha = 0.1) +
geom_smooth(method = lm, aes(x = z2, y = y), color = "red", size = 2, alpha = 0.1)

输出是这样的: enter image description here

如何明确设置回归线的透明度?

您的建议将不胜感激。

最佳答案

您可以使用 geom_line 对回归线进行更细粒度的控制。


ggplot(data1) +
geom_point(aes(x = z1, y = y), color = "blue", size = 3) +
geom_point(aes(x = z2, y = y), color = "red", size = 3) +
geom_line(stat = "smooth", method = lm, aes(x = z1, y = y), color = "blue", size = 2, alpha = 0.1) +
geom_line(stat = "smooth", method = lm, aes(x = z2, y = y), color = "red", size = 2, alpha = 0.1) +
geom_smooth(method = lm, aes(x = z1, y = y), color = NA, size = 2, alpha = 0.1) +
geom_smooth(method = lm, aes(x = z2, y = y), color = NA, size = 2, alpha = 0.1)

关于r - 如何在ggplot中为平滑线设置alpha,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44484868/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com