【发布时间】:2020-10-31 07:15:03
【问题描述】:
我在使用 gganimate 时遇到了困难,我将不胜感激。
我对两个连续变量之间的关系在因子变量的两个值之间有何不同感兴趣。想象一下,比如说,我对在给定干预前后考试成绩和身高之间的关系如何变化感兴趣。我的数据大致有以下结构:
set.seed(24601)
height <- rnorm(10, 0, 1)
test_data <-
tibble(height = rep(height, 2),
test_scores = rnorm(20, 0, 1),
before_or_after = c(rep("before", 10), rep("after", 10)),
id = rep(c("a", "b", "c", "d", "e", "f", "g", "h", "i", "j"), 2))
如果我想在静态 ggplot 中显示它,我可以这样做:
ggplot(data = test_data) +
aes(x = height,
y = test_scores,
colour = before_or_after) +
geom_point() +
geom_smooth(method = "lm", se = FALSE)
但我有兴趣关注干预前后测试分数的变化,因此希望为每个观察和 geom_smooth() 设置动画。
我可以按如下方式为每个观察设置动画:
ggplot(data = test_data) +
aes(x = height,
y = test_scores,
group = id) +
geom_point() +
transition_states(before_or_after)
但理想情况下,我会执行以下操作:
ggplot(data = test_data) +
aes(x = height,
y = test_scores,
group = id) +
geom_point() +
geom_smooth(method = "lm",
se = FALSE) +
transition_states(before_or_after)
geom_smooth 沿着点移动。但是,当我这样做时,我会收到错误消息
Error in `$<-.data.frame`(`*tmp*`, "group", value = "") :
replacement has 1 row, data has 0
如果有人能告诉我如何解决这个问题,我将不胜感激!
【问题讨论】: