Home > Software design >  Why do I get a blank animation instead of a map and two geom_points on top?
Why do I get a blank animation instead of a map and two geom_points on top?

Time:01-01

I tried to produce a gganimation with two geoms on a map of germany, but eventually only produced a blank animation. This here is a reproducible example of the animation I tried to produce:

# Load necessary packages
pacman::p_load(ggplot2, tidyverse, maps, mapdata,
               gganimate, av, emojifont)

# Production of example data
{
set.seed(123)
index = rep(c(1:10),
            each = 100) ; year = rep(c(1951:1960), 
                                     each = 10, times = 10); month = rep(c(1:10),
                                                                         each = 1,
                                                                         times = 100)

variable_of_interest = runif(1000,
                      min = 0,
                      max = 0.4); season = rep(x = c("Winter", "Winter", "Spring", "Spring", "Spring",
                                                     "Summer", "Summer", "Summer", "Autmn", "Autmn"),
                                               times = 100)

lat = rep(c(seq(from = 48,
              to = 53,
              length = 10)),
          each = 100); lon = rep(c(seq(from = 7,
                                       to = 14,
                                       length = 10)),
                                 each = 100)

Variable_of_Interest = as.data.frame(cbind(index, year, month, variable_of_interest, season, lat, lon))

Variable_of_Interest$month_of_year = paste0(Variable_of_Interest$year,
                                                        "-",
                                                        Variable_of_Interest$month)

#Variable_of_Interest = Variable_of_Interest[order(Variable_of_Interest$year,
#                                                                              Variable_of_Interest$month),]

rownames(Variable_of_Interest) = NULL
}

# Transform columns from character to numeric
Variable_of_Interest = mutate_at(Variable_of_Interest,
                                               c(1,2,3,4,6,7),
                                               as.numeric)


# Preperation for the animation
season_symbols = c("Spring" = emoji("cherry_blossom"),
                   "Summer" =  emoji("tropical_drink"), 
                   "Autmn" = emoji("fallen_leaf"),
                   "Winter" = emoji("snowflake"))


# Map of values of the variable of interest
Variable_of_Interest_Map = ggplot(Variable_of_Interest)   
  geom_path(data = map_data("world","Germany"),
            aes(x = long, 
                y = lat, 
                group = group))  
  coord_fixed(xlim = c(6,
                       15),
              ylim = c(47,
                       55))   
  geom_point(aes(x=lon, 
                 y=lat, 
                 group = month_of_year,
                 shape = season), 
             size = 10)   
  scale_shape_manual(values = season_symbols, breaks = c("Spring", "Summer", "Autmn", "Winter"))  
  geom_point(aes(x=lon, 
                 y=lat, 
                 group = month_of_year),
             colour = ifelse(test = Variable_of_Interest$variable_of_interest > 0.3, 
                             I("red"),
                             I("blue")))   
  # scale_color_gradient(low="blue", high="yellow")   
  xlab("Longitude (degree)")  
  ylab("Latitude (degree)")   
  theme_bw()  
  transition_manual(frames = month_of_year)   
  theme(legend.position = "bottom", 
        legend.title = element_text(size=12, face="bold"),
        panel.background = element_blank(),
        legend.background = element_blank())  
  labs(title = '{unique(Variable_of_Interest$month_of_year)[as.integer(frame)]}', 
       color = paste0("Variable of Interest"),
       shape = "season")

anim_save("Variable-of-Interest.mp4",
          animate(
            Variable_of_Interest_Map,
            nframes = 1000,
            renderer = ffmpeg_renderer()
          ))



The plan was to draw a map of germany, use one set of geom_points to represent the variation of the variable_of_interest among the locations in Germany through time and another set of geom_points to visualize the present season through drawing symbols like snowflakes for winter.

CodePudding user response:

The main issue with your code is that you tried to display your emojis via the shape aes and a geom_point which at least on my machine resulted in an error. Also I have not found any reference on using emoji font like that. Instead you could achieve your desired result using a geom_text to which end I added a column to your data containing the season symbol and which could be mapped on the label aes:

library(tidyverse)
library(emojifont)
library(gganimate)

Variable_of_Interest$season_symbol <- season_symbols[Variable_of_Interest$season]

Variable_of_Interest_Map <- ggplot(Variable_of_Interest)  
  geom_path(
    data = map_data("world", "Germany"),
    aes(
      x = long,
      y = lat,
      group = group
    )
  )  
  coord_fixed(
    xlim = c(
      6,
      15
    ),
    ylim = c(
      47,
      55
    )
  )  
  geom_text(
    aes(
      x = lon,
      y = lat,
      group = month_of_year,
      label = season_symbol
    ),
    size = 10, family = "EmojiOne"
  )  
  geom_point(aes(
    x = lon,
    y = lat,
    colour = ifelse(variable_of_interest > 0.3, "red", "blue"),
    group = month_of_year
  ))  
  scale_color_manual(values = c(red = "red", blue = "blue"), guide = "none")  
  xlab("Longitude (degree)")  
  ylab("Latitude (degree)")  
  theme_bw()  
  transition_manual(frames = month_of_year)  
  theme(
    legend.position = "bottom",
    legend.title = element_text(size = 12, face = "bold"),
    panel.background = element_blank(),
    legend.background = element_blank()
  )  
  labs(
    title = "{unique(Variable_of_Interest$month_of_year)[as.integer(frame)]}",
    color = paste0("Variable of Interest"),
    shape = "season"
  )

Variable_of_Interest_Map

  • Related