I have been playing with the new version of gganimate, I tend to use animations a lot in my classes. I am trying to build a graph that shows how Nitrous oxide changes over time in a station in Spain. I want two features in the animations
I have been able to build this two graphs using the following data
Madrid3 <- structure(list(month = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8,
9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3,
4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5,
6, 7, 8, 9, 10, 11, 12), name = c("Cuatro Caminos", "Cuatro Caminos",
"Cuatro Caminos", "Cuatro Caminos", "Cuatro Caminos", "Cuatro Caminos",
"Cuatro Caminos", "Cuatro Caminos", "Cuatro Caminos", "Cuatro Caminos",
"Cuatro Caminos", "Cuatro Caminos", "Cuatro Caminos", "Cuatro Caminos",
"Cuatro Caminos", "Cuatro Caminos", "Cuatro Caminos", "Cuatro Caminos",
"Cuatro Caminos", "Cuatro Caminos", "Cuatro Caminos", "Cuatro Caminos",
"Cuatro Caminos", "Cuatro Caminos", "Cuatro Caminos", "Cuatro Caminos",
"Cuatro Caminos", "Cuatro Caminos", "Cuatro Caminos", "Cuatro Caminos",
"Cuatro Caminos", "Cuatro Caminos", "Cuatro Caminos", "Cuatro Caminos",
"Cuatro Caminos", "Cuatro Caminos", "Cuatro Caminos", "Cuatro Caminos",
"Cuatro Caminos", "Cuatro Caminos", "Cuatro Caminos", "Cuatro Caminos",
"Cuatro Caminos", "Cuatro Caminos", "Cuatro Caminos", "Cuatro Caminos",
"Cuatro Caminos", "Cuatro Caminos", "Cuatro Caminos", "Cuatro Caminos",
"Cuatro Caminos", "Cuatro Caminos", "Cuatro Caminos", "Cuatro Caminos",
"Cuatro Caminos", "Cuatro Caminos", "Cuatro Caminos", "Cuatro Caminos",
"Cuatro Caminos", "Cuatro Caminos", "Cuatro Caminos", "Cuatro Caminos",
"Cuatro Caminos", "Cuatro Caminos", "Cuatro Caminos", "Cuatro Caminos",
"Cuatro Caminos", "Cuatro Caminos", "Cuatro Caminos", "Cuatro Caminos",
"Cuatro Caminos", "Cuatro Caminos", "Cuatro Caminos", "Cuatro Caminos",
"Cuatro Caminos", "Cuatro Caminos", "Cuatro Caminos", "Cuatro Caminos",
"Cuatro Caminos", "Cuatro Caminos", "Cuatro Caminos", "Cuatro Caminos",
"Cuatro Caminos", "Cuatro Caminos", "Cuatro Caminos", "Cuatro Caminos",
"Cuatro Caminos", "Cuatro Caminos", "Cuatro Caminos", "Cuatro Caminos",
"Cuatro Caminos", "Cuatro Caminos", "Cuatro Caminos", "Cuatro Caminos",
"Cuatro Caminos", "Cuatro Caminos"), year = c(2010, 2010, 2010,
2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2011, 2011,
2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2012,
2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012,
2013, 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2013,
2013, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014,
2014, 2014, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015,
2015, 2015, 2015, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016,
2016, 2016, 2016, 2016, 2017, 2017, 2017, 2017, 2017, 2017, 2017,
2017, 2017, 2017, 2017, 2017), NO_2 = c(52.7411978235155, 49.9936308697576,
45.3346567988235, 42.7514465030941, 35.8548923634714, 47.1773919094889,
53.7143816896664, 41.9823522292158, 63.525647942449, 72.838654011127,
67.8730001012484, 74.422916644363, 72.6258411843876, 82.929955290611,
54.8495702005731, 52.7180555555556, 46.2647849462366, 50.0291666666667,
41.483039348711, 39.4808510638298, 57.9651324965132, 58.7177419354839,
51.3212795549374, 54.7997311827957, 65.4245283018868, 52.0502873563218,
46.5370121130552, 28.3212795549374, 35.5846774193548, 28.4361111111111,
31.822102425876, 26.3978494623656, 39.7367688022284, 57.5685483870968,
50.7777777777778, 59.7415881561238, 52.8936742934051, 48.1741071428571,
34.8891891891892, 36.7805555555556, 34.9381720430108, 33.1390820584145,
38.257065948856, 29.1467025572005, 46.3147632311978, 48.7190860215054,
48.9763560500695, 66.9152086137281, 45.5302826379542, 40.3288690476191,
46.7063599458728, 36.5340751043115, 34.25, 34.5805555555556,
33.1009421265141, 25.4072580645161, 38.3157162726008, 52.9743243243243,
47.8969359331476, 66.6617250673854, 70.5094594594595, 39.5111773472429,
47.6205962059621, 30.6193820224719, 32.2088948787062, 35.2154929577465,
35.3301886792453, 24.688679245283, 37.933147632312, 46.2293080054274,
65.5738161559889, 73.0350404312669, 44.7102425876011, 39.2126436781609,
37.7466307277628, 34.9527777777778, 32.7379032258064, 33.7051460361613,
35.6263440860215, 28.3189771197847, 46.3207810320781, 55.5389784946237,
54.9066852367688, 66.5080862533693, 59.8812415654521, 46.010447761194,
43.7183288409704, 34.3513888888889, 33.4, 35.7649513212796, 33.9986486486486,
26.2876344086022, 43.5251396648045, 59.6370967741936, 73.4442896935933,
60.0040431266846), n = c(743L, 672L, 744L, 720L, 744L, 720L,
744L, 744L, 720L, 744L, 720L, 744L, 743L, 672L, 720L, 720L, 744L,
720L, 744L, 720L, 720L, 744L, 720L, 744L, 743L, 696L, 744L, 720L,
744L, 720L, 744L, 744L, 720L, 744L, 720L, 744L, 743L, 672L, 744L,
720L, 744L, 720L, 744L, 744L, 720L, 744L, 720L, 744L, 743L, 672L,
744L, 720L, 744L, 720L, 744L, 744L, 720L, 744L, 720L, 744L, 743L,
672L, 744L, 720L, 744L, 720L, 744L, 744L, 720L, 744L, 720L, 744L,
743L, 696L, 744L, 720L, 744L, 720L, 744L, 744L, 720L, 744L, 720L,
744L, 743L, 672L, 744L, 720L, 744L, 720L, 744L, 744L, 720L, 744L,
720L, 744L)), row.names = c(NA, -96L), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"), vars = c("month", "name"), .Names = c("month",
"name", "year", "NO_2", "n"), indices = list(c(0L, 12L, 24L,
36L, 48L, 60L, 72L, 84L), c(1L, 13L, 25L, 37L, 49L, 61L, 73L,
85L), c(2L, 14L, 26L, 38L, 50L, 62L, 74L, 86L), c(3L, 15L, 27L,
39L, 51L, 63L, 75L, 87L), c(4L, 16L, 28L, 40L, 52L, 64L, 76L,
88L), c(5L, 17L, 29L, 41L, 53L, 65L, 77L, 89L), c(6L, 18L, 30L,
42L, 54L, 66L, 78L, 90L), c(7L, 19L, 31L, 43L, 55L, 67L, 79L,
91L), c(8L, 20L, 32L, 44L, 56L, 68L, 80L, 92L), c(9L, 21L, 33L,
45L, 57L, 69L, 81L, 93L), c(10L, 22L, 34L, 46L, 58L, 70L, 82L,
94L), c(11L, 23L, 35L, 47L, 59L, 71L, 83L, 95L)), group_sizes = c(8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L), biggest_group_size = 8L, labels = structure(list(
month = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12), name = c("Cuatro Caminos",
"Cuatro Caminos", "Cuatro Caminos", "Cuatro Caminos", "Cuatro Caminos",
"Cuatro Caminos", "Cuatro Caminos", "Cuatro Caminos", "Cuatro Caminos",
"Cuatro Caminos", "Cuatro Caminos", "Cuatro Caminos")), row.names = c(NA,
-12L), class = "data.frame", vars = c("month", "name"), .Names = c("month",
"name")))
When I use transition time, using the following code:
ggplot(Madrid2,aes(x = month, y = NO_2)) + stat_smooth(method = "lm", formula = y ~ x + I(x^2), alpha = 0.5,aes(fill = name)) + geom_point() +
# Here comes the gganimate code
transition_time(year) +
enter_fade() +
exit_shrink() +
ease_aes('linear') + labs(title = 'Year: {round(frame_time,0)}', x = 'Month', y = 'NO_2')
I get this image, which is good because I get the years as a title, but I would like the gif to stop for a while on each year. It follows condition 2 but not 1 of my list
So I try the following code:
ggplot(Madrid3,aes(x = month, y = NO_2)) + stat_smooth(method = "lm", formula = y ~ x + I(x^2), alpha = 0.5,aes(fill = name)) + geom_point() +
# Here comes the gganimate code
transition_time(year, state_length = 2, transition_length = 1) +
enter_fade() +
exit_shrink() +
ease_aes('linear') + labs(title = 'Year: {round(frame_time,0)}', x = 'Month', y = 'NO_2')
but I get the following error:
Error in transition_time(year, state_length = 2, transition_length = 1) :
unused arguments (state_length = 2, transition_length = 1)
So I tried with transition_states
instead of transition_times
With transtition_states I have a different problem, it works fine if I do this:
ggplot(Madrid3,aes(x = month, y = NO_2)) + stat_smooth(method = "lm", formula = y ~ x + I(x^2), alpha = 0.5,aes(fill = name)) + geom_point() +
# Here comes the gganimate code
transition_states(year, state_length = 2, transition_length = 1) +
enter_fade() +
exit_shrink() +
ease_aes('linear')
Which gives me the following graph:
In this case I have the transtition pauses I wanted (condition 1), but I can't get the titles to work (condition 2), I have tried:
ggplot(Madrid3,aes(x = month, y = NO_2)) + stat_smooth(method = "lm", formula = y ~ x + I(x^2), alpha = 0.5,aes(fill = name)) + geom_point() +
# Here comes the gganimate code
transition_states(year, state_length = 2, transition_length = 1) +
enter_fade() +
exit_shrink() +
ease_aes('linear') + labs(title = 'Year: {round(frame_time,0)}', x = 'Month', y = 'NO_2')
Which gives me the following error:
Error in eval(parse(text = text, keep.source = FALSE), envir) :
object 'frame_time' not found
So then I though, maybe change frame_time to frame_states
ggplot(Madrid3,aes(x = month, y = NO_2)) + stat_smooth(method = "lm", formula = y ~ x + I(x^2), alpha = 0.5,aes(fill = name)) + geom_point() +
# Here comes the gganimate code
transition_states(year, state_length = 2, transition_length = 1) +
enter_fade() +
exit_shrink() +
ease_aes('linear') + labs(title = 'Year: {round(frame_states,0)}', x = 'Month', y = 'NO_2')
But it gives me the following error:
Error in eval(parse(text = text, keep.source = FALSE), envir) :
object 'frame_states' not found
I am not sure what else to try
?transition_states
tells you which "variables [are] available for string
literal interpretation". So these are the variables you can use for your title. You want one of the following:
previous_state
next_state
closest_state
depending on preference.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With