I am trying to learn how to animate plots (using an animation showing movement by quarters of the year)
After running Option #1 below, the plot pane of R Studio does not do anything. I was expecting it to play an animated plot, transitioning quarter by quarter. I am able to view the static plot, p.
Option #2 does create a viewer pane but I have to view it outside of R studio to be able to see it and play it. This also does not correctly display the title I want. It just says {closest state} as the title.
Any ideas why I am having these issues with Option 1 or Option 2?
rm(list=ls()) #clear the slate
library(ggplot2)
library(plotly)
library(gganimate)
library(Rmisc)
mydata<-read.csv("Data for R.csv",head=TRUE)
#option 1
p <- ggplot(mydata,aes(x=Segment,y=Payroll)) + geom_point()
anim <- p + transition_states(Quarter,transition_length = 1, state_length=1) + ggtitle('{closest state}', subtitle='Frame {frame} of {nframes}')
anim
#option 2
#try 2
p <- ggplot(mydata,aes(x=Segment,y=Payroll)) + geom_point(aes(frame=Quarter))
p <- p + ggtitle('{closest state}', subtitle='Frame {frame} of {nframes}')
ggplotly(p)
Updated post to include the output of the following:
> dput(head(mydata, 20))
structure(list(Segment = c(100L, 200L, 300L, 400L, 500L, 600L,
700L, 800L, 900L, 1000L, 1100L, 1200L, 1300L, 1400L, 1500L, 1600L,
1700L, 1800L, 1900L, 2000L), Quarter = structure(c(1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L), .Label = c("2010 Q4", "2011 Q1", "2011 Q2", "2011 Q3", "2011 Q4",
"2012 Q1", "2012 Q2", "2012 Q3", "2012 Q4", "2013 Q1", "2013 Q2",
"2013 Q3", "2013 Q4", "2014 Q1", "2014 Q2", "2014 Q3", "2014 Q4",
"2015 Q1", "2015 Q2", "2015 Q3", "2015 Q4", "2016 Q1", "2016 Q2",
"2016 Q3", "2016 Q4", "2017 Q1", "2017 Q2", "2017 Q3", "2017 Q4",
"2018 Q1", "2018 Q2", "2018 Q3", "2018 Q4", "2019 Q1", "2019 Q2",
"2019 Q3", "2019 Q4"), class = "factor"), Payroll = c(450000,
3350000, 12400000, 22650000, 36300000, 51600000, 6.5e+07, 38600000,
6e+07, 5.2e+07, 54900000, 46500000, 47500000, 47500000, 61100000,
83800000, 131100000, 210200000, 1.5e+08, 165500000)), row.names = c(NA,
20L), class = "data.frame")