切碎 bsplines 并给它们上色
Chop up bsplines and color them
我对 Albert Cairo 的这个情节很感兴趣。
我可以用 ggforce::bspline
充分平滑我的曲线
但是,既然我没有日期轴,我不确定如何在中途更改样条曲线的颜色。
让我们假设三个点代表1990年,1991年和1992年的三年。所以曲线从原点到大约 (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")
在想什么M.A。告诉我有关群组的信息,我现在拥有的代码可以:
改变直线段的颜色:
# 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"))
以及b样条的连续颜色。但我希望这只是上图中的离散颜色。
# 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"))
或者这个错误,"Error: Continuous value supplied to discrete scale" 有:
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 手动控制离散段的颜色。
您可以通过分组来做到这一点:
data <- tibble (
x = c(10, 15, 17, 17, 20, 22),
y = c(5, 7, 4, 4, 0, 5),
g = c("A", "A", "A", "B", "B", "B")
)
ggplot(data) +
stat_bspline2(
aes(x = x, y = y, color = ..group.., group = g),
n = 300, geom = "bspline0") +
scale_colour_gradient(low = "blue", high = "red", guide=FALSE)
编辑:
错误 Continuous value supplied to discrete scale
在这里有点令人困惑。我不知道是否有更简单的方法来获得您想要的东西,但可以使用 scale_colour_gradientn()
来实现。此函数允许将组 g
映射到 n
颜色之间的渐变,因此您希望 n
成为组数。
例如,考虑一个包含四组的更大数据集:
# example data
data <- tibble (
x = c(10, 15, 17, 17, 20, 22, 22, 23, 25, 25, 27, 29),
y = c(5, 7, 4, 4, 0, 5, 5, 6, 5, 5, 4, 5.5),
g = c("A", "A", "A", "B", "B", "B", "C", "C", "C", "D","D","D")
)
您可以使用像 rainbow()
这样的调色板并将渐变的颜色数指定为 4,因为有四个组 A
、B
、C
和 D
.
# use a colour palette:
ggplot(data) +
stat_bspline2(
aes(x = x, y = y, color = ..group.., group = g),
n = 300, size = 1, geom = "bspline0") +
scale_color_gradientn(colours = rainbow(4),
guide = F
)
对于自定义颜色,您可以执行以下操作:
# use custom colors:
ggplot(data, aes(x=x, y=y, color = ..group.., group = g)) +
geom_bspline2(size = 1, n = 300) +
scale_color_gradientn(
colours = c("red", "pink", "green", "white"),
guide = F
)
这使用颜色 red
、pink
、green
和 white
之间的渐变。请注意,颜色的顺序很重要,因为不同的顺序会导致不同的渐变,从而导致不同的组映射。
我对 Albert Cairo 的这个情节很感兴趣。
我可以用 ggforce::bspline
充分平滑我的曲线但是,既然我没有日期轴,我不确定如何在中途更改样条曲线的颜色。
让我们假设三个点代表1990年,1991年和1992年的三年。所以曲线从原点到大约 (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")
在想什么M.A。告诉我有关群组的信息,我现在拥有的代码可以:
改变直线段的颜色:
# 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"))
以及b样条的连续颜色。但我希望这只是上图中的离散颜色。
# 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"))
或者这个错误,"Error: Continuous value supplied to discrete scale" 有:
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 手动控制离散段的颜色。
您可以通过分组来做到这一点:
data <- tibble (
x = c(10, 15, 17, 17, 20, 22),
y = c(5, 7, 4, 4, 0, 5),
g = c("A", "A", "A", "B", "B", "B")
)
ggplot(data) +
stat_bspline2(
aes(x = x, y = y, color = ..group.., group = g),
n = 300, geom = "bspline0") +
scale_colour_gradient(low = "blue", high = "red", guide=FALSE)
编辑:
错误 Continuous value supplied to discrete scale
在这里有点令人困惑。我不知道是否有更简单的方法来获得您想要的东西,但可以使用 scale_colour_gradientn()
来实现。此函数允许将组 g
映射到 n
颜色之间的渐变,因此您希望 n
成为组数。
例如,考虑一个包含四组的更大数据集:
# example data
data <- tibble (
x = c(10, 15, 17, 17, 20, 22, 22, 23, 25, 25, 27, 29),
y = c(5, 7, 4, 4, 0, 5, 5, 6, 5, 5, 4, 5.5),
g = c("A", "A", "A", "B", "B", "B", "C", "C", "C", "D","D","D")
)
您可以使用像 rainbow()
这样的调色板并将渐变的颜色数指定为 4,因为有四个组 A
、B
、C
和 D
.
# use a colour palette:
ggplot(data) +
stat_bspline2(
aes(x = x, y = y, color = ..group.., group = g),
n = 300, size = 1, geom = "bspline0") +
scale_color_gradientn(colours = rainbow(4),
guide = F
)
对于自定义颜色,您可以执行以下操作:
# use custom colors:
ggplot(data, aes(x=x, y=y, color = ..group.., group = g)) +
geom_bspline2(size = 1, n = 300) +
scale_color_gradientn(
colours = c("red", "pink", "green", "white"),
guide = F
)
这使用颜色 red
、pink
、green
和 white
之间的渐变。请注意,颜色的顺序很重要,因为不同的顺序会导致不同的渐变,从而导致不同的组映射。