Skip to content

Latest commit

 

History

History
101 lines (81 loc) · 4.31 KB

asdf.md

File metadata and controls

101 lines (81 loc) · 4.31 KB
library(dplyr)    # pipes and dplyr verbs
## 
## Attaching package: 'dplyr'

## The following objects are masked from 'package:stats':
## 
##     filter, lag

## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(readr)    # reading in .csv files

library(ggplot2)  # plotting
library(ggthemes) # ggplot2 themes
library(scales)   # ggplot2 scaling options
## 
## Attaching package: 'scales'

## The following object is masked from 'package:readr':
## 
##     col_factor
Japan_Steel <- readxl::read_xls("Data/iron_steel_production_1948-2004.xls",
                             skip = 7,
                             col_names = c("J_Year", "Year", "Pig_iron", 
                                           "Ferro-alloys", "Crude_steel",
                                           "Hot_steel_products", "Galvanized_sheets")) %>% 
  select(Year, Crude_steel) %>% 
  filter(Year <= 1960)

Japan_Steel %>% 
  ggplot(aes(x = Year, y = Crude_steel)) + 
  geom_line(size = 2.5, color = "#474747") +
  scale_x_continuous(breaks = pretty_breaks(n = 10)) +
  scale_y_continuous(breaks = pretty_breaks(), labels = comma, limits = c(0, 26000)) +
  geom_vline(xintercept = 1950) +
  geom_vline(xintercept = 1953) +
  annotate(geom = "text",
           label = "Korean\nWar", size = 5, color = "#474747",
           x = 1951.55, y = 17500) +
  theme_economist_white() +
  theme(axis.text = element_text(size = 10, color = "#474747"), 
        axis.title = element_text(size = 15, color = "#474747"),
        plot.title = element_text(hjust = 0.5, color = "#474747")) +
  labs(x = "Year", y = "Steel Production (Thousands of tons)", 
       title = "Japan Steel Production\n(1950-1960)",
       caption = "Source: Historical Statistics of Japan, 2012") 

Introducing gganimate!

The idea behind gganimate is simple, as the name implies the animation part of a plot becomes just another aesthetic within the ggplot concept. This package is dependent on ImageMagick and FFmpeg to save your plots as GIFs or videos so make sure you have those installed as well. Once all that's done, go into the ggplot() code you previously made and it's just as simple as adding in frame = ____ (whatever the variable you want to create an individual frame out of). You can also include cumulative = TRUE if you want to keep previous frames of added data on the plot as you change to the next frames.

For other tutorials take a look at gganimate's Github repo for a short tutorial or take a look at Peter Aldhous' lecture notes on Iteration & Animation (with ggplot2).

If you're more of a video person or just want something to watch while grabbing some lunch here is David Robinson talking about gganimate at Plotcon 2016.

library(gganimate) # animations

Steel_Animate <- Japan_Steel %>% 
  ggplot(aes(x = Year, y = Crude_steel, frame = Year, cumulative = TRUE)) + 
  geom_line(size = 2.5, color = "#474747") +
  scale_x_continuous(breaks = pretty_breaks(n = 10)) +
  scale_y_continuous(breaks = pretty_breaks(), labels = comma, limits = c(0, 26000)) +
  geom_vline(xintercept = 1950) +
  geom_vline(xintercept = 1953) +
  annotate(geom = "text",
           label = "Korean\nWar", size = 5, color = "#474747",
           x = 1951.55, y = 17500) +
  theme_economist_white() +
  theme(axis.text = element_text(size = 10, color = "#474747"), 
        axis.title = element_text(size = 15, color = "#474747"),
        plot.title = element_text(hjust = 0.5, color = "#474747")) +
  labs(x = "Year", y = "Steel Production (Thousands of tons)", 
       title = "Japan Steel Production\n(1950-1960)",
       caption = "Source: Historical Statistics of Japan, 2012") 

gganimate(Steel_Animate, "animate.mp4")
## geom_path: Each group consists of only one observation. Do you need to
## adjust the group aesthetic?

## Executing: "ffmpeg" -y -framerate 1 -i Rplot%d.png -pix_fmt yuv420p animate.mp4

## 
## 
## Video has been created at: C:\Users\Ryo Nakagawara\Documents\R_materials\AmericaJapanPostWW2Trade\animate.mp4