Let the snail crawl: Animated density curves
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Previously, I’ve plotted a ridgeline based on a variable’s density through time. It might look nice but it’s quite obvious that time can be visualized in a more fitting way – by time itself, in an animated plot that is. So, let’s fire up the {gganimate} package again.
My goal is to show a moving kernel density curve as it moves through time, based on a moving window of 30 days sliding from the past to the present. It would also be nice to see earlier stages as shadows in the animated plot. But first, let’s get the data again:
temp <- tempfile() download.file("https://www.owid.de/plus/cowidplus2020/data/unigram-results.csv.zip", temp) con <- unz(temp, "unigram-results.csv") res <- read.table(con, sep = ",", header = T) unlink(temp) scroll_box(kable_styling(kable(res)), width = "100%", height = "400px")
date | tok | rel.ent | redundancy | msttr | top100share | week.day | weekend |
---|---|---|---|---|---|---|---|
2020-01-01 | 51369 | 0.7952043 | 0.2047957 | 0.6340588 | 0.4388250 | 4 | Weekday |
2020-01-02 | 68452 | 0.7968620 | 0.2031380 | 0.6636471 | 0.4134138 | 5 | Weekday |
2020-01-03 | 74528 | 0.7904068 | 0.2095932 | 0.6597047 | 0.4211571 | 6 | Weekday |
2020-01-04 | 47749 | 0.8054274 | 0.1945726 | 0.6518526 | 0.4211607 | 7 | Weekend |
2020-01-05 | 55207 | 0.7954869 | 0.2045131 | 0.6402364 | 0.4309961 | 1 | Weekend |
2020-01-06 | 71235 | 0.7938855 | 0.2061145 | 0.6636197 | 0.4147399 | 2 | Weekday |
2020-01-07 | 81513 | 0.7909694 | 0.2090306 | 0.6667117 | 0.4177002 | 3 | Weekday |
2020-01-08 | 81895 | 0.7879631 | 0.2120369 | 0.6622699 | 0.4225411 | 4 | Weekday |
2020-01-09 | 81108 | 0.7931069 | 0.2068931 | 0.6703951 | 0.4108596 | 5 | Weekday |
2020-01-10 | 83265 | 0.7920327 | 0.2079673 | 0.6728795 | 0.4125023 | 6 | Weekday |
2020-01-11 | 52040 | 0.8058137 | 0.1941863 | 0.6517308 | 0.4163336 | 7 | Weekend |
2020-01-12 | 55119 | 0.8048257 | 0.1951743 | 0.6590182 | 0.4151382 | 1 | Weekend |
2020-01-13 | 80103 | 0.7939123 | 0.2060877 | 0.6724250 | 0.4075752 | 2 | Weekday |
2020-01-14 | 84188 | 0.7936109 | 0.2063891 | 0.6788452 | 0.4075759 | 3 | Weekday |
2020-01-15 | 81905 | 0.7960199 | 0.2039801 | 0.6784049 | 0.4054331 | 4 | Weekday |
2020-01-16 | 88650 | 0.7907580 | 0.2092420 | 0.6733107 | 0.4111788 | 5 | Weekday |
2020-01-17 | 84255 | 0.7954551 | 0.2045449 | 0.6822381 | 0.4046169 | 6 | Weekday |
2020-01-18 | 55478 | 0.7993700 | 0.2006300 | 0.6422545 | 0.4208515 | 7 | Weekend |
2020-01-19 | 54796 | 0.8027303 | 0.1972697 | 0.6534862 | 0.4181327 | 1 | Weekend |
2020-01-20 | 82544 | 0.7945252 | 0.2054748 | 0.6760727 | 0.4062924 | 2 | Weekday |
2020-01-21 | 84487 | 0.7927262 | 0.2072738 | 0.6727500 | 0.4096488 | 3 | Weekday |
2020-01-22 | 84811 | 0.7938524 | 0.2061476 | 0.6788521 | 0.4048649 | 4 | Weekday |
2020-01-23 | 82327 | 0.7928354 | 0.2071646 | 0.6726829 | 0.4049218 | 5 | Weekday |
2020-01-24 | 85756 | 0.7902831 | 0.2097169 | 0.6721053 | 0.4071552 | 6 | Weekday |
2020-01-25 | 57620 | 0.7984417 | 0.2015583 | 0.6458609 | 0.4217806 | 7 | Weekend |
2020-01-26 | 51484 | 0.8064711 | 0.1935289 | 0.6568235 | 0.4136237 | 1 | Weekend |
2020-01-27 | 81112 | 0.7933556 | 0.2066444 | 0.6742469 | 0.4104448 | 2 | Weekday |
2020-01-28 | 84583 | 0.7897648 | 0.2102352 | 0.6635503 | 0.4152962 | 3 | Weekday |
2020-01-29 | 89950 | 0.7909319 | 0.2090681 | 0.6734637 | 0.4097721 | 4 | Weekday |
2020-01-30 | 89090 | 0.7885952 | 0.2114048 | 0.6753146 | 0.4086654 | 5 | Weekday |
2020-01-31 | 84957 | 0.7871475 | 0.2128525 | 0.6664970 | 0.4188825 | 6 | Weekday |
2020-02-01 | 54682 | 0.8013665 | 0.1986335 | 0.6520367 | 0.4206686 | 7 | Weekend |
2020-02-02 | 55367 | 0.7968999 | 0.2031001 | 0.6442364 | 0.4297506 | 1 | Weekend |
2020-02-03 | 79531 | 0.7919761 | 0.2080239 | 0.6699371 | 0.4140398 | 2 | Weekday |
2020-02-04 | 85264 | 0.7915730 | 0.2084270 | 0.6708000 | 0.4101379 | 3 | Weekday |
2020-02-05 | 89622 | 0.7814496 | 0.2185504 | 0.6517207 | 0.4264578 | 4 | Weekday |
2020-02-06 | 85800 | 0.7865656 | 0.2134344 | 0.6637661 | 0.4219697 | 5 | Weekday |
2020-02-07 | 82222 | 0.7906027 | 0.2093973 | 0.6698902 | 0.4141227 | 6 | Weekday |
2020-02-08 | 56285 | 0.7992940 | 0.2007060 | 0.6461429 | 0.4210713 | 7 | Weekend |
2020-02-09 | 61222 | 0.7917481 | 0.2082519 | 0.6369016 | 0.4372284 | 1 | Weekend |
2020-02-10 | 85419 | 0.7854907 | 0.2145093 | 0.6594706 | 0.4244957 | 2 | Weekday |
2020-02-11 | 83886 | 0.7890498 | 0.2109502 | 0.6658683 | 0.4135017 | 3 | Weekday |
2020-02-12 | 84191 | 0.7921862 | 0.2078138 | 0.6731667 | 0.4101389 | 4 | Weekday |
2020-02-13 | 80507 | 0.7950453 | 0.2049547 | 0.6778012 | 0.4072068 | 5 | Weekday |
2020-02-14 | 84980 | 0.7912255 | 0.2087745 | 0.6719408 | 0.4072605 | 6 | Weekday |
2020-02-15 | 54358 | 0.7995883 | 0.2004117 | 0.6440000 | 0.4209868 | 7 | Weekend |
2020-02-16 | 53686 | 0.8026761 | 0.1973239 | 0.6579813 | 0.4191409 | 1 | Weekend |
2020-02-17 | 82349 | 0.7938916 | 0.2061084 | 0.6710000 | 0.4100353 | 2 | Weekday |
2020-02-18 | 86044 | 0.7909025 | 0.2090975 | 0.6725581 | 0.4087560 | 3 | Weekday |
2020-02-19 | 83368 | 0.7928467 | 0.2071533 | 0.6730482 | 0.4060191 | 4 | Weekday |
2020-02-20 | 91217 | 0.7798272 | 0.2201728 | 0.6534066 | 0.4271024 | 5 | Weekday |
2020-02-21 | 82436 | 0.7890917 | 0.2109083 | 0.6633171 | 0.4145883 | 6 | Weekday |
2020-02-22 | 55111 | 0.7992130 | 0.2007870 | 0.6494364 | 0.4218940 | 7 | Weekend |
2020-02-23 | 67455 | 0.7875844 | 0.2124156 | 0.6373284 | 0.4369580 | 1 | Weekend |
2020-02-24 | 85232 | 0.7830546 | 0.2169454 | 0.6533882 | 0.4262601 | 2 | Weekday |
2020-02-25 | 84383 | 0.7871764 | 0.2128236 | 0.6623214 | 0.4260574 | 3 | Weekday |
2020-02-26 | 79735 | 0.7904838 | 0.2095162 | 0.6664151 | 0.4148241 | 4 | Weekday |
2020-02-27 | 84444 | 0.7867161 | 0.2132839 | 0.6656429 | 0.4203614 | 5 | Weekday |
2020-02-28 | 83600 | 0.7850317 | 0.2149683 | 0.6577485 | 0.4236603 | 6 | Weekday |
2020-02-29 | 54067 | 0.7963609 | 0.2036391 | 0.6365370 | 0.4316496 | 7 | Weekend |
2020-03-01 | 52026 | 0.7990732 | 0.2009268 | 0.6448846 | 0.4306116 | 1 | Weekend |
2020-03-02 | 82670 | 0.7868875 | 0.2131125 | 0.6608364 | 0.4215072 | 2 | Weekday |
2020-03-03 | 87192 | 0.7857865 | 0.2142135 | 0.6616207 | 0.4180773 | 3 | Weekday |
2020-03-04 | 81912 | 0.7851150 | 0.2148850 | 0.6545644 | 0.4236254 | 4 | Weekday |
2020-03-05 | 77082 | 0.7903705 | 0.2096295 | 0.6671169 | 0.4177888 | 5 | Weekday |
2020-03-06 | 80540 | 0.7886634 | 0.2113366 | 0.6593416 | 0.4206978 | 6 | Weekday |
2020-03-07 | 51735 | 0.8009777 | 0.1990223 | 0.6432816 | 0.4225573 | 7 | Weekend |
2020-03-08 | 51397 | 0.7971364 | 0.2028636 | 0.6420588 | 0.4321069 | 1 | Weekend |
2020-03-09 | 81043 | 0.7815881 | 0.2184119 | 0.6516790 | 0.4300803 | 2 | Weekday |
2020-03-10 | 82885 | 0.7826860 | 0.2173140 | 0.6507273 | 0.4273270 | 3 | Weekday |
2020-03-11 | 60200 | 0.7919958 | 0.2080042 | 0.6478833 | 0.4278239 | 4 | Weekday |
2020-03-12 | 81978 | 0.7812868 | 0.2187132 | 0.6501718 | 0.4297007 | 5 | Weekday |
2020-03-13 | 84085 | 0.7760214 | 0.2239786 | 0.6355952 | 0.4444550 | 6 | Weekday |
2020-03-14 | 42858 | 0.8008339 | 0.1991661 | 0.6368471 | 0.4405245 | 7 | Weekend |
2020-03-15 | 59580 | 0.7864733 | 0.2135267 | 0.6226891 | 0.4475327 | 1 | Weekend |
2020-03-16 | 84271 | 0.7776921 | 0.2223079 | 0.6440000 | 0.4374340 | 2 | Weekday |
2020-03-17 | 88512 | 0.7784378 | 0.2215622 | 0.6460000 | 0.4343592 | 3 | Weekday |
2020-03-18 | 84720 | 0.7780674 | 0.2219326 | 0.6529349 | 0.4356941 | 4 | Weekday |
2020-03-19 | 88220 | 0.7791581 | 0.2208419 | 0.6542159 | 0.4336205 | 5 | Weekday |
2020-03-20 | 85200 | 0.7782588 | 0.2217412 | 0.6534353 | 0.4371127 | 6 | Weekday |
2020-03-21 | 54122 | 0.7906716 | 0.2093284 | 0.6357778 | 0.4476368 | 7 | Weekend |
2020-03-22 | 56133 | 0.7887630 | 0.2112370 | 0.6340714 | 0.4468673 | 1 | Weekend |
2020-03-23 | 88065 | 0.7779287 | 0.2220713 | 0.6543864 | 0.4327485 | 2 | Weekday |
2020-03-24 | 86467 | 0.7781631 | 0.2218369 | 0.6490233 | 0.4314710 | 3 | Weekday |
2020-03-25 | 85078 | 0.7800776 | 0.2199224 | 0.6569294 | 0.4332495 | 4 | Weekday |
2020-03-26 | 86411 | 0.7804191 | 0.2195809 | 0.6583953 | 0.4330467 | 5 | Weekday |
2020-03-27 | 92292 | 0.7762086 | 0.2237914 | 0.6413804 | 0.4359966 | 6 | Weekday |
2020-03-28 | 51952 | 0.7926929 | 0.2073071 | 0.6371650 | 0.4424854 | 7 | Weekend |
2020-03-29 | 57823 | 0.7917336 | 0.2082664 | 0.6413043 | 0.4370406 | 1 | Weekend |
2020-03-30 | 80659 | 0.7819309 | 0.2180691 | 0.6548075 | 0.4343719 | 2 | Weekday |
2020-03-31 | 84480 | 0.7802200 | 0.2197800 | 0.6532381 | 0.4353101 | 3 | Weekday |
2020-04-01 | 85961 | 0.7786752 | 0.2213248 | 0.6490175 | 0.4351043 | 4 | Weekday |
2020-04-02 | 87073 | 0.7769011 | 0.2230989 | 0.6514598 | 0.4346468 | 5 | Weekday |
2020-04-03 | 84070 | 0.7788110 | 0.2211890 | 0.6466667 | 0.4371714 | 6 | Weekday |
2020-04-04 | 49554 | 0.7908976 | 0.2091024 | 0.6248283 | 0.4517092 | 7 | Weekend |
2020-04-05 | 48712 | 0.7979905 | 0.2020095 | 0.6459175 | 0.4375924 | 1 | Weekend |
2020-04-06 | 84307 | 0.7802760 | 0.2197240 | 0.6476429 | 0.4332736 | 2 | Weekday |
2020-04-07 | 85712 | 0.7824181 | 0.2175819 | 0.6563392 | 0.4271164 | 3 | Weekday |
2020-04-08 | 83331 | 0.7806317 | 0.2193683 | 0.6509639 | 0.4336081 | 4 | Weekday |
2020-04-09 | 78347 | 0.7807241 | 0.2192759 | 0.6463205 | 0.4352687 | 5 | Weekday |
2020-04-10 | 55153 | 0.7874630 | 0.2125370 | 0.6386909 | 0.4432760 | 6 | Weekday |
2020-04-11 | 45844 | 0.7919326 | 0.2080674 | 0.6229451 | 0.4515967 | 7 | Weekend |
2020-04-12 | 46532 | 0.7939683 | 0.2060317 | 0.6211828 | 0.4479928 | 1 | Weekend |
2020-04-13 | 46158 | 0.7972272 | 0.2027728 | 0.6443913 | 0.4388405 | 2 | Weekday |
2020-04-14 | 82601 | 0.7812166 | 0.2187834 | 0.6468485 | 0.4319197 | 3 | Weekday |
2020-04-15 | 87951 | 0.7762814 | 0.2237186 | 0.6389257 | 0.4388807 | 4 | Weekday |
2020-04-16 | 84453 | 0.7799929 | 0.2200071 | 0.6520952 | 0.4335074 | 5 | Weekday |
2020-04-17 | 85067 | 0.7818723 | 0.2181277 | 0.6575765 | 0.4276041 | 6 | Weekday |
2020-04-18 | 45168 | 0.8024592 | 0.1975408 | 0.6518000 | 0.4320979 | 7 | Weekend |
2020-04-19 | 52811 | 0.7945292 | 0.2054708 | 0.6394286 | 0.4371627 | 1 | Weekend |
2020-04-20 | 85459 | 0.7807893 | 0.2192107 | 0.6520471 | 0.4287670 | 2 | Weekday |
2020-04-21 | 85699 | 0.7816108 | 0.2183892 | 0.6608421 | 0.4256409 | 3 | Weekday |
2020-04-22 | 72605 | 0.7863049 | 0.2136951 | 0.6667448 | 0.4250258 | 4 | Weekday |
2020-04-23 | 74505 | 0.7855832 | 0.2144168 | 0.6646846 | 0.4252198 | 5 | Weekday |
2020-04-24 | 71254 | 0.7867388 | 0.2132612 | 0.6637183 | 0.4243411 | 6 | Weekday |
2020-04-25 | 37658 | 0.8068960 | 0.1931040 | 0.6650933 | 0.4326571 | 7 | Weekend |
2020-04-26 | 40715 | 0.8057726 | 0.1942274 | 0.6658519 | 0.4312907 | 1 | Weekend |
2020-04-27 | 66813 | 0.7884818 | 0.2115182 | 0.6663008 | 0.4269379 | 2 | Weekday |
2020-04-28 | 76816 | 0.7863474 | 0.2136526 | 0.6612549 | 0.4225812 | 3 | Weekday |
2020-04-29 | 89663 | 0.7809773 | 0.2190227 | 0.6550950 | 0.4255155 | 4 | Weekday |
2020-04-30 | 87311 | 0.7808337 | 0.2191663 | 0.6495287 | 0.4293388 | 5 | Weekday |
2020-05-01 | 60206 | 0.7909618 | 0.2090382 | 0.6413167 | 0.4360695 | 6 | Weekday |
2020-05-02 | 51293 | 0.7989337 | 0.2010663 | 0.6497255 | 0.4316768 | 7 | Weekend |
2020-05-03 | 49731 | 0.8004099 | 0.1995901 | 0.6493535 | 0.4259717 | 1 | Weekend |
2020-05-04 | 88535 | 0.7824431 | 0.2175569 | 0.6577514 | 0.4234822 | 2 | Weekday |
2020-05-05 | 88475 | 0.7834158 | 0.2165842 | 0.6598409 | 0.4213055 | 3 | Weekday |
2020-05-06 | 92638 | 0.7778823 | 0.2221177 | 0.6508649 | 0.4303741 | 4 | Weekday |
2020-05-07 | 91437 | 0.7820580 | 0.2179420 | 0.6532857 | 0.4256264 | 5 | Weekday |
2020-05-08 | 84453 | 0.7850390 | 0.2149610 | 0.6592738 | 0.4223414 | 6 | Weekday |
2020-05-09 | 51197 | 0.7994368 | 0.2005632 | 0.6510000 | 0.4299275 | 7 | Weekend |
2020-05-10 | 53891 | 0.8016529 | 0.1983471 | 0.6541869 | 0.4235216 | 1 | Weekend |
2020-05-11 | 85920 | 0.7873753 | 0.2126247 | 0.6609474 | 0.4195996 | 2 | Weekday |
2020-05-12 | 84930 | 0.7875947 | 0.2124053 | 0.6670178 | 0.4180502 | 3 | Weekday |
2020-05-13 | 85600 | 0.7868008 | 0.2131992 | 0.6602222 | 0.4191822 | 4 | Weekday |
2020-05-14 | 89261 | 0.7887132 | 0.2112868 | 0.6687640 | 0.4121621 | 5 | Weekday |
2020-05-15 | 87291 | 0.7864209 | 0.2135791 | 0.6659310 | 0.4180156 | 6 | Weekday |
2020-05-16 | 51975 | 0.7936814 | 0.2063186 | 0.6355146 | 0.4378259 | 7 | Weekend |
2020-05-17 | 51627 | 0.7986468 | 0.2013532 | 0.6432816 | 0.4293102 | 1 | Weekend |
2020-05-18 | 84880 | 0.7863127 | 0.2136873 | 0.6616450 | 0.4203935 | 2 | Weekday |
2020-05-19 | 81332 | 0.7889221 | 0.2110779 | 0.6642222 | 0.4194659 | 3 | Weekday |
2020-05-20 | 82622 | 0.7884312 | 0.2115688 | 0.6600000 | 0.4203602 | 4 | Weekday |
2020-05-21 | 56207 | 0.7979912 | 0.2020088 | 0.6521429 | 0.4263170 | 5 | Weekday |
2020-05-22 | 81477 | 0.7905641 | 0.2094359 | 0.6581481 | 0.4165470 | 6 | Weekday |
2020-05-23 | 49221 | 0.7992516 | 0.2007484 | 0.6396327 | 0.4364804 | 7 | Weekend |
2020-05-24 | 54694 | 0.7997799 | 0.2002201 | 0.6502202 | 0.4228983 | 1 | Weekend |
2020-05-25 | 74895 | 0.7906728 | 0.2093272 | 0.6592081 | 0.4204687 | 2 | Weekday |
2020-05-26 | 83378 | 0.7893662 | 0.2106338 | 0.6613735 | 0.4171964 | 3 | Weekday |
2020-05-27 | 91648 | 0.7875543 | 0.2124457 | 0.6611585 | 0.4145972 | 4 | Weekday |
2020-05-28 | 88087 | 0.7875149 | 0.2124851 | 0.6614091 | 0.4156686 | 5 | Weekday |
2020-05-29 | 86512 | 0.7864848 | 0.2135152 | 0.6592139 | 0.4191095 | 6 | Weekday |
2020-05-30 | 53512 | 0.7959694 | 0.2040306 | 0.6414766 | 0.4369487 | 7 | Weekend |
2020-05-31 | 51556 | 0.8003557 | 0.1996443 | 0.6405825 | 0.4324618 | 1 | Weekend |
2020-06-01 | 54071 | 0.7962974 | 0.2037026 | 0.6345926 | 0.4358159 | 2 | Weekday |
2020-06-02 | 83127 | 0.7867177 | 0.2132823 | 0.6594578 | 0.4243146 | 3 | Weekday |
2020-06-03 | 88455 | 0.7862790 | 0.2137210 | 0.6537045 | 0.4219660 | 4 | Weekday |
2020-06-04 | 87886 | 0.7860459 | 0.2139541 | 0.6657257 | 0.4193273 | 5 | Weekday |
2020-06-05 | 88242 | 0.7872172 | 0.2127828 | 0.6661932 | 0.4166383 | 6 | Weekday |
2020-06-06 | 54976 | 0.7953259 | 0.2046741 | 0.6406239 | 0.4370271 | 7 | Weekend |
2020-06-07 | 55605 | 0.7966713 | 0.2033287 | 0.6492072 | 0.4285046 | 1 | Weekend |
2020-06-08 | 82064 | 0.7874663 | 0.2125337 | 0.6624756 | 0.4210494 | 2 | Weekday |
2020-06-09 | 89601 | 0.7847696 | 0.2152304 | 0.6605587 | 0.4189016 | 3 | Weekday |
2020-06-10 | 77899 | 0.7932600 | 0.2067400 | 0.6705290 | 0.4156536 | 4 | Weekday |
2020-06-11 | 72864 | 0.7941090 | 0.2058910 | 0.6657931 | 0.4173254 | 5 | Weekday |
2020-06-12 | 84881 | 0.7897289 | 0.2102711 | 0.6636095 | 0.4141327 | 6 | Weekday |
2020-06-13 | 53235 | 0.7989451 | 0.2010549 | 0.6438868 | 0.4260167 | 7 | Weekend |
2020-06-14 | 51290 | 0.8003585 | 0.1996415 | 0.6479216 | 0.4278222 | 1 | Weekend |
2020-06-15 | 84772 | 0.7878221 | 0.2121779 | 0.6583550 | 0.4184636 | 2 | Weekday |
2020-06-16 | 92423 | 0.7873055 | 0.2126945 | 0.6641630 | 0.4143233 | 3 | Weekday |
2020-06-17 | 94744 | 0.7861772 | 0.2138228 | 0.6634497 | 0.4133454 | 4 | Weekday |
2020-06-18 | 88026 | 0.7917873 | 0.2082127 | 0.6710455 | 0.4113785 | 5 | Weekday |
2020-06-19 | 89276 | 0.7877546 | 0.2122454 | 0.6675618 | 0.4143219 | 6 | Weekday |
2020-06-20 | 52775 | 0.8021089 | 0.1978911 | 0.6394095 | 0.4221885 | 7 | Weekend |
2020-06-21 | 56656 | 0.7950449 | 0.2049551 | 0.6438407 | 0.4307223 | 1 | Weekend |
2020-06-22 | 87640 | 0.7873336 | 0.2126664 | 0.6630857 | 0.4182223 | 2 | Weekday |
2020-06-23 | 89777 | 0.7865729 | 0.2134271 | 0.6611285 | 0.4194281 | 3 | Weekday |
2020-06-24 | 88766 | 0.7880260 | 0.2119740 | 0.6623503 | 0.4164207 | 4 | Weekday |
2020-06-25 | 84605 | 0.7895301 | 0.2104699 | 0.6669112 | 0.4161456 | 5 | Weekday |
2020-06-26 | 84494 | 0.7905411 | 0.2094589 | 0.6673810 | 0.4160059 | 6 | Weekday |
2020-06-27 | 54964 | 0.7987926 | 0.2012074 | 0.6384404 | 0.4261517 | 7 | Weekend |
2020-06-28 | 56807 | 0.7969920 | 0.2030080 | 0.6436460 | 0.4276762 | 1 | Weekend |
2020-06-29 | 84124 | 0.7883219 | 0.2116781 | 0.6624643 | 0.4170629 | 2 | Weekday |
2020-06-30 | 88676 | 0.7907854 | 0.2092146 | 0.6690056 | 0.4116334 | 3 | Weekday |
2020-07-01 | 87743 | 0.7903091 | 0.2096909 | 0.6673600 | 0.4118391 | 4 | Weekday |
2020-07-02 | 86797 | 0.7915112 | 0.2084888 | 0.6714913 | 0.4099796 | 5 | Weekday |
2020-07-03 | 84352 | 0.7902720 | 0.2097280 | 0.6673929 | 0.4127703 | 6 | Weekday |
2020-07-04 | 53235 | 0.7978670 | 0.2021330 | 0.6447736 | 0.4304311 | 7 | Weekend |
2020-07-05 | 51746 | 0.8033841 | 0.1966159 | 0.6558252 | 0.4213466 | 1 | Weekend |
2020-07-06 | 82812 | 0.7910371 | 0.2089629 | 0.6620000 | 0.4144931 | 2 | Weekday |
2020-07-07 | 84828 | 0.7897554 | 0.2102446 | 0.6623314 | 0.4158297 | 3 | Weekday |
2020-07-08 | 84062 | 0.7896631 | 0.2103369 | 0.6705000 | 0.4163831 | 4 | Weekday |
2020-07-09 | 86311 | 0.7926739 | 0.2073261 | 0.6723372 | 0.4093684 | 5 | Weekday |
2020-07-10 | 84805 | 0.7929638 | 0.2070362 | 0.6748166 | 0.4083014 | 6 | Weekday |
2020-07-11 | 49547 | 0.8060407 | 0.1939593 | 0.6550707 | 0.4212162 | 7 | Weekend |
2020-07-12 | 51368 | 0.8010080 | 0.1989920 | 0.6518824 | 0.4244861 | 1 | Weekend |
2020-07-13 | 82646 | 0.7919394 | 0.2080606 | 0.6681091 | 0.4141640 | 2 | Weekday |
2020-07-14 | 73796 | 0.7962172 | 0.2037828 | 0.6726939 | 0.4101306 | 3 | Weekday |
2020-07-15 | 86027 | 0.7915026 | 0.2084974 | 0.6719419 | 0.4106966 | 4 | Weekday |
2020-07-16 | 86186 | 0.7915558 | 0.2084442 | 0.6699884 | 0.4103219 | 5 | Weekday |
2020-07-17 | 70104 | 0.7975518 | 0.2024482 | 0.6681857 | 0.4120878 | 6 | Weekday |
2020-07-18 | 44889 | 0.8094752 | 0.1905248 | 0.6493933 | 0.4195237 | 7 | Weekend |
2020-07-19 | 54792 | 0.7978821 | 0.2021179 | 0.6497064 | 0.4267229 | 1 | Weekend |
2020-07-20 | 80080 | 0.7917029 | 0.2082971 | 0.6656875 | 0.4169456 | 2 | Weekday |
2020-07-21 | 81540 | 0.7884366 | 0.2115634 | 0.6643313 | 0.4206279 | 3 | Weekday |
2020-07-22 | 74658 | 0.7960022 | 0.2039978 | 0.6685101 | 0.4097618 | 4 | Weekday |
2020-07-23 | 76465 | 0.7943874 | 0.2056126 | 0.6706579 | 0.4128425 | 5 | Weekday |
2020-07-24 | 70332 | 0.7938776 | 0.2061224 | 0.6676143 | 0.4142211 | 6 | Weekday |
2020-07-25 | 41251 | 0.8093904 | 0.1906096 | 0.6457561 | 0.4192383 | 7 | Weekend |
2020-07-26 | 47499 | 0.8024417 | 0.1975583 | 0.6427447 | 0.4247879 | 1 | Weekend |
2020-07-27 | 72922 | 0.7947130 | 0.2052870 | 0.6621655 | 0.4150462 | 2 | Weekday |
2020-07-28 | 75943 | 0.7934655 | 0.2065345 | 0.6646093 | 0.4147979 | 3 | Weekday |
2020-07-29 | 68458 | 0.7956255 | 0.2043745 | 0.6653676 | 0.4157147 | 4 | Weekday |
2020-07-30 | 72801 | 0.7936889 | 0.2063111 | 0.6638069 | 0.4157773 | 5 | Weekday |
We will only need the columns date
and top100share
, which is a simple measure of lexical diversity. It’s the total frequency of the 100 most frequent types (different words) in the corpus on that day divided by the total frequency (column tok
). So, on January 1st, 2020, approx. 43% of all tokens (running words) belonged to the 100 most frequent word forms.
The frequency curve should then show us the distribution of top100share
in the 30 days that fall into the window at any given point in time. So, we cannot simply use date
as the “transition variable” for {gganimate} because we need 30 data points for each curve and not only one (which would not work anyhow). My solution might be a bit expensive, but it works:
- Create an empty dataframe
slw.df
. - For each day
di
in the dataset, select the 30 day window starting with daydi
and save it inret
. - Assign
di
to the columnbegin
inret
. - Write
ret
into the dataframeslw.df
.
slw.df
, the dataframe holding the sliding window data, is going to be much longer than our source dataframe, but we can now use the column begin
as the “transition variable” because each animation step will get all values for the 30 days starting with the date in begin
. Let’s do it:
library(data.table) max.rows <- nrow(res) - 30 slw.df <- lapply(1:nrow(res), FUN = function (di) { if (di < max.rows) { ret <- res[di:(di + 30),] ret$begin <- as.Date(paste(min(ret$date))) ret } }) slw.df <- rbindlist(slw.df) nrow(slw.df) / nrow(res) ## [1] 26.46698
So, our plotting dataframe slw.df
is approx. 26 times longer than our original dataset res
. It might not be very parsimonious but it does the trick. If you have an idea how to do this more efficiently, let me know.
Now for creating the plot:
- By setting the fill aesthetic to
factor(begin)
, the density curve changes its color while progressing through time. We have to switch of the legend, though (this is done byguides(fill = F)
. - The curve will be transparently filled.
- We have a transition through time, based on the variable
begin
. - The shadows of previous time steps are even more transparent and the color of the line (not the fill) changes to grey.
library(ggplot2) library(gganimate) gg <- ggplot(slw.df, aes(x = top100share, fill = factor(begin))) + geom_density(alpha = .5) + transition_time(begin) + ease_aes('cubic-in-out') + labs(title='30 days since {frame_time}', x = "", y = "Kernel density top100 frequency share") + shadow_mark(alpha = alpha/4, color = alpha("grey", .25)) + guides(fill = F) + theme(title = element_text(size = 18), axis.text.x = element_text(size = 14), axis.text.y = element_text(size = 14))
All we have to do now is animating the plot. For a preview, I like to use low parameters for frames per second fps
and duration because rendering takes several minutes for higher fps
and duration
settings. If you want to see a higher-resolution rendering, you can check out my tweet related to this blog post. I’ve used fps = 25
and duration = 20
for the video you’re seeing there.
animate(gg, fps = 5, duration = 10, width = 500, height = 350, rewind = F)
But also on a much lower temporal resolution, we’re seeing that the density curve is crawling to the right during March and April and crawls back afterwards. Basically, this animation transports the same information as the ridgeline plot which was divided by weeks. However, it encodes time by animation stages and the moving window adds some smoothness to the process. Also, it incorporates more data points into each density curve.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.