随着尺度变化(轴限)

时间:2016-12-07 19:45:22

标签: r axes gganimate

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

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)

enter image description here

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

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

enter image description here

2 个答案:

答案 0 :(得分:3)

您可以尝试使用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)

答案 1 :(得分:0)

要手动定义比例,请参见view_stepview_step_manual(另请参见view_zoomview_zoom_manual)。

相关问题