 Hey folks, in the last few episodes of Code Club, I've been spending a fair amount of time trying to recreate different versions of climate spirals. These are spirals diagrams indicating temperature deviations from the average temperature between 1951 and 1980. The data I'm using comes to us from GISS at NASA here in the United States. We've looked at climate spirals developed by Ed Hawkins in the University of Reading, people at NASA here in the United States. We've made GIFs, we've made MP4s, we've made interactives. So one of the things that I was intrigued by at the end of the MP4 made by NASA was that we get to 2021 and then tilt up the climate spiral showing it in three dimensions so that each stack is a different year and then radially we have the temperature deviation. It goes from April on the left to October on the right. And so I think those two months are perhaps arbitrary. They're basically three o'clock and nine o'clock on a clock face, but I thought this was a cool visual and I would like to build this today with you. I think it's got some unique challenges and what I'd like you to do before I get going is to think in your own mind, how would you go about generating this? You know that you're going to need a geome from GGplot. Well, what geome are you going to use to draw those lines? When we go through the video, see if what you said agrees with what I end up doing and if you do it by a different approach by all means, let me know down below in the comments and I'd love to see what your thoughts were on how you would pursue generating this figure. As always, if you want to get the code and the data that I am working on here in our studio, down below in the description is a link to a blog post that will get you everything you need to get up and running. I have a brand new R script here loads in the tidyverse library as well as that initial T data data frame. So T data data frame, of course, has each the year, the month and the T diff. I'm going to go ahead and save this into my code directory. And I'm going to call it climate tornado because it kind of looks like a tornado. Great. And so what I want again are the months, April and October. So I'll go ahead and bring down T data. And to get those two months, we can do a filter on month equals equals April. It's actually going to be a PR right because it's a three letter abbreviation or with the vertical line that pipe month equals equals October. And so now we have the year and the month for all of our April and October data. So the approach I'm going to take is to use geome segment to draw horizontal lines going from April to October. And so geome segment needs four arguments, it needs a y, a y and a x and an x and and so to get those x and x and I'm going to use different columns. So I'm going to make a April column in an October column. And then the y is going to be the year. And so that that year will be the y as well as the y end. So let's go ahead and do a pivot wider. And we'll do names from equals month. And then values from equals T diff. Great. And so now we can see we've got our year and our two months, we could go ahead and feed this into ggplot as and again, we'll do x equals October on the left right. So we'll do OCT and then x and will be April. And then our y will be the year and our y end will be the year as well. And then we'll do geome segment. So that doesn't quite look what we'd expected. It's actually fairly close, but just a little bit off. If we look at the original version here from NASA, we notice that zero is not running right through the middle of the diagram, right? Zeroes are offset. Again, the x axis is a month with the left being on August, October and the right being April. And so what we need to do is subtract, say like the October numbers from a more negative number to kind of pull those out. Basically, we need to create space in the middle. We saw this before when we're looking in polar coordinates, and we had a negative radius, right? So we added a value to the radius to open up the donut, so to speak, to get the climate spiral, kind of when we're looking down on this particular visual. So what I'm going to do is I'm going to take a constant like minus four, and then subtract all the October values from that to basically pull those off to the left, opening up some space in the middle. Now I don't have an x axis on this, what I have are these four grid lines. And so what I'll need to do is to figure out what is the zero line for October, and what is the one degree line for October, right? And so we'll be able to figure that out with a little bit of algebra. So again, let's come in here to our x, getting mapping October on the x, and I'm going to take, say, negative four and subtract October from that. There we go. Now we get something that looks like that tornado plot that we saw from NASA. And what we can see again is that we have negative four here, which will be zero, right? So that negative four will be zero because if we think about our mapping here, if October is zero, then x is going to be negative four, and then one degree will be negative five, right? That'll be out here. Yeah, right out there, right? Cool. So next thing I want to add to this is our color. And so the color we're going to say, I'm going to say as an average T. And so let's create our average T by coming up here, and we'll do a mutate on AVET. And that's going to be the average of April and October. Actually, I don't want it there. I want it down here after the pivot wider. And so we'll do oct plus APR divided by two. And that's going to get us the average temperature of those two months. And then we'll use that as our color. Now what we want to do is go ahead and add in our color gradient, right? And so we saw that we could do this scale color gradient two, right? And so the low mid and high. So low is going to be, I'll do dark blue, high, I'll do dark red, mid is going to be white, which is the default. And then midpoint will be zero. Great, we've got our desired color scheme. That looks pretty nice. So the next thing I want to add to this are those vertical lines. And so coming back here, let's go ahead and do GMV line. And we'll do, again, it's going to be negative five and negative four. Those are going to be the plus one and zero lines for the October, as well as then the zero and the one. And that is going to be our x intercept. So then write that just to be explicit. And then my color, I'm going to make this gold and see what that looks like. And of course, those are on top of the lines. The original is behind the segments. So let's go ahead and move that back up here before the segments. And that now is falling behind them. Let's go ahead and start playing around about a bit with the styling here. So we can get things looking the way we want. I'm going to go ahead and turn off the legend. So I'll go ahead and do guide equals none. That gets rid of that. So let's go ahead and remove our labels, we'll do labs x equals null, y equals null. And of course, the title is already null, but that's gone now. Let's go ahead and get rid of those x axis labels, and the y axis labels, those years, we're actually going to put right on top of the plot. And so then we can do that with theme. So we'll do axis dot text equals element blank. That gets rid of those, we also want to get rid of those ticks. So we'll do axis dot ticks equals element blank as well. That gets rid of those good. So let's get rid of those grid lines. And so we'll do panel dot grid equals element blank. Now we have that gray background. I'm going to go ahead and turn the background of the panel as well as the plot to be black. Maybe I'll put that up here at the top. So we'll do plot dot background equals element wrecked will do fill equals black, as well as panel dot background element wrecked fill equals black. And so now we have a black background on all that. I'm noticing that our lines extend beyond the top and bottom of the plot. Again, looking at the original, it extends above the top, but not the bottom. And so what we can do maybe for our scale y continuous, so we can do scale y continuous. And we can then do limits equals 1880. So BC comma 1880 comma let's do 2030. So that will get the limit to be 1880 at the bottom, and then the top to be 2030. Actually, you know, for this, this bottom limit, we could always put in na. And then we can do expand equals see zero comma zero that expand again, does removes the expansion layer at the bottom and the top. And so we added the 2030 to go nine years or eight years into the future. So our lines extend a little bit up past the top. Very good. I'm going to go ahead and save this to a file, because we're getting to the point now we're going to start changing the sizes of different things and the actual location of different labels. And I want to make sure that those go to the right place. What we see in this lower right plot panel isn't always the way things turn out in the final version. And so by putting it into an actual file on the computer, we know what it's going to look like. So I can do g save. And I can then put it in figures climate tornado.png and I'll do width equals 4.5 height equals 4.5 and units equals inches. So it looks pretty good. I might want to change the aspect ratio a little bit to make it a little bit shorter and squatter. And so we could also do that by changing the amount that we subtract October from. But let's go ahead and make our height say 3.5. So I think that gets us a squatter better looking image. One thing I notice when I kind of change the size of this file is that if you look really closely, there's a white border all the way around the plotting window. So let's come to our panel background. And let's make sure that the color is black. And sure enough, that got rid of that white border. I'm not sure why that white border is there. But that's cool. Okay, the next thing I want to do is let's go ahead and change the thickness of our line segments, those horizontal segments, because they're just thin enough that we can see some of the black background showing through. We can also see those vertical golden bars showing through where our segments are going over top of those. And so we can come back up here to GM segment, we can do size equals, let's try one, I think the default is 0.5. But let's try one. And so that looks a bit thicker, maybe it's too thick. Let's try 0.75. Again, I think 0.5 is the default. So I can still kind of see the yellow in the background right in there, if you look closely. Maybe we'll just take it up to 0.9 and just call it good. One thing I'm also noticing is that the ends of my segments are squared. I can't reproduce the actual appearance of the NASA version. But they do look roundish, right? So one of the things I wish we could do is to figure out how to do changing depth as we go across the months that are facing us, right? Because it's basically the bottom of the clock face, right? So it's like March, April, May, June, July, August, September, October, right? And so we kind of miss a bit of that texture. But one of the things I do see is that it does look like a round cord. We can actually get that effect by coming up to GM segment and doing line and equals round. And so you'll notice that we now have nice little round caps at the end of each of those segments. Cool. Again, I don't think we can get the texture or kind of the 3d feel of each of those lines. But I think this looks pretty good. So the next thing I want to do is go ahead and put labels on those grid lines. And so I'll say grid labels. And we'll make a tibble out of this. And so the x positions are going to be a vector. And again, it's going to be minus five, minus four, zero and one, again, that minus five and minus four, because we subtracted our October values from negative four. And then y is going to be at the top. And so you'll recall that we made our limit 2030. But the most recent data we have is 2022. And so let's go ahead for y and we might adjust this, but let's go ahead and put in 2028. So also want to put in labels. And here we're going to put in a vector of texts, going from the left to the right. And so that'll be plus one backslash u 00 b zero c. So that's the unit code for one for a degree sign. And then we'll also do zero. And then we'll also basically do the same thing all over again, but for the right side of the figure, right? And again, we'll put this there, right? And so again, it's going to go one, zero, zero, one. And now what we can do is we can use a geome label, because we want that background, right, the black background, that we're going to put on top of geom view lines, we'll do geom label, data equals grid labels. And then we'll do a s x equals x y equals y label equals labels. And then we'll also want to do inherit dot a s equals false. So we see we have a white background and black text, we need to make gold text and a black background. So again, we can come back up here to geom label, and we can do fill equals black, color equals gold. And then we'll also want to set the label dot size to be zero. So those labels are in the right place. They look well formatted. They do seem a bit large. They're also getting clipped at the very top. We can change that clipping so it doesn't get clipped. But let's start by shrinking the font down a bit. And to do that, again, we can come back up to geom label. And then we can do, let's do size equals three. I never quite know what the size is for geom label. So I think that looks pretty good. I might actually want to move it up a little bit. So we have a little bit more length on the yellow bar. So to do that, we'll change the Y position. So let's go ahead and put it at 2030. So we clearly see that our numbers are getting clipped off there up at the top. And so we can fix that clipping by coming up here and I'm going to go ahead and add cord Cartesian and we'll do clip equals off. And again, that will turn off the clipping. So basically clip equals on as the default. And it clips all of the plotting to hide anything that's outside of the plotting window. And there we go. We now have our numbers right on the grid lines. And I think they look pretty well portioned. The final thing I want to do is add the years to the middle of our tornado here, right? So looking at the original version here, you'll see it goes from 1880 to 2021, basically 20 year increments until 2020, and it skips 2020 to 2021. 2021 is also the last full year that we have data for. If I were making this from scratch without trying to impersonate what they're doing, I would probably go to 2020. I don't know that it really matters. But at the same time, having 2021 makes it clear that we're going beyond 2020, right? We have this extra year's worth of data. Anyway, let's do what they have. And we'll go from there. Again, we'll come back up here and I will make a new data frame that I'll call year labels. And this will be a till. And so then our acts, it's going to be midway between zero and negative four. So that should be negative two. If my math's wrong, we can always change it. And then why I will make a sequence from 1880 to 2020 by 20s. And so that's going to get me every 20 year, I actually don't want the 2020, right? I want 2000. And I'm going to concatenate on 2021. Right. And so again, this vector will get us all of the years, right, 20 year increments. And then the C will concatenate on to that vector 2021. And so we'll get those years as well. So we'll go ahead and add that. And then down here, we'll add a geome text on top of the segments. So it goes after geome segment. So do geome text, data equals year labels. And then we'll do AES x equals x, y equals y. And then our label will be the same as the y the year, right? And then we'll again do inherit AS equals false. And a color is that horrible green. Again, I'm just trying to impersonate what they did. So let's see what this looks like. There we go. That looks a lot like what they had in the original version of the figure. You know, maybe the year was a little bit smaller, I think it's actually the same size as the numbers on the grid lines. So maybe we could come back up here, and we could change that geome text to be the same size, which I had as three. So it looks more appropriate for the size. I think they did make it bold. So I'll come back up to geome text, and I'll do font face equals bold. And so that's bolded. That green is really unattractive, in my opinion. Honestly, I don't know what other color I would use. Maybe maybe I would use yellow, you know, let's go ahead and turn that to gold and see what that looks like. Sorry, I keep picking at this, but I can't let that green go. So let's do gold. I think that looks pretty good. Maybe it gets a little bit washed out, kind of down in here. But you know, the green was really horrible. Also green and red are a horrible color combination. So I'm going to roll with that gold color. And I think that looks pretty good. Alternatively, we could always put the year off to the left. I like having it in the middle, though, because then it's clear, more clear, your eyes don't have to scan back and forth. Let me know what you think. What would you do with these years? Would you put them off to the left? Would you leave them here? Do you like this color? Do you like the green? Let me know what you think. I'm curious to kind of get your sensibility of how to make this look a little bit more attractive. I'm pretty happy with the way this looks. I think it was pretty cool to be able to use geome segment to generate this a little bit of funny algebra that we had to do so that we could bring the October date off to the side like we did. Again, let me know what you think about the placement of these years and their styling. Tell your friends about what we're doing. Ask them for their feedback as well. And we'll see you next time for another episode of Code Club.