如何使用 `gganimate` 为轴标签设置动画?
How to animate the axis label using `gganimate`?
看到我无法快速找到如何执行此操作的指南,我感到非常惊讶。这是一个例子:
library(ggplot2)
library(gganimate)
library(data.table)
library(magrittr)
dt <- lapply(seq(10), function(i){
mean = i
label = paste0("T = ", i)
dt = data.table(x = seq(0, 50, length.out = 100))
set(dt, j = "y", value = dt[, dlnorm(x, meanlog = log(mean), sdlog = 0.2)])
set(dt, j = "frameN", value = i)
return(dt)
}) %>% rbindlist
print(dt)
p <- ggplot(dt, aes(x = x, y = y)) +
geom_line() +
scale_x_continuous(name = "x", breaks = c(0, 1)) +
transition_manual(frameN)
animate(p)
我希望 scale_x_continuous
的 breaks
和 labels
遵循我自己的定义:
arr_breaks <- c(1, 3, 2, 4, 3, 5, 4, 6, 5, 7)
arr_labels <- paste0(seq(10, 100, 10), " kg")
然后
breaks = arr_breaks[1], labels = arr_labels[1]
帧 1
breaks = arr_breaks[2], labels = arr_labels[2]
帧 2
...
breaks = arr_breaks[10], labels = arr_labels[10]
第 10 帧
无论我怎么做,我都会出错。有什么想法吗?
正如@z-lin 所指出的那样,gganimate 目前尚未设置(据我所知)以使用不同的中断来制作音阶动画。使用 geom 可以非常接近这种效果,并且通过更多的工作,您可能可以根据不断变化的比例进行精确的视觉匹配。
breaks_df <- data.frame(
frameN = c(1:10),
arr_breaks = c(1, 3, 2, 4, 3, 5, 4, 6, 5, 7),
arr_labels = paste0(seq(10, 100, 10), " kg")
)
p <- ggplot(dt, aes(x = x, y = y)) +
geom_segment(data = breaks_df, color = "white",
aes(x = arr_breaks, xend = arr_breaks,
y = -Inf, yend = Inf)) +
geom_text(data = breaks_df, vjust = 3, size = 3.5, color = "gray30",
aes(x = arr_breaks, y = 0, label = arr_labels)) +
geom_line() +
scale_x_continuous(name = "x", breaks = c(0)) +
coord_cartesian(clip = "off") +
transition_manual(frameN)
animate(p, width = 600, height = 250)
看到我无法快速找到如何执行此操作的指南,我感到非常惊讶。这是一个例子:
library(ggplot2)
library(gganimate)
library(data.table)
library(magrittr)
dt <- lapply(seq(10), function(i){
mean = i
label = paste0("T = ", i)
dt = data.table(x = seq(0, 50, length.out = 100))
set(dt, j = "y", value = dt[, dlnorm(x, meanlog = log(mean), sdlog = 0.2)])
set(dt, j = "frameN", value = i)
return(dt)
}) %>% rbindlist
print(dt)
p <- ggplot(dt, aes(x = x, y = y)) +
geom_line() +
scale_x_continuous(name = "x", breaks = c(0, 1)) +
transition_manual(frameN)
animate(p)
我希望 scale_x_continuous
的 breaks
和 labels
遵循我自己的定义:
arr_breaks <- c(1, 3, 2, 4, 3, 5, 4, 6, 5, 7)
arr_labels <- paste0(seq(10, 100, 10), " kg")
然后
breaks = arr_breaks[1], labels = arr_labels[1]
帧 1breaks = arr_breaks[2], labels = arr_labels[2]
帧 2
...
breaks = arr_breaks[10], labels = arr_labels[10]
第 10 帧
无论我怎么做,我都会出错。有什么想法吗?
正如@z-lin 所指出的那样,gganimate 目前尚未设置(据我所知)以使用不同的中断来制作音阶动画。使用 geom 可以非常接近这种效果,并且通过更多的工作,您可能可以根据不断变化的比例进行精确的视觉匹配。
breaks_df <- data.frame(
frameN = c(1:10),
arr_breaks = c(1, 3, 2, 4, 3, 5, 4, 6, 5, 7),
arr_labels = paste0(seq(10, 100, 10), " kg")
)
p <- ggplot(dt, aes(x = x, y = y)) +
geom_segment(data = breaks_df, color = "white",
aes(x = arr_breaks, xend = arr_breaks,
y = -Inf, yend = Inf)) +
geom_text(data = breaks_df, vjust = 3, size = 3.5, color = "gray30",
aes(x = arr_breaks, y = 0, label = arr_labels)) +
geom_line() +
scale_x_continuous(name = "x", breaks = c(0)) +
coord_cartesian(clip = "off") +
transition_manual(frameN)
animate(p, width = 600, height = 250)