我一直在玩新版本的 gganimate,我在课堂上经常使用动画。我正在尝试建立一个图表,显示一氧化二氮如何随时间在西班牙的一个车站发生变化。我想要动画中的两个功能
- 每年停一段时间
- 每次的标题中都有年份
我已经能够使用以下数据构建这两个图表
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")))
使用 transition_time
当我使用过渡时间时,使用以下代码:
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')
我得到了这张图片,这很好,因为我得到了年份作为标题,但我希望 gif 每年停止一段时间。它遵循条件 2 但不是我列表中的条件 1
所以我尝试以下代码:
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')
但我收到以下错误:
Error in transition_time(year, state_length = 2, transition_length = 1) :
unused arguments (state_length = 2, transition_length = 1)
所以我尝试使用transition_states
而不是transition_times
使用过渡状态
使用 transtition_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')
这给了我下图:
在这种情况下,我有我想要的过渡暂停(条件 1),但我无法让标题正常工作(条件 2),我尝试过:
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')
这给了我以下错误:
Error in eval(parse(text = text, keep.source = FALSE), envir) :
object 'frame_time' not found
那么我虽然可能将 frame_time 更改为 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')
但它给了我以下错误:
Error in eval(parse(text = text, keep.source = FALSE), envir) :
object 'frame_states' not found
我不知道还能尝试什么