如何更改 ggPlot 中回归线的颜色?
How do I change the color of the regression lines in ggPlot?
我做了一个回归的可视化。目前这就是图表的样子。
很难看到回归线,因为它们与散点图点的颜色相同。
我的问题是,如何使回归线与散点图点的颜色不同?
这是我的代码:
(ggplot(data=df, mapping=aes(x='score', y='relent',
color='factor(threshold)'))+
geom_point()+
scale_color_manual(values=['darkorange', 'purple'])+
geom_smooth(method='lm',
formula = 'y ~ x+I(x**2)',se=False, )+
geom_vline(xintercept = 766, color = "red", size = 1, linetype = "dashed")+
labs(y = "Yield",
x = "Score")+
theme_bw()
)
比较:
iris %>%
ggplot(aes(Petal.Length, Sepal.Width, color = Species)) +
geom_point() +
geom_smooth(method = "lm", aes(group = Species))
有:
iris %>%
ggplot(aes(Petal.Length, Sepal.Width)) +
geom_point(aes(color = Species)) +
geom_smooth(method = "lm", aes(group = Species))
当在 ggplot()
中指定 aes(color = ...)
时,它会应用于后续的两个 geom。将其移动到 geom_point()
仅适用于点。
实现所需结果的一种选择是使用不同的值“复制”您的 threshold
列,例如在下面的代码中,我将 0 映射到 2,将 1 映射到 3。然后可以将这个重复的列映射到 geom_smooth
内的 color
aes 上,并允许为回归线设置不同的颜色。
我下面的代码使用 R
或 ggplot2
但 TBMK 代码可以很容易地适应 plotnine
:
n <- 1000
df <- data.frame(
relent = c(runif(n, 100, 200), runif(n, 150, 250)),
score = c(runif(n, 764, 766), runif(n, 766, 768)),
threshold = c(rep(0, n), rep(1, n))
)
df$threshold_sm <- c(rep(2, n), rep(3, n))
library(ggplot2)
p <- ggplot(data = df, mapping = aes(x = score, y = relent, color = factor(threshold))) +
scale_color_manual(values = c("darkorange", "purple", "blue", "green")) +
geom_vline(xintercept = 766, color = "red", size = 1, linetype = "dashed") +
labs(
y = "Yield",
x = "Score"
) +
theme_bw()
p +
geom_point() +
geom_smooth(aes(color = factor(threshold_sm)),
method = "lm",
formula = y ~ x + I(x**2), se = FALSE
)
第二种选择是为点添加一些透明度,使线条更清晰,顺便处理点的过度绘制:
p +
geom_point(alpha = .3) +
geom_smooth(aes(color = factor(threshold)),
method = "lm",
formula = y ~ x + I(x**2), se = FALSE
) +
guides(color = guide_legend(override.aes = list(alpha = 1)))
我做了一个回归的可视化。目前这就是图表的样子。
很难看到回归线,因为它们与散点图点的颜色相同。
我的问题是,如何使回归线与散点图点的颜色不同?
这是我的代码:
(ggplot(data=df, mapping=aes(x='score', y='relent',
color='factor(threshold)'))+
geom_point()+
scale_color_manual(values=['darkorange', 'purple'])+
geom_smooth(method='lm',
formula = 'y ~ x+I(x**2)',se=False, )+
geom_vline(xintercept = 766, color = "red", size = 1, linetype = "dashed")+
labs(y = "Yield",
x = "Score")+
theme_bw()
)
比较:
iris %>%
ggplot(aes(Petal.Length, Sepal.Width, color = Species)) +
geom_point() +
geom_smooth(method = "lm", aes(group = Species))
有:
iris %>%
ggplot(aes(Petal.Length, Sepal.Width)) +
geom_point(aes(color = Species)) +
geom_smooth(method = "lm", aes(group = Species))
当在 ggplot()
中指定 aes(color = ...)
时,它会应用于后续的两个 geom。将其移动到 geom_point()
仅适用于点。
实现所需结果的一种选择是使用不同的值“复制”您的 threshold
列,例如在下面的代码中,我将 0 映射到 2,将 1 映射到 3。然后可以将这个重复的列映射到 geom_smooth
内的 color
aes 上,并允许为回归线设置不同的颜色。
我下面的代码使用 R
或 ggplot2
但 TBMK 代码可以很容易地适应 plotnine
:
n <- 1000
df <- data.frame(
relent = c(runif(n, 100, 200), runif(n, 150, 250)),
score = c(runif(n, 764, 766), runif(n, 766, 768)),
threshold = c(rep(0, n), rep(1, n))
)
df$threshold_sm <- c(rep(2, n), rep(3, n))
library(ggplot2)
p <- ggplot(data = df, mapping = aes(x = score, y = relent, color = factor(threshold))) +
scale_color_manual(values = c("darkorange", "purple", "blue", "green")) +
geom_vline(xintercept = 766, color = "red", size = 1, linetype = "dashed") +
labs(
y = "Yield",
x = "Score"
) +
theme_bw()
p +
geom_point() +
geom_smooth(aes(color = factor(threshold_sm)),
method = "lm",
formula = y ~ x + I(x**2), se = FALSE
)
第二种选择是为点添加一些透明度,使线条更清晰,顺便处理点的过度绘制:
p +
geom_point(alpha = .3) +
geom_smooth(aes(color = factor(threshold)),
method = "lm",
formula = y ~ x + I(x**2), se = FALSE
) +
guides(color = guide_legend(override.aes = list(alpha = 1)))