Невозможно изменить заголовок или время перехода в зависимости от использования transition_states или transition_times в новой версии gganimate - PullRequest
0 голосов
/ 27 августа 2018

Я играл с новой версией gganimate, я часто использую анимацию в своих классах. Я пытаюсь построить график, который показывает, как закись азота меняется со временем на станции в Испании. Я хочу две функции в анимации

  1. Остановитесь на некоторое время в каждом году
  2. Каждый год указывать в заголовке год

Мне удалось построить эти два графика, используя следующие данные

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')

Я получаю это изображение, и это хорошо, потому что в качестве названия у меня указаны годы, но я бы хотел, чтобы гифки останавливались на некоторое время каждый год. Это соответствует условию 2, но не 1 из моего списка

enter image description here

Поэтому я пробую следующий код:

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') 

Что дает мне следующий график:

enter image description here

В этом случае у меня есть паузы перехода, которые я хотел (условие 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

Я не уверен, что еще попробовать

1 Ответ

0 голосов
/ 27 августа 2018

?transition_states сообщает вам, какие "переменные [доступны] для строкового литерального интерпретации" .Так что это переменная, которую вы можете использовать для вашего заголовка.Вы хотите одно из следующего:

  • previous_state
  • next_state
  • closest_state

в зависимости от предпочтения.

...