当 x 和 y 不变时,如何使 geom_bar 颜色随 gganimate 变化?
How do I make geom_bar colors change with gganimate when x and y are constant?
我是 gganimate
的新手,很难弄清楚如何做到这一点。
我想通过动画颜色过渡来显示变量在两个不同级别的分布。我想通过让 narrow level
在相同时间内过渡比 wider level
更小的颜色范围来展示这一点。这可能吗?
这是我到目前为止的可重现示例。
library(ggplot2)
df <- data.frame(x = rep(c("narrow", "wide"), each = 1000),
y = c(sort(runif(n = 1000, min = 6, max = 7)),
sort(runif(n = 1000, min = 3, 10))),
animate.time = c(1:1000))
ggplot(data = df, aes(x = x, fill = y)) +
geom_bar()
由 reprex package (v2.0.0)
创建于 2021-06-12
作为其中的一部分,每个条使用的色标应该相同,但是 narrow
x 应该占据比 wide
x 更窄的颜色范围。
我使用 geom_bar
因为我不想改变可视化的形状,但是我不确定如何继续。我尝试添加 animate.time
这样 gganimate
就会有一些东西可以通过,但是我不知道我应该如何根据 y
.[=23 改变颜色=]
如有任何指点,我将不胜感激。
根据 this.
,有一种更简单的方法可以做到这一点
library(colorspace)
library(gganimate)
library(ggplot2)
# Narrow Visualization
narrow<-data.frame(x = rep(10,10),
state_cont=rep(1:2, each = 5))
ggplot(data = narrow, aes(x = x, fill = state_cont)) +
geom_bar() +
colorspace::scale_fill_continuous_sequential(palette = "Reds 3", begin = 0.4, end = .6) +
transition_states(states = state_cont) + theme_void() +
theme(legend.position = "none")
# Wide Visualization
wide <- data.frame(x=rep(10,10),
state_cont=rep(1:2, each = 5))
ggplot(data = wide, aes(x = x, fill = state_cont)) +
geom_bar() +
colorspace::scale_fill_continuous_sequential(palette = "Reds 3", begin = 0, end = 1) +
transition_states(states = state_cont) + theme_void() + theme(legend.position = "none")
解释是,对于这个特定的问题,您不想设置它应该摆动的每个值,通过简单地在 scale_fill_continuous_sequential()
中设置边界会容易得多begin
和 end
参数。然后,gganimate
会根据state_cont
自动循环
我是 gganimate
的新手,很难弄清楚如何做到这一点。
我想通过动画颜色过渡来显示变量在两个不同级别的分布。我想通过让 narrow level
在相同时间内过渡比 wider level
更小的颜色范围来展示这一点。这可能吗?
这是我到目前为止的可重现示例。
library(ggplot2)
df <- data.frame(x = rep(c("narrow", "wide"), each = 1000),
y = c(sort(runif(n = 1000, min = 6, max = 7)),
sort(runif(n = 1000, min = 3, 10))),
animate.time = c(1:1000))
ggplot(data = df, aes(x = x, fill = y)) +
geom_bar()
由 reprex package (v2.0.0)
创建于 2021-06-12作为其中的一部分,每个条使用的色标应该相同,但是 narrow
x 应该占据比 wide
x 更窄的颜色范围。
我使用 geom_bar
因为我不想改变可视化的形状,但是我不确定如何继续。我尝试添加 animate.time
这样 gganimate
就会有一些东西可以通过,但是我不知道我应该如何根据 y
.[=23 改变颜色=]
如有任何指点,我将不胜感激。
根据 this.
,有一种更简单的方法可以做到这一点library(colorspace)
library(gganimate)
library(ggplot2)
# Narrow Visualization
narrow<-data.frame(x = rep(10,10),
state_cont=rep(1:2, each = 5))
ggplot(data = narrow, aes(x = x, fill = state_cont)) +
geom_bar() +
colorspace::scale_fill_continuous_sequential(palette = "Reds 3", begin = 0.4, end = .6) +
transition_states(states = state_cont) + theme_void() +
theme(legend.position = "none")
# Wide Visualization
wide <- data.frame(x=rep(10,10),
state_cont=rep(1:2, each = 5))
ggplot(data = wide, aes(x = x, fill = state_cont)) +
geom_bar() +
colorspace::scale_fill_continuous_sequential(palette = "Reds 3", begin = 0, end = 1) +
transition_states(states = state_cont) + theme_void() + theme(legend.position = "none")
解释是,对于这个特定的问题,您不想设置它应该摆动的每个值,通过简单地在 scale_fill_continuous_sequential()
中设置边界会容易得多begin
和 end
参数。然后,gganimate
会根据state_cont