 Hey folks, I am super excited for today's episode. What we're gonna do today is we're gonna take the climate spiral that I generated in the last episode This was a static image And we're going to update it to make it an animated version if you've been around social media very long You'll likely see this animation pop up every now and then when new data is released And to me it is a masterful data visualization for storytelling You can't help but just watch the years tick by and know that the train is going off the tracks As giddy as I am about the visualization clearly it is very frightening and worrisome But we can have some fun in making this visualization and that's what we're going to do today in our Building off of what we did in the last episode, but bringing in tools from gg animate So if you haven't seen that last episode yet and the episode before that as well I'd encourage you to go back and check out those two episodes. I'll put a link up here So you can go and check those out I'm over here in our studio and I've got a new r script called climate spiral animation r It is the same file that we ended with in the last episode that was called climate spiral r if you want to get this file the data and everything else for this project down below in the description there's a link to a Blog post where you can get everything you need to get going To follow along today, which I strongly encourage you to do we go ahead and run everything in here So we can remember what this produces so you'll recall that this was the visual we made in the last episode From this code. So now what I want to do is bring in gg animate So I'll come over to the packages tab here and look for gg animate. I don't have it installed So let's go ahead and install it and so you can do gg animate this then installs the package And it's been installed and now I can do library Gg animate now. I want to think about how or do I want to set up this animation? Ideally, I would have my line create for every month and every year of data that I have I Don't currently have the ability to do that in my data because I have Each line being a different year, right? And so if I want to break it down I need to add a little bit more granularity, right? So here I have group equals year I if I step by year that I'm gonna get one circle for every year and I'd rather have it go by month So let's see how we might do that instead Well, what we could do is we could again think about what is in our t-data, right? So if we look at t-data, it's a data frame that's got our year our month our Temperature difference and the month number We've also concatenate on the next January. So we go ahead and arrange this by year and month And again, if we look at t-data, we now see that we've got year month and the temperature difference I don't actually want that year 1879 Because that next January is really from 1880. So what's a filter year not equal to 1879 Again, that next January was a trick that we added so you could get a complete loop of a year If we don't have that next January and it goes from January to December and then there's a gap between December of this year in January of next year. Okay, so now if we look at the output of all this We see that we've gotten rid of that 1879 now What I'd like to do is add a row number Because what I'd like to do is basically step over the row number of the data frame using that transition reveal and so I can then do mutate and I can do Step number I'll do one to colon and row on the current data frame and let's make sure that all works Yeah, so now we have that and so if I look at t-data and look at the tail I Now see I've got 1849 rows and we're off to the races this other information in here like annotation Templines month labels, that's all stuff to help with the styling this big chunk of code builds out our plot And so down here now. I'm going to do as I said transition underscore reveal So gg animate comes with a variety of different transitions transition revealed basically adds data and keeps the old data there So let's go ahead and transition reveal and the argument that we'll do is along and we'll set that equal to step Under score number. So let's run this and see if it works. So we get an error message Right, of course, you probably could have predicted that given the length of the rest of the video So what I'm going to do is show you how I would track down this problem and then solve it Right, so I'm going to go ahead back up into my code And I'm going to look at it and try to figure out, you know, what's essential and what's not essential? So a lot of this stuff with like gm call gm hline. This is all styling. So I'm going to comment that out the geom point the end The label the geom point is the final point that yellow circle that we had at the end the label color red fill black those were the Temperatures on the red circles that kind of provide a legend so I'm going to go ahead and cut that and paste that above geom line and Comment that out. I want to kind of keep all this stuff together that I currently have commented out because it's more of the styling The geom text is the month labels. So that's how we got the months Arrayed around the circle again, that's more styling stuff that I'll comment out here as well This geom text indicates the year in the center I'm going to go ahead and keep it where it is because that's something that's going to get updated with each loop Around the year and so because I'm not totally sure if this is the problem I'm going to go ahead and comment that out for now Geom line is the big part that I want to keep because that's the line that makes the spiral, right? And so then we have these different scales Our cord polar and our theme so I'm going to leave that stuff in there for now And let's let's see if that gets rid of our problem. Ah, so that got rid of the problem It's now rendering so when you build a gg animate plot There's two stages where it renders it basically builds out the animation It's making a gif by default and so a gif is a series of PNG files concatenated together and so then it then Takes makes a hundred images by the default and then encodes them into making a gif and so looking at this Animated spiral chart. That's pretty awesome. It's working, right? It's just so mesmerizing to watch. I love it. All right So now we know that something in what we commented out was causing a problem So now what I do is I go back to the stuff I commented out and I bring it back in and see if that causes the problem So I'm going to remove for now this cord Cartesian because we're working in polar coordinates So we'll get rid of that so it's not a distraction Let me go ahead and bring back in this geome text and I want my label to actually be the year So we'll do label equals year and so now what we see is it's continuing to build the circles And it's inserting the years in the middle, but the years are kind of writing on top of each other So it's working kind of and we'll come back and clean that up in a bit. So that's not the problem That's not why we're getting the error message. Okay, so now let's look at geome text again This was the months around the perimeter and so that gets me my months around the perimeter You can kind of see the the spatial size of everything is a little bit off Don't worry. I just want things to work and then we'll go back and make things look good. So, okay. That's good It wasn't the month labels. Let's go ahead now and put in the geome label for again There are isotherm lines and while I'm here. I think I'll also go ahead and grab this geome H line And that is the actual circles for the legend of those one and a half and two degree temperature We now see that we've got our red circles With our labels on top of them kind of coming back up here. The geome point was the point at the very end I'm not totally thrilled about keeping this. So I'm gonna go ahead and delete that Now we come to our two geome columns So it's got to be one of these two right because if I if I left them in we got that error message so let me go ahead and Uncomment the first one. That's where we're getting an error And if we come back up and we comment this one out and Uncomment this one we get the same error. Okay, so I think the problem is in these two geome columns So again, what we were doing in these two steps is basically making a pie chart to go behind our Plot right and so that's how we would get the black background Inside of our spiral chart Okay, so what I'm gonna do is use a different geome and see if that maybe makes the problem go away So what we'll do is geome wrecked. So this is another geome It'll make a rectangle in Cartesian coordinates and then when we put it into polar coordinates It'll circularizer right so we'll use AES and then we'll use X min X max Y min Y max so we have to set those four values So X min I'll set to one X max. We'll set to 13 Y min I'll do Let's what do I have for my minimum value on my scale y continuous so negative two to two point seven So let's do negative two to two And so we'll do equals minus two Alright, so that kind of works We see that we've got this Darker or later gray background and we're also getting a line for I think the edge of The rectangle again if you think of a rectangle going from one to 13 It's circularizing it so one and 13 go up against each other and that rectangle then has a border so I need to come back up here to geome wrecked and I'll want to do a color Equals and I want it to be black And I want my fill to be black Right, and I forgot to add it and I'm surprised it didn't complain to me But I also want to put in here actually it did use the color. So I want to now say inherit Yes equals false. So what I was saying was that it without inherit equals false It was applying the color to be the year, right? So if you watch the animation this line For the rectangle changes as the year goes up, right? So it's inherent a s equals false then we're not gonna be worrying about these x y group or a color Aesthetics we're just throwing them out great. We now have our black donut. I don't want a donut though So let's see if we can plug in that hole Also, I'd like the radius of the donut so to speak or the circle to be wider than my red line So let's come back up here to geome wrecked and for my y max. I'm gonna do let's do say 2.4 That's what I had done before with the geome call and then my y min I was going to minus 2 But let's see if we do like minus 2.5 to see if we can make that hole a little bit smaller So we now get a plot without our donut and we're getting 50 or more warnings So if we do warnings on that we see that it's removing rows because it contains missing values and as we've seen in the past if I set my min or max my values of my Aesthetics for the geome outside of the boundaries of the limits then it's going to cause problems, right? So I see that my scale y continuous goes from negative 2 to 2 And so I if I use y min of negative 2 and a half then that causes problems, right? So that's outside of the limits and so then it says this is outside of limits We're just not going to plot it whereas With the scale y continuous. Yeah, my max here was 2.7 So my y max and my rectangles 2.4. That's less and so we're good I think the reason that we're getting the donut hole in the middle is because we did this expansion thing and You'll recall from the last episode that was necessary to push the months and the circle out to be closer to the title Otherwise, there'd be a big gap up here and I didn't want that. So I think that's the problem So maybe let's go ahead and see what this looks like without worrying about that y min But having the wider donut so that looks better to have the black circle go out further And now we want to plug in that middle part So what I think we'll do is we'll fix the problem with the year at the same time that we plug that hole So what we'll do is here for geome text. I'm going to change that to a geome label now We see we have a label Instead of just the text what the label gets us is a background that basically hides all of the other numbers I'm also noticing that the numbers don't really update as frequently as you might expect. It's a little bit herky jerky I'm not totally sure what that's about We'll we'll get to that in a moment But I think what I want to do here though is let's go ahead and make our background black on that label And make it have more padding and then we should be able to plug that hole So let's come back up here to our geome label And we'll do color equals black fill equals black And then we'll do label dot padding And that takes a unit function So we'll do unit and then let's say 20 pt. I'm not sure if that'll work or not So let's give it a shot. We might have to tweak so that got us a black background That's bigger, but I realize I did color equals black And so our color is black So let's make this white because in the the original version had a white number with a fill of black Let's increase the padding. I'm going to go up to say 50 we're gaining on it. Trust me We now have that rectangle plugging the hole Of course, we also have a white border that I don't want And so what we can add to here then would be label dot size Equals zero and that will get rid of the white border. I'm also plotting that label on top of the lines So let's go ahead and change that order so that we put the label behind the line And that's a lot better. We now have the year number with the black background plugging the hole of the doughnut We still have this problem of the years not incrementing as Frequently as we think it should let's hold on to that So we've done some tweaking with sizing of things with especially the size of that label in the middle What I like to do now is output this to an actual gif And then play around with that animation and get things working that way and then we'll come back and worry about the label So to save that we can come down to this line where we had gg save and instead use annum underscore save I'm gonna instead of it being climate spiral dot png I'll do gif And I'm going to go ahead for now and remove this width and height because this isn't where we're going to set the dimensions Coming to my finder window if I double click on climate spiral It opens up the gif which you can see is a bunch of individual files So it's a hundred different images. Again, these are png files That are concatenated together to make a gif and you can see each frame Each of these individual pngs is a different step in the overall gif if I actually want to watch the gif I can open it with my browser. So let's open that with chrome. So here it is in chrome We can see the default size is 480 by 480 pixels We've noticed a couple things. So there's these white bars on the side. It's a bit grainy Also, we still have this problem with the year So to clean that up, we're going to Use another function called animate which comes to us from gg animate And what we'll do is we'll take our plot or all of our code for our plot And we'll assign this to a variable. So I'll call it a for my animation And while I'm here, I'm going to go ahead and remove these gm call lines because we don't need them anymore So we run that and so you'll notice that when I ran that it didn't render it, right? So if I now type a Now it goes to the process of rendering the image I'll let that run. But what animate does is it allows us to tell gg animate through the animate function Things like the the dimension the resolution of the images How many frames we want how many frames per second we want? The default is 100 frames and 10 frames per second So we can modify all those things. So I'm going to animate a And we're going to set our width to be Let's say five our height to be 4.5 and then we have to give it units Of inches and then we can say res equals 300 to give us a high resolution image So now I have a larger version of the image. The resolution is a lot better I still have these white bars on the side. I can get rid of those I did some tweaking of These numbers to play around with them and if I do 4.155 Those white bars on the side will go away So one of the things that we still have as a problem with this visual is that the year isn't getting regularly updated, right? It's 1888 now and then in a bit it'll jump up to like the 1960s. I think or 70s, right? And now it pauses, right? And then it goes back and right and it's not updating as regularly as we think it would And part of what is happening here. I think is that gg animate does this thing called easing And so easing is dealing with kind of how the transition happens, right? So as more points are added as we basically go down through the data frame How fluidly do we move from one line to the next? And I think the problem is that there's something going on with the easing that causes the year not to get updated Every frame and so what I'd like to try is let's increase the number of frames And let's um and see if we can give each year its own frame or perhaps even each month its own frame This might take a long time to render, but let's give it a shot We we can do is we can then come in here and we can do n frames And so n frames is the number of frames that we're going to have and so that could be like n row on t data, right? Which we saw was 1849 that's a lot of rows a lot of a lot of frames, right? And so then we could then do fps for frames per second So basically if we have 1849 rows and each frame gets its own second Then that's going to be like 30 minutes of a gif which no one wants, right? So if we do n row on t data divided by 12 so that we kind of think about a year in terms of seconds. Let's divide that by 60 seconds 60 minutes, right? And so basically what we should then get is a frame rate of 0.04, right? So 0.04 times 12 then Would be about half a second so each year would get half a second And so then over the 140 years or whatever, uh, it'll take just over a minute. I don't know how we'll see Let's run this and see what happens. So it turns out that I used the wrong frame per second And that this made it just run exceedingly slow And i'm not going to go back and change the frame per second rate because you can see that this problem persists, right? We're getting multiple laps here and it's still at 1888 So we need a different solution the solution is to go away from transition reveal to transition manual And so what transition manual will do is it will kind of cut out all of that easing So let's go ahead and try to run this so to test things out I'm going to go ahead and turn off the end frames and the frames per second Just because it makes things take really long time to run. So we'll go ahead and Run this with the transition manual as well then as the animate And I gave it the wrong argument instead of a long it should be frames So it's now complaining about each group consisting of only one observation I think it wants to set the frames to be like the group, right? And so if I use the group which was the year then we'll get one loop One circle basically for each year So let's go ahead and do that and you know now that I think about it I'm not so worried about that because if you go back to the ed hawkins version We would basically get one year every half a second And so your eyes really can't process things that quickly to realize that it's not racing around You basically get a year Every second or every half second. So let's give that a shot and see if we can live with You know, not perhaps being able to draw each year month by month. So here we see the result of using Transition manual is that the year now is incrementing every year and that looks pretty good But we're only getting one loop with each iteration. So we want to change that So what we can do is give an argument to transition manual Which would be cumulative Equals true. So that did take a few minutes to render. I'm not totally sure why this took so long I felt like it was a pretty minimal Um set of specifications. I suspect the big size of this as well as the resolution Maybe affecting how long it takes to do the rendering, but on the whole I think this looks pretty good I think there's enough chaos of what's going on with the image that you you don't notice that it's not laying it down month at a time um, I It bugs me a bit that I can't figure out how to do that month at a time process I have an idea for how to deal with that, but that's kind of beyond what we're doing with for today Maybe I'll come back and redress that in a future episode So this is a gif and as I've said a gif is really a bunch of png files all tacked together Say we wanted to make a movie. Well, we can take largely the same code as I have here And we would then add a renderer. We'll do renderer Equals av renderer and then we give it the name of the file and so I'll do figures climate spiral But I'm going to do mp4 And so all the other settings should be the same and it'll create an mp4 file So using the av renderer as the value to the renderer argument in the animate function Produces this mp4 that on a mac at least I can see on a quick time player And it goes all the way through till 2022 and then stops You can obviously replay it. Um, so I think this looks really nice I'm really happy with the way it turned out Again, I don't know that it needs to be quite so big if I was again putting this up on twitter or wherever I'd probably want that original smaller size and things would probably also render a lot quicker I think before I go ahead and push things up to github. I'll probably dial back to the original default Sizes, but maybe still use that higher resolution. So it doesn't quite look so grainy Anyway, um, I hope you found this as exciting as I did I'm really excited to see this come to fruition when I started. I wasn't actually sure that I could actually do this It's pretty cool that it worked out in the next episode We're going to take this visualization and the code and data We're going to modify it to look like a visualization that was posted in the last few months by the folks at gis s from nasa We'll see that there's a few strategies that we can take To adapting code to get it to appear slightly differently and perhaps we can think about, you know, what do we like better? And what do we like worse so that you don't miss that episode? Please please please be sure that you subscribe to the channel You've clicked that bell icon and you've given my video a thumbs up. Thank you so much for walking through these videos with me I've really been just, you know, overjoyed by the feedback people have been giving me that you know These videos just really get them excited. So Tell your friends and let's expand the the community of people that are watching these videos. Thanks and we'll see you next time