使用gganimate
时无法创建高分辨率文件。图表中的文本模糊不清,几乎就像在.gif
中为每个新框架编写文本一样。
是否有解决此问题的简单方法?我尝试摆弄ani.options()
,但那里也没有运气。
示例数据和代码:
value <- sample(0:30, 300, replace = TRUE)
year <- rep(c(2000, 2001, 2002), 100)
df <- as.data.frame(cbind(value, year))
library(gganimate)
library(tidyverse)
df <- df %>% group_by(year) %>% mutate(avg.value = mean(value))
p <- df %>%
ggplot(aes(x = value)) +
geom_histogram(position = 'identity', color = "white") +
geom_segment(aes(xend = avg.value, x = avg.value, y = 0, yend = -.5),
color = "red", size = 2.5) +
transition_states(as.factor(year), transition_length = 2, state_length
= 1, wrap = TRUE) +
annotate("text", x = df$avg.value, y = -1, label = "Average") +
labs(x = " ",
y = "",
title = "Example chart",
subtitle = "Don't it look blurry?")
animate(p, nframes = 100, renderer = gifski_renderer("example.gif"))
答案 0 :(得分:0)
要向annotate
(或我的代码中的geom_text
)提交多个相同的avg.value
值。每年当您只需要一个时,就可以添加一百个值。这样就可以相互绘制出一百个“平均”。
解决方案是向仅包含3行(每年一次)的geom_text
(即df2
)提交不同的数据。
代码:
library(gganimate)
library(tidyverse)
value <- sample(0:30, 300, replace = TRUE)
year <- rep(c(2000, 2001, 2002), 100)
df1 <- as.data.frame(cbind(value, year))
df1 <- df1 %>% group_by(year) %>% mutate(avg.value = mean(value))
df2 <- df1 %>% group_by(year) %>% summarize(avg.value = head(avg.value, 1))
r1 <- ggplot(df1) +
geom_histogram(aes(value), position = "identity", color = "white") +
geom_segment(aes(x = avg.value, xend = avg.value, y = 0, yend = -0.5),
color = "red", size = 2.5) +
geom_text(aes(avg.value, -1, label = "Average"), df2) +
transition_states(as.factor(year), 2, 1) +
labs(title = "Example chart",
subtitle = "No more blur",
x = NULL,
y = NULL)
animate(r1, nframes = 100, renderer = gifski_renderer("example.gif"))