3

I'm having trouble creating high resolution files when using gganimate. The text in the charts comes out blurry, almost as if the text is being written for every new frame in the .gif.

Example of blurry chart text

Is there a simple solution to this problem? I tried fiddling with the ani.options(), but haven't had any luck there either.

Example data and code:

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"))
pogibas
  • 27,303
  • 19
  • 84
  • 117
henryjean
  • 102
  • 9

1 Answers1

2

To annotate (or geom_text in my code) you're submitting multiple same avg.value values. When for yeach year you need only one you're adding hundred values. This results in hundred "Average" plotted on each other.
Solution is to submit different data to geom_text (ie, df2) that contains only 3 rows (one entry for each year).

Code:

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"))
pogibas
  • 27,303
  • 19
  • 84
  • 117
  • 1
    Aces! That definitely clears up the "average" text. But what about the chart title/subtitle itself? It doesn't appear as sharp as a static image of one frame would. Is that just a byproduct of exporting it as a .gif? Or is there a way to increase the overall resolution of the animated image – henryjean Oct 31 '18 at 19:34
  • @henryjean I guess it's a device resolution problem with text, you should look into `gganimate` settings for resolution. – pogibas Oct 31 '18 at 19:35
  • thanks for the help. I tried poking around gganimate settings, but never found anything related to dpi. Unfortunately it doesn't appear to be as straightforward as in ggsave where you can set the value of the dpi – henryjean Oct 31 '18 at 20:05
  • 1
    Or you can add `check_overlap = TRUE` to `geom_text` – Tung Oct 31 '18 at 20:12