我做了一個回歸的可視化。目前這就是圖表的樣子。

回歸線很難看到,因為它們與散點圖點的顏色相同。
我的問題是,如何使回歸線與散點圖點的顏色不同?
這是我的代碼:
(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()
)
uj5u.com熱心網友回復:
實作您想要的結果的一種選擇是threshold使用不同的值“復制”您的列,例如,在下面的代碼中,我將 0 映射到 2,將 1 映射到 3。然后可以將這個復制的列映射到color內部的aesgeom_smooth并允許設定不同的回歸線的顏色。
我下面的代碼使用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)))

uj5u.com熱心網友回復:
相比:
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))

當在aes(color = ...)中指定時ggplot(),它將應用于后續的兩個幾何體。將其移動到geom_point()僅適用于點。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/371940.html
上一篇:在cowplot/add_sub中,如何對齊多個文本標簽?
下一篇:更改ggplot2中的圖例
