标签: gganimate

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)

在此处输入图片说明

r axes gganimate

6
推荐指数
1
解决办法
2071
查看次数

gganimate 在 Windows 10 中渲染像素化

我必须使用 gganimate 渲染一些动画,但图像不好看。它们缺乏清晰度,边界像素化。有什么方法可以在 Windows 10 中获得更好的结果吗?

我的代码是:

library(gapminder)
library(ggplot2)
library(gganimate)
Cairo(600, 600, file="plot.png", type="png", bg="white")
ggplot(gapminder,aes(gdpPercap, lifeExp, size = pop, colour = country)) +
  geom_point(alpha = 0.7, show.legend = FALSE) +
  scale_colour_manual(values = country_colors) +
  scale_size(range = c(2, 12)) +
  scale_x_log10() +
  facet_wrap(~continent) +
  # Here comes the gganimate specific bits
  labs(title = 'Year: {frame_time}', x = 'GDP per capita', y = 'life expectancy') +
  transition_time(year) +
  ease_aes('linear')
Run Code Online (Sandbox Code Playgroud)

我得到的是这样的:

在此输入图像描述

animation r ggplot2 gganimate

5
推荐指数
1
解决办法
951
查看次数

多个 gganimate 地块并排

我想并排或至少在同一个文档中显示由 gganimate 包制作的动画。

使用的绘图:

library(ggplot2)
library(gganimate)

anime_one <- 
  ggplot(mtcars, aes(factor(cyl), mpg)) + 
  geom_boxplot() + 
  # Here comes the gganimate code
  transition_states(
    gear,
    transition_length = 2,
    state_length = 1
  ) +
  enter_fade() + 
  exit_shrink() +
  ease_aes('sine-in-out')

anime_two <- 
  ggplot(mtcars, aes(factor(cyl), mpg, fill = factor(cyl))) + 
  geom_boxplot() + 
  # Here comes the gganimate code
  transition_states(
    gear,
    transition_length = 2,
    state_length = 1
  ) +
  enter_fade() + 
  exit_shrink() +
  ease_aes('sine-in-out')
Run Code Online (Sandbox Code Playgroud)

第一次尝试:

library(patchwork)
anime_one + anime_two  

Error in UseMethod("ggplot_gtable") : 
no applicable method for …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 gganimate

5
推荐指数
1
解决办法
3022
查看次数

r gganimate:transition_reveal(更改标题)

我想使用 gganimate 创建一个 transition_reveal 动画,其中标题沿着当前显示的迭代变化。因此,开始时的标题将是“日期:2019-01-01”,然后逐渐适应动画并以显示“日期:2019-01-20”结束。

a <- seq(1,20,1)
b <- seq(1,40,2)
c <- seq(as.Date("2019-01-01"), as.Date("2019-01-20"), by="days")
db <- cbind(a,b,c) %>% as.data.frame()

p <- ggplot(db, aes(x = a, y = b)) +
  geom_path() +
  labs(title = "Date: {frame_time}") +
  transition_reveal(c)

animate(p, renderer = gifski_renderer(loop = FALSE), nframes = 90, fps = 30, height = 600, width = 1100, start_pause = 0)
Run Code Online (Sandbox Code Playgroud)

目前,我收到以下错误:

50: object 'fame_time' not found
Run Code Online (Sandbox Code Playgroud)

transition r gganimate

5
推荐指数
0
解决办法
2544
查看次数

如何阻止ggrepel标签在R/ggplot2中的gganimate帧之间移动?

我想在 ggplot 的行尾添加标签,避免它们重叠,并避免它们在动画过程中移动。

到目前为止,我可以将标签放在正确的位置并使用 保持它们静态geom_text,但标签重叠,或者我可以防止它们重叠使用geom_text_repel但标签不会出现在我希望它们出现的位置,然后在情节动画后跳舞(后一个版本在下面的代码中)。

我认为一个解决方案可能涉及在 ggplot(p1下)中有效地创建一个静态层,然后添加一个动画层(p2下),但似乎不是。

如何在动画 ggplot 中保存绘图常量(即静态)的某些元素?(在这种情况下,行尾的标签。)

此外,geom_text标签显示为我想要的 - 在每条线的末尾,在绘图之外 - 但是使用geom_text_repel,标签都在绘图区域内移动。为什么是这样?

以下是一些示例数据:

library(dplyr)
library(ggplot2)
library(gganimate)
library(ggrepel)

set.seed(99)

# data
static_data <- data.frame(
  hline_label = c("fixed_label_1", "fixed_label_2", "fixed_label_3", "fixed_label_4", 
                  "fixed_label_5", "fixed_label_6", "fixed_label_7", "fixed_label_8", 
                  "fixed_label_9", "fixed_label_10"), 
  fixed_score = c(2.63, 2.45, 2.13, 2.29, 2.26, 2.34, 2.34, 2.11, 2.26, 2.37))

animated_data <- data.frame(condition = c("a", "b")) %>% 
  slice(rep(1:n(), each = 10)) %>% 
  group_by(condition) %>% 
  mutate(time_point = row_number()) %>% …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 gganimate ggrepel

5
推荐指数
1
解决办法
1279
查看次数

使用 gganimate 构建堆叠直方图

我正在尝试显示随着时间的推移构建的直方图。它将从 1952 年的数据开始,然后每年更新直方图,并不断增长。

这条路似乎很笨拙,我想transition_reveal随着时间的推移慢慢地揭示更多的数据。这似乎不起作用。

假设我从这个开始:

library(gapminder)
library(tidyverse)
library(gganimate)

ggplot(gapminder, 
       aes(lifeExp, fill = fct_rev(factor(year)), group = fct_rev(factor(year)))) +
  geom_histogram(position = "stack", bins = 20) +
  transition_reveal(year)  
Run Code Online (Sandbox Code Playgroud)

这很失败。

我可以将一些东西与 混合在一起transition_layer,如下所示:

ggplot(gapminder, aes(lifeExp, fill = fct_rev(factor(year)))) +
  geom_histogram(position = "stack", bins = 20, 
                 data = filter(gapminder, year<= 1952)) +
  geom_histogram(position = "stack", bins = 20, 
                 data = filter(gapminder, year<= 1957)) +
  geom_histogram(position = "stack", bins = 20, 
                 data = filter(gapminder, year<= 1962)) +
  geom_histogram(position = "stack", bins = 20, …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 gganimate

5
推荐指数
1
解决办法
424
查看次数

将 gganimate 与华夫饼图结合使用

我一直在玩这个waffle包,并试图让它与gganimate.

作为mpg示例,我创建了华夫饼图来显示每个模型的数量class。然后,我想使用 gganimate 依次显示每个制造商按类别划分的模型图表。我可以用来facet_wrap()同时显示所有制造商的图表,但希望能够循环浏览它们。

当我尝试将 gganimate 应用于华夫饼图时,出现错误:

mapply(FUN = f, ..., SIMPLIFY = FALSE) 中的错误:零长度输入不能与非零长度输入混合

我不确定是否waffle与 不兼容gganimate,或者我是否做错了什么。

这是代码:

library(tidyverse)
library(waffle)
library(gganimate)

data("mpg")
d <- mpg

d$class <- as.factor(d$class)
d$manufacturer <- as.factor(d$manufacturer)

plot <- d %>% count(manufacturer, class) %>%
  ggplot(aes(fill = class, values = n)) +
  geom_waffle(color = "white",
              size = .75,
              n_rows = 4) +
  ggthemes::scale_fill_tableau(name = NULL) +
  coord_equal() +
  theme_minimal() +
  theme(panel.grid = element_blank(), …
Run Code Online (Sandbox Code Playgroud)

animation r ggplot2 gganimate waffle-chart

5
推荐指数
1
解决办法
584
查看次数

使用 gganimate 为地图制作动画需要使用哪个transition_*?

我想制作一张美国各县的动画分区统计图,其中包含一段时间内确诊的 COVID-19 病例数(是的,又一个冠状病毒图)。是三天数据选择的链接(欢迎建议更永久的托管位置)。以下是创建静态地图的代码(注释过滤器以包含所有日期):

library(tidyverse)
library(gganimate)
library(ggmap)
library(maps)
library(scales)

p <- part_data %>%
  filter(date == as.Date("2020-03-30")) %>%
  ggplot(aes(x = long, y = lat, group = group)) +
  geom_polygon(aes(fill = confirmed_new), color = "grey70", size = 0.05) + 
  geom_path(data = state_map, colour = "black") +
  coord_map() +
  scale_fill_distiller(trans = "log10", direction = 1, palette = "YlOrRd", na.value = "white", limits = c(1, 1E4), labels = comma)
Run Code Online (Sandbox Code Playgroud)

这给出了这个非常漂亮的情节: 地图图

但是我现在如何制作一个随日期移动的动画呢?我试过

p +
  transition_time(date)
Run Code Online (Sandbox Code Playgroud)

p + 
  transition_states(date)
Run Code Online (Sandbox Code Playgroud)

但在这两种情况下,R 似乎在渲染进度条出现之前就冻结了,过了一会儿我收到了错误Error: cannot allocate …

ggplot2 gganimate

5
推荐指数
1
解决办法
744
查看次数

如何使用多核使 gganimate 更快

我的问题是如何利用 iMac 的多个核心来使 gganimate 运行得更快。还有另一个问题(更多链接在下面)询问同样的事情\xe2\x80\x94我的问题是关于这个问题的答案加速 gganimate Rendering

\n

在该答案中,Roman 和 mhovd 指出了此GitHub 评论中的一个示例(另请参阅此 GitHub 帖子):

\n
library(gganimate)\nlibrary(future)\n\nanim <- ggplot(mtcars, aes(mpg, disp)) +\n  transition_states(gear, transition_length = 2, state_length = 1) +\n  enter_fade() +\n  exit_fade()\n\nfuture::plan("sequential")  ## default\nt0 <- system.time(animate(anim))\nprint(t0)\n\nfuture::plan("multiprocess", workers = 4L)\nt1 <- system.time(animate(anim))\nprint(t1)\n
Run Code Online (Sandbox Code Playgroud)\n

我已经尝试过这个,但得到的时间彼此非常接近:

\n
     user    system   elapsed \n1.0041475 0.9775679 0.9995509 \n
Run Code Online (Sandbox Code Playgroud)\n

除了这段代码之外,我还需要做些什么吗?根据上述 StackOverflow 答案或 GitHub 页面,我无法判断这段代码是否应该按原样工作,或者是否在幕后进行了其他修改。

\n

如果有帮助的话,我正在使用配备 8 核 Intel 处理器的 iMac。我也在 R 中运行它,因为 RStudio 说了一些关于它不支持多核的内容。

\n

另请注意,我的问题也广泛涉及这三个过去的问题:

\n …

parallel-processing macos r ggplot2 gganimate

5
推荐指数
1
解决办法
841
查看次数

如何在 R 中制作三元图的动画

我正在尝试用 gganimate 为 R 中的三元图制作动画,而我面对着一堵墙。

library(tidyverse)
library(gganimate)
library(ggtern)
#> Registered S3 methods overwritten by 'ggtern':
#>   method           from   
#>   grid.draw.ggplot ggplot2
#>   plot.ggplot      ggplot2
#>   print.ggplot     ggplot2
#> --
#> Remember to cite, run citation(package = 'ggtern') for further info.
#> --
#> 
#> Attaching package: 'ggtern'
#> The following objects are masked from 'package:ggplot2':
#> 
#>     aes, annotate, ggplot, ggplot_build, ggplot_gtable, ggplotGrob,
#>     ggsave, layer_data, theme_bw, theme_classic, theme_dark,
#>     theme_gray, theme_light, theme_linedraw, theme_minimal, theme_void

data=tibble(x=runif(200),  y = runif(200),  z …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 ggtern gganimate tidyverse

5
推荐指数
1
解决办法
172
查看次数