1

I am relatively new to R and am attempting to use gganimate to plot the occurrences of a given phenomenon on a map of the United States. I have a column frameid which is calculated by multiplying a column week by 100 and adding the nth occurrence of said phenomenon. So the 7th occurrence of a phenomenon in New York in the first week of the dataset would have a frameid of 107, and the 7th occurrence of a phenomenon in Los Angeles would also have a frameid of 107. I am passing frameid into transition_states, but the problem is that I only want to display the week (i.e. everything but the last two characters, as the occurrences could never conceivably exceed 99), so I need to manipulate frameid somehow. The attempted solution:

    labs(title = "My Title",
    subtitle = paste("2019, Week",
                     substring("{closest_state}", 1, nchar("{closest_state}")-2)))

Returns 50 warnings, the first of which is "1: Cannot get dimensions of plot table. Plot region might not be fixed" and the last 49 of which are "Expecting '}'".

I have also tried:

gsub('.{3}$', '', "{closest_state}")

Which returns a similar error. Is there a solution or a workaround to this problem or can "{closest_state}" not be manipulated?

Here is the reproducible code:

library(ggplot2)
library(gganimate)
library(fiftystater)

sample <- data.frame(
          frameid = c(101, 101, 101, 102, 102, 102, 201, 201, 201),
          latitude = c(38.02262, 38.99691, 41.31194, 27.00071,
                        28.539, 30.2836, 38.02262, 38.03112,
                        40.21603),
          longitude = c(-84.50521, -104.84369, -105.56906, -108.4121,
                        -81.4028, -97.73234, -84.50521, -78.51371,
                        -85.4177)
)


fortynine_states <- fifty_states %>%
                    filter(id != "alaska")

my_plot <- sample %>%
  ggplot(aes(x = longitude, y = latitude)) +
  geom_polygon(data = fortynine_states, 
               mapping = aes(long, lat, group = group),
               fill = "white", color = "black") +
  geom_point(aes(alpha = 0.2, color = "red")) +
  coord_map() +
  labs(title = "My Title",
       subtitle = paste("2019, Week",
                        substring("{closest_state}", 1, nchar("{closest_state}")-2))) +
  transition_states(frameid, transition_length = 2) +
  exit_fade()

  animate(my_plot, duration = 5, fps = 20, width = 400, height = 300, renderer = gifski_renderer())
Bryce R.
  • 11
  • 3
  • If you need help with R code, you should provide the [reproducible data](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Wiktor Stribiżew Mar 05 '20 at 18:27
  • @WiktorStribiżew my apologies. I have updated the original post and included the reproducible data. – Bryce R. Mar 05 '20 at 19:26

0 Answers0