我一直在玩新版本的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")))
使用过渡时间时,请使用以下代码:
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
我不确定还有什么尝试
答案 0 :(得分:3)
?transition_states
告诉您哪些“变量[可用于字符串
文字解释”。。因此,这些是可以用于标题的变量。您需要以下条件之一:
previous_state
next_state
closest_state
取决于偏好。