cis-ds / dataviz

3 stars 21 forks source link

Using gganimate #52

Closed bensoltoff closed 7 years ago

bensoltoff commented 7 years ago

@DanaWestley When I run the following code:

library(gganimate)

p <- ggplot(fertmelt, aes(map_id = "Country", frame = "Year")) +
  geom_map(aes(fill = "Fertility"), map = world) +
  expand_limits(x = world$long, y = world$lat) +
  scale_fill_brewer(palette = "BuGn") +
  labs(title = "Fertility rate",
       fill = NULL) +
  ggthemes::theme_map() +
  coord_map(projection = "mollweide") +
  theme(legend.position = "none")
gg_animate(p)

I don't get an error. Remember that the package is called gganimate but the function is called gg_animate(). This creates the following image:

plot

DanaWestley commented 7 years ago

When I leave the underscore in it says no such function found. I saw some examples online without the underscore so i tried that. Still not able to produce an animation on my end.

DanaWestley commented 7 years ago

Also i realized I named two objects 'p' so the one that you are seeing is not the world map I made, but a ggplot I tried to troubleshoot with.

DanaWestley commented 7 years ago

I feel like some default packages may not be loading correctly. In class, I was able to create a map only loading the package maps and ggplot2. At home I had issues and had to load dyplr and tibble as packages in order to save a map_data

bensoltoff commented 7 years ago

Running this code chunk on my computer:

library(tidyverse)
library(gganimate)

# get fertility data
fert <- read_csv("FertilityHistory2.csv")
fert

# tidy the data frame
fert_tidy <- fert %>%
  gather(year, fertility, -Country, convert = TRUE)
fert_tidy

# get world geodatabase
world <- map_data("world")

# test the plot on just a few years of data

p <- fert_tidy %>%
  filter(year > 2000, year < 2003) %>%
  ggplot(aes(map_id = Country, frame = year)) +
  geom_map(aes(fill = fertility), map = world) +
  expand_limits(x = world$long, y = world$lat) +
  labs(title = "Fertility rate",
       fill = NULL) +
  ggthemes::theme_map() +
  coord_map(projection = "mollweide", xlim = c(-180, 180))
gg_animate(p, filename = "fertility_year.gif")

Produces this image file:

fertility_year

This is just on a two year sample of data. If you want to animate the entire 200+ year history of fertility, it just takes a long time for the computer to animate each frame.