我对阿尔伯特开罗的这个情节很感兴趣。
我可以用 ggforce::bspline 充分平滑我的曲线
但是,既然我没有日期轴,我不确定如何在中途更改样条曲线的颜色。
Let's assume that the three points represent the years 1990, 1991 and 1992. And someone got elected on July 1, 1990. I would like to change the color of the spline at this point. 所以曲线从原点到近似 (12, 5.6) 是红色的,然后从 (12, 5.6) 到 (17,4) 是蓝色的
我不知道如何做到这一点。
library(ggforce)
library(ggplot2)
data <- tibble (
x = c(10, 15, 17),
y = c(5, 7, 4)
)
ggplot(data) +
stat_bspline2(aes(x = x, y = y), n = 300, geom = "bspline0", color = "red") +
stat_bspline2(aes(x = x, y = y), n = 3, geom = "point", color = "red") +
geom_point(aes(x = x, y = y), color = "grey")
想想 MA 告诉我的关于组的内容,我现在拥有的代码可以:
改变直线段的颜色:
# Works for straight lines
ggplot(data, aes(x=x, y=y, colour = g, group = 1)) +
geom_line(size = 3) +
geom_point() +
scale_color_manual(values = c("A" = "red", "B" = "pink", "C" = "green", "D" = "white"))
以及 bspline 的连续颜色。但我希望这只是上图中的离散颜色。
# Works with continuous color
ggplot(data, aes(x=x, y=y, colour = g, group = 1)) +
geom_bspline2(size = 4, n = 300) +
scale_color_manual(values = c("A" = "red", "B" = "pink", "C" = "green", "D" = "white"))
或者这个错误,“错误:提供给离散比例的连续值”:
ggplot(data) +
stat_bspline2(aes(x = x, y = y, color = ..group.., group = 1), n = 300, geom = "bspline0") +
scale_color_manual(values = c("A" = "red", "B" = "pink", "C" = "green", "D" = "white"))
所以我想知道如何使用 bspline 手动控制离散段的颜色。