 Hey folks, we're in the middle of a series of episodes looking at different approaches to visualizing climate change data using our ggplot2 and other tools from the tidyverse. I've been inspired a lot recently by Ed Hawkins, who is a researcher at the University of Reading in England. In the last episode, I made these temperature lines, where each line represents a different year, in my case going back to 1880. Ed's data goes back even further to 1850, and my data is normalized so that the average temperature was zero between the years 1951 and 1980. Later, I found this visualization, which I think many of us have seen on social media, albeit in an animated version. This he calls a climate spiral. While I'm not going to animate it today, what I do want to do is show you how I figured out that we can use ggplot2 to generate this final screen grab of the data visualization. The main difference is between what Ed's got and what I've got is that we've got different data sources. Mine is coming from NASA GISS. Also, my data was scaled between the years of 1951 to 1980 to have a zero temperature change over that period of time. So the scaling might look a little bit different, but I think we'll find that the visualizations look very similar. So I'm going to start by going into my code directory, and then copying my temperature lines dot r script. And so again, I can do that here in our studio by clicking on that gear icon, copy that I'm going to call this climate, a spiral dot r. If you want to get a copy of my project, along with data and all the scripts down below in the description is a link to a blog post that's got instructions on how you can get everything that I've got here. All right, so I've got climate spiral dot r. And she might be saying, Pat, why are you starting with climate spiral dot r instead of a fresh screen? Well, the reality is that those lines that I showed you again one line for each year is a climate spiral, albeit in Cartesian coordinates. That's right. So if you take a line, and you then in Cartesian coordinates, and then you put it in polar coordinates, you'll get a circle, right? If you take a stacked bar, again, in Cartesian coordinates, and then you put it into polar coordinates, you know what you get? You get a pie chart. So cord polar has a limited number of uses. And I think we're going to use it here again, to make these climate spiral diagrams. I'm going to go ahead and run all of this so we can regenerate those temperature lines. And then we'll use that to go ahead and start on our climate spiral. We've got this very hideous looking plot down here in the bottom right corner. That reminds me to come down here to gg save. And I'm going to change this to be climate spiral dot PNG. So I'm not writing over my temperature lines PNG climate spiral dot PNG. Great. And again, that with eight height four and a half is the aspect ratio of a YouTube thumbnail. And so I'm going to use whatever we come up with as my thumbnail, which you probably already know, since you're watching this video right now. All right. So again, looking back at this, we load the tidy burst, we generate T diff, which is a data frame that has the year, the month and the temperature difference again, from that normalized level. We then generated last December last DC. And that is basically our way of getting the lines to go back from January to December of the previous year. And similarly, we also have next Jan, which is the way to get the line to go from December of this year to January of next year. So I actually am not interested in keeping. Well, let's see what happens if we don't keep either of these, right? So I'm going to go ahead and comment these out for now. And I will also comment out this bind rows. And instead, what I'll use is T diff and pipe that in. I'm not totally going to delete it because we'll need one of those here before too long. We have this annotation in here, which I'm going to go ahead and comment this out for now. I kind of want to simplify things a little bit so that it's not so hard to figure out where we are in all the data. And again, if we looked at this visualization, as it was, we now see that we've got things kind of trimmed between January and December of each year. I also have this line thickness. I'm not going to use that that we didn't see that in the final version that Ed Hawkins had made. So I'm going to go ahead and remove the stuff for scale size manual here. And then I think back up here somewhere, I had this year as 2022. I'm going to go ahead and remove this as well. And then I also had a geome text, which was right here as well, which was the way that we indicated the year. So I'm going to go ahead and remove that. And then one other thing I think I had in here was our AES for the GG plot. I had size for this year. Again, that was one of the things we'd used to make the line a little bit thicker. So we'll go ahead and remove all that. So again, I'm just kind of cleaning things up a little bit. And now what I want to do is go into where we had cord cartesian. And I'm going to go ahead and comment this out for now. I'll probably end up deleting it. And we'll do a cord polar. And we'll go ahead and add that in. So what we get is a very odd looking spiral plot. And one of the things you'll notice is that January and December are effectively 12 o'clock on a clock face. We would like to have from the original plot December at 12 and January at one. And so to get that, I need to basically insert data, right? And you'll notice if you look closely at these lines, they don't totally connect. They don't butt up with each other exactly, right? And so that, again, is because we are missing the next January data, right? So the December into the next January. So I'm going to go ahead and uncomment the next January. And then I will bring back in this bind rows. But I'm going to remove the last December. So we have Tdiff and next January. I'm also going to remove last December from my factor. So let's go ahead and run all this. And there we go. We have December at the top then January for every March. And you can see that yeah, March and September are three and nine o'clock on a clock face. And that looks pretty good. There's our spiral diagram. We're done. Well, not exactly. So what we now want to do is let's go ahead and remove some of the extra stuff, right? So we've got the legend in there. We've got the axis labels. So I'll start with removing the legend down here in the theme. I can remove all these attributes of the legend. And then back up here where we had scale color verdens, I can replace all this stuff for the guide. Remember, we made a white border around the color bar. But instead of that, I could put in quotes, none to get rid of the legend. I'm also going to get rid of the axis tick marks. I don't need those. So I'll go ahead and comment that stuff out for now. Because who knows, we might need it eventually. And what I see is that I still have the basically the scale, why continuous in here, again, in this margin over on the left, I want to get rid of that. And so I can get rid of that by doing axis dot text dot y equals element blank. Okay, so that stuff all goes away. And we're in good shape. We also have the axis title. So I'm going to go ahead and remove that. So we'll do axis dot title equals element blank. And you know what, I can also come back up here to labs, where I had this and replace that with null. The original was global temperature change 1850 to 2017. So we'll say global temperature change, 1880 to 2022. Right. And I forgot a comma here. And it's complaining about my axis title. I'm not sure why it's got axis title title x and y. And the documentation says it inherits from axis title, but whatever, I'll just go ahead and put in y here. And so now we've gotten rid of that. We also have this white border around the panel background. So let's go ahead and I'm just going to go ahead and remove that actually. All right, so I'm noticing a few odd things. There's actually a white border around my plot. Now my aspect ratio is wider than the plot probably wants to be. And so it's putting in a white border. So I wonder actually if that's not a white border, but it's actually kind of a null border. It's a transparent border. Also, if I look closely, there's actually tick marks in there. So let's come back in here and I'll go ahead and put back in the axis ticks and do element blank to get rid of those tick marks. And I'll get rid of these tick lengths. And then for my panel plot background, I'll also then do color equals and I'll make it the same court color, this set of fours, and that gets rid of those tick marks and gets rid of that white border. So this is starting to shape up to look a lot like what Ed Hawkins had. I still see in there that I've got my white line at zero. Ed's version has those two red circles with labels. So let's go ahead and insert those. And again, back up here at geome h line, we have y intercept equals zero. So he had one and a half and two. And so let's go ahead and put in red lines at one and a half and two. So the 1.5 and 2.0. And then I'll do color red. And so now we've got lines at one and a half and two, we're in good shape. All right. And now we need to put labels on those, of course. And so we can do that with geome text. And so I will go ahead and put in here, temp line. And we'll make that a till for x, the x position, I'm going to put that at 12. Because that's where the December was. And then for y, I'm going to put that to be 1.5 and 2.0, right? And then my labels, I'll make that 1.5. And again, our unicode of u, 0, 0, b, 0, c. Again, this part is the unicode for a degree symbol. And so then I'll copy this and then put it in for two degrees. All right. And so now I want to put make sure I've got this loaded. And then I want that to be on top of geome line. So I'll then do geome. I'll actually use label. And we'll use data equals temp lines, aes, x equals x, y equals y, label equals labels. And then I'll also do inherit dot aes equals false. And I'm going to want this color to be red. And I forgot a plus sign here. All right. So I've got my labels on there. But I've got a red border and a white fill. So I want to actually make the fill black. So again, we'll come up to geome label fill equals black. And then I want color to be red for the number, the text in there. But I also want label dot size. So that'll be the thickness of the line, I'm going to make that zero. So I have no border effectively. I'm basically want geome text, but I want a black background, right? So I'm using geome label and removing the actual border of the label. So if we compare Ed's version to my version, we can see that we're kind of gaining on it, right? And so this actually is looking pretty good. Let's see. So one thing I notice is that the center of Ed's is a little bit more open than mine. And so let's see if we can't change the Y limits. And so where do I set the Y limits? Let me remind myself. So I actually don't set the Y limits. So let's do that. So up here in scale Y continuous, we'll do limits. And we'll do that from let's say negative two to two. Again, we're thinking in terms of X and Y, but it's taking X with a chord, a polar and circularizing that, but we still have the same Y value, right? So Y is basically transferred to the radius and X to the theta or the angle. And so that gives us a more open center to our overall plot. You know, maybe that's a little bit too much, but let's leave it there for now and we'll see what we can do with that. So there's two things that I notice that are different between my version and Ed's version. So the first is that his dates, his months are basically tangential to the spiral diagram. The other thing is that his plotting window is black and circular, whereas mine is black and square, and it's on the outside of everything, right? So I'm first going to take on those month names, and then we'll come back and think about how we can make the interior black, a black circle in the exterior than that dark gray. What we'll do to start is remove the axis text X, right, the X labels, which are the months. And so here we'll again, we'll do element blank. Again, that gets rid of those months. Now what we're going to do is we're going to create a data frame of the months and their positions. And so I'll do month labels. And we'll call that a table. And then my X positions are going to be one to 12. My labels will be month dot ABB. And then my Y, I'm going to make say 2.4. And so now I of course have month labels. And again, we have that data frame. And then in here, I can go ahead and I'm going to put this on top of everything, I'll do geom text. And I'll do data equals month labels, right, and then AES X equals X, Y equals Y label equals labels, double check that I got all that right. Yep. And we'll then go ahead and do inherit dot AES equals false. So let's run this. Let's go ahead also put in color equals white. And it's complaining that it's got missing values. And that's because I set a Y position of 2.4, which is beyond the limits of two. So let's go and put that up to 2.4. And so now we get our months. And it looks a lot like what we had before, right? But all of our labels are horizontal to the horizon, right? Instead, I want those to be tangential. So geom text has an argument for the angle. So each of these I can give a different angle. So December, which is in position 12 has zero angle, right? Whereas January needs to be tilted a bit, February bit, and so forth, right? So to help me to think about this, what we're going to have is a seek go from zero to 360, only it's not quite 360, right? It's 360 minus 360 divided by 12. And then we want our length dot out to be 12, right? So we want 12 values from zero to 330. And I think that's pretty good. I'm wondering though if we might want things in the opposite direction. But let's try this and then we can tweak things along the way. So I'm going to go ahead and grab that and come back up here to our geom text, put these things on separate lines. And they'll do angle equals that sequence. And isn't that awesome? Trust me, we are gaining on it. All right. So I think what we want is something different, right? Let's come back up here and think about this again. Where were we? Right here. So I think what we'll do is from 360, basically 330 down to zero. Let's try that out. Now we have December at the top, January, February, March, and everything is tangential to the the the circles, right? And so that looks really nice. You know what, I might want to bump that out a little bit. And so what I'll do is I will come up and make this let's say 2.5. And as we saw, we then need to increase our limits on the y axis to 2.5. And so that gives us a little bit more breathing room. Awesome, right? So again, we're gaining on it. Now what I want to take on is that the Ed Hawkins version has a black circle on the inside, and then dark gray everywhere else. Obviously, I have a black square surrounded by a gray square. And inside my black square is my spiral diagram. So looking at this and thinking about the Hawkins version, I am thinking that I need to make a circle inside. And so what is a circle? Well, a circle is a pie chart where it's 100%. So what if I turn everything dark gray like I have in the far background? And behind everything, I put in a black pie chart. That'd be pretty cool, right? And I think that will get us the effect we're looking for. So again, I'll come back up here. And instead of black, I'm going to make that that this set of fours to get that dark gray background. I want the pound sign for hexadecimal. So now everything is gray. And now we can put our pie chart in the background. To do that, we'll come back up here. And again, we want it in the background. So we want that right after ggplot. And we will do geom call to create a column plot. And my data I'm also going to grab from the month labels. And then aes and my x will be x. My y is what I want things to go to. So let's make that like 2.2. And then my fill, I'm going to make black. And then I'm going to inherit.as and that's going to be false. Let's go ahead and put that on a separate line. And we need the plus sign. Well, we're getting there, trust me. So we have two problems. So problem one is that our bars are segmented, right? And again, you think about a bar chart, ggplot when you use geom call put spaces in between those. And that's because the default width is like 90%. So if we make the width one 100%, then that will fill everything in. So let's do that. And then we'll worry about all the other problems. So in here, then we can do width equals one. So that gives us a solid black background. So we've got this gap in here. And I wonder if we have a problem with our data frame, where we again, assigned each month to a different number. But we created 13 months. And so we had 13 months for the spiral and 12 months for the pie chart. I want to come back up here to my t data. And you know what, I'm a little bit worried about t data. So let's look at t data. And we can then do t data, count on month number. And I'm worried, we got a zero. So I think that's a problem because that zero came back from when we were also incorporating the previous December. And so I don't think we want this minus one. So we'll go ahead and remove that. And so of course, now if we do our count on that, we see that we start at one and go to 13. Ah, so we'll see if that screws anything else up. Hope not. All right. So let me go ahead and remove that bar plot. And we'll see what's going on here. So we'll move that and let's see what looks like. So removing that bar plot. Now that we've again bumped everything, you know, we remove that minus one, we now need to turn everything and kind of recalibrate. And instead of starting at zero, I want things to start basically five minutes off. And so we can set that in code cord polar. And there's an argument start and start measures things in radians. And if you remember back to a high school geometry, a circle is two pi radians. So we can do two times pi. And then each five minutes is, you know, one 12. And so this would be then point five to radians. Okay, so that gets us back to having December at the top. Let's go ahead and add back in our bar or pie chart, right? So we'll go ahead and uncomment that we now see that once we add back in the pie chart, the problems reemerge. And so I think what we need to do is go ahead and add a half a unit to our pie chart, so that that closes everything in. So again, if we come back up to our pie chart, our GM call to x, we can add 0.5. That closes that in. But what I'm noticing is that we have a hole in the middle, right? And I think that is because you can kind of see the black starts right about zero, right? And so we have a bar chart that goes from zero out to two and a half or whatever we said. But there's nothing on the inside, right? And so we need to make another pie chart on the inside, going to negative values inside, right? And so we can do that by coming back up here to our GM call. I'm going to copy this down. And we'll also do minus two. And I just want to double check that our y limits. Yep. And so we see now that we've plugged in the middle. That looks good. I think I want the pie chart actually to be a little bit bigger. And so let's increase that a smidge. And so yeah, back up here. Let's go out to like 2.4. All right. And so now we need to move our months out a little bit. And so back up here again, we could do say like 2.7. And again, we're removing stuff. And that's because we've got our limits on our y axis. Let's put that to 2.7. And that looks really nice. Okay. So one thing I'm noticing is that there's a big gap surrounding my plot, right? And if I turn back on my grid lines or turn off my element blank to turn back on my grid lines, I noticed that there's a fair amount of extra space. And if I come back in, and I put expand in here to be C0 comma zero, which should get rid of that expansion. And if I put it down here for the next one as well, I see that doesn't really that doesn't change anything, right? And so I think what I want to do is on my scale y continuous, I want to increase the expansion. So the first is addition, and the second is multiplication. And I see that's shrinking things down. And so maybe what I want to do instead of one as my expansion, let's try minus one. So that expands it out, right? That looks good. Although it's maybe a little bit too expanded because I've lost December and March and September and June down here at the bottom. So let's make it 0.8. So minus 0.8. Too much. Let's go back to 0.7. Negative 0.7 here. And that looks really nice. We still have those grid lines that I'd like to get rid of, though, that was there just to kind of see what was going on. And there we go. I think that's pretty slick. Now what I want to do is let's go ahead and see if we can't put 2022 in the middle, as well as label that final point. We had a data frame earlier that generated that this annotation. So let me go ahead and remove those comments. And again, if we think about annotation, that gives us the year, the month, the T diff and the month number. And so I'm going to use that with a geome point after my geome line. So the geome point data equals annotation AES x, it's all the same stuff, right? So month number, y equals T diff. And I want, yeah, that should be good. And we'll also do color equals year. And I don't want the grouping. I don't know if that matters. We had that up ahead here, right, for making the line grouping by year. And so here, I'll then do inherit AES equals false. And I now see I have that circle at the end, let's make it a little bit bigger by doing, let's say, like a size equals two gives us a little bit bigger circle. Now let's put a white date in the middle. So let's come back up here. And we will then do another geome text. And we'll do AES x equals, let's say one, and y equals, let's do negative two. And then let's set our label to be 2022. And let's go ahead and run this. So our label is down a bit, let's see if we can't raise it up to get the vertical position, right? So we currently put a negative two. Let's see what happens if we raise that to say negative 1.8. All right, we're gaining on it. Let's go up to one point negative 1.3. So I think that looks pretty good. You know what, I'm going to leave that the color of the year, because I think it's good to kind of give a further indication of kind of the temperature, so to speak, of the year. If we wanted it to be white, we could of course overwrite it and do color equals white up here in the geome text. I'm going to leave it just the way it is. I'm pretty happy with that. I don't want to be humble, but I think I've done a pretty good job of replicating the visual that Ed Hawkins originally pioneered this with. One thing you'll notice that's a little bit subtle is that the line connecting the two months is straight. Whereas with mine, you'll notice there's a bit of a curvature. I think that's because of how the cord polar works in doing the transformation. It doesn't connect two dots. It's taking a line and then transforming it into the polar coordinates. I actually like the curved shape a little bit better. It looks more smooth, more organic, more natural. I think that looks pretty good. I noticed that his year in the middle is a little bit bigger than mine. I'll probably increase that before I make the thumbnail and post the code. I think maybe his text here is gray whereas mine is white. But I'm pretty proud of how we figured out how to put those labels arrayed around tangential to, again, that black circular plotting field, which I think is pretty cool. In the next episode, we will take this the next step, I hope, in trying to animate our version to match the animated version of the Ed Hawkins climate spiral. I just think that is such a compelling visual and tells an amazing story about climate change. So that you don't miss that episode, please, please, please be sure that you subscribe to the channel. You click the bell so you're getting notifications and give me a thumbs up for all the hard work that we put into making this visual. We don't usually see cord polar in a lot of different situations. I generally hate pie charts, but I think this is a pretty effective use of polar coordinates. Let me know what you think down below in the comments. All right, well, we'll see you next time for another episode of Code Club.