gganimate 改变比例(轴限制)

Meg*_*ron 6 r axes gganimate

我想使用 来创建一个 gif gganimate,但我的轴范围在一帧中变化很大。这导致所有后续帧都被压缩。

ggplot2某些方面,有一个选项可以让scales="free". 有没有办法在 的每一帧中都有自由比例gganimate

下面是一个例子:

library(gapminder)
library(ggplot2)
library(gganimate)
theme_set(theme_bw())

p <- ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, color = continent,
                           frame = year)) +
  geom_point() +
  scale_x_log10()

gganimate(p)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

现在我们将其中一个数据点移动到某个极值。这会挤压所有后续未受影响帧中的点。

gapminder[1, "lifeExp"] <- 1000
gapminder[1, "gdpPercap"] <- 1e60

p <- ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, color = continent, 
                           frame = year)) +
  geom_point() +
  scale_x_log10()

gganimate(p)  # smooshed
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

Rom*_*man 7

您可以尝试使用view_follow().

1

代码

p <- ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, color = continent)) +
    geom_point() +
    labs(title = 'Year: {frame_time}', x = 'GDP per capita', y = 'life expectancy') +
    transition_time(year) +
    view_follow()

animate(p)
Run Code Online (Sandbox Code Playgroud)

  • 我建议用 view_zoom() 替换 view_follow() 。它为时间序列数据产生更好的结果。 (3认同)