 Hey folks, I'm in the midst of a series of videos looking at different approaches people have taken to visualizing global climate change data. In today's episode, we're going to look at something called the Global Temperature Index, which is a measure of the change in temperature relative to a baseline that was calculated between the years 1951 and 1980. One of the visuals that I have found to depict this comes from NASA. If you go to climate.nasa.gov forward slash vital signals global temperatures, I'll put the link down below, you'll find this figure. I thought that this is simple, yet perhaps kind of complicated to generate. And so what I want to do in today's episode is use the data that we downloaded in the last episode to go ahead and regenerate this figure and, you know, perhaps dust off some of those parts of ggplot2 and the theme function that we haven't used in a while. Here in our studio, I have a brand new R script called temperatureindexplot.r. All it has so far is library tidyverse in it. That's basically what we put in every script, isn't it? If you want to get this repository, there's also data that we're going to be using that's in the repository. Go down below in the description. There's a link to a blog post. You know what to do. There's a video across the top that'll help you get caught up as well. And again, if we go ahead and load the tidyverse, we can then go into our files tab. And in the data directory of our project, there's this glb.ts, blah, blah, blah, CSV file. Let's go ahead and view that. And so this opens up a tab in our studio showing us what the file looks like. One of the things I noticed right off the bat is that there is a header line in here that we will want to skip because our column names begin in the second row. One of the other things that I notice is that we have months like January through December, but then there's also aggregated months. So the average between, say, January to December, so that would be like a full year, December to November, December, January, February, March, April, May, June, July, August, September, October, November, you get the point. The final thing that I notice is that there are three stars to indicate positions where there's no data. And so for example, this might be, oh, I don't know, one of those three month combinations. So like December, January, February, right, where we don't have 1879 data. So we couldn't calculate the the average over those three months. All right. So there's a couple of things we need to think about when we read in the data. So again, we'll do read CSV, and we'll get the data out of data forward slash GLB, all that will then use the skip argument to skip one row. And then we'll do na equals and then the character, right. So we can tell read CSV what characters to treat as an A values. This then reads in our data frame as a table. We've got 143 years worth of data, 143 rows and 19 columns. So there's extra columns in here that we don't necessarily want. I want the average over the full year. So to keep things simple, I'm going to go ahead and use this J through D column. So we'll start with a select and we'll do year. And we'll do J hyphen D and this of course gives us our year and our temperature difference. I don't like having that J hyphen D because it just becomes a pain to have to worry about those back ticks. So I'm going to call this T underscore diff equals J through D and I'm also going to do lowercase year equals year. And so now we've got the data that we need to generate our plot winning, right? We're in good shape. So we'll go ahead and do a GG plot. B, S, X on the X axis, we'll put the year on the Y, we'll put the T diff and then we'll add geom line and we get that line plot. This looks a lot like what we had from the website as you can see, although we still have a ways to go. One of the things that I notice is a subtle difference between my line plot and this line plot is that this line plot also has the dots on it. So let's go ahead and add geom point. And the points that we want are from that special series of values from 21 to 25. I want a white fill and a gray background. So we'll do fill equals white and then color equals gray. And my line, I also want to be colored gray. And so now I'm getting a fill that's white and gray because I forgot to change the plotting symbol, right? And so I can then do shape equals 21. Very good. We're going to have our open circles on top of our line plot and that looks pretty nice. Of course, we could go ahead and let's try the theme light to get the white background with the grid lines. That gives us our grid lines with a white background looking pretty light, if you will. So good. The next thing that I want to add to this is the geom smooth. You'll notice from this version of the figure that there's a black line that goes through the line to fit it all. And so what we could do is a geom smooth and let's start there. And so this gives us a smooth line through the data. One thing I notice is that we've got the cloud around the line for the smooth line. Another thing I notice is that the line is blue. It's also a little bit thicker than the background line of the actual data. And the other thing I notice is that it's not as wiggly, if you will, as we found with the original version of the figure. We have a couple of things to change here in geom smooth. So let's start with SE equals false. That will get rid of that cloud. We'll do color equals black. And let's do then size equals one. Let's try that. And then up here in geom one, we'll also make that size equals one. You know what? I'm going to make it 0.5 because I think one is actually pretty thick. I can also use an argument called span, which determines the wiggleness or the wigglyness of that fitted line. And so the default is 0.75. So let's go to 0.25 and see what it looks like and we'll adjust from there. Good. So we now have a black line that's the same as the gray line behind it. It doesn't have that cloud for the standard error. It's a little bit more wiggly, but you know what? I think it could still be a little bit more wiggly. So let's go ahead and drop this down and I'll try 0.15. And so that looks a suitable level of wigglyness, right? Good. I think we're gaining on it, right? And so we can see already the similarity between what we have and what they had in the original figure. At this point, I'm going to go ahead and output the figure to a file so I can get the dimensions to more or less match what they had in the web page. And so I'll use gg save and I'll put this into my figures directory and I'll then call this temperature indexplot.png and let's go for a width of 6 and a height of 4. So the next thing that I want to do is go ahead and modify my y and x axes. On the y axis, the original version goes from negative 0.5 up to 1.5 and on the x it goes from 1880 to 220 plus a little bit, right? So we can set that by using scale x continuous and then I can put in my breaks going as a sequence going from 1880 to 2023 and we'll go by 20 year increments. Then we'll do scale y continuous and we'll do limits going from negative 0.5 up to 1.5. And so we've got our limits set right. One thing I noticed though is that there's a bit of a margin say between the limit on the y and x axis and the other axis, right? So there's a bit of space here between the negative 0.5 and the x axis whereas on the web page version there's no space there. We can get rid of that space by adding an argument called expand. So we can do expand equals c0 comma 0 and we can do the same thing that we did for the x for the y. So we can now see the values on the axes go right to the edge. One thing I see that we have that the web page doesn't have are the tick marks. So let's go ahead and remove those using the theme function. So we can do theme and we can then do access.tix equals element blank. That of course gets rid of all of those tick marks and we're in pretty good shape. We can now go ahead and let's modify our labels. We need an x and y axis label. We also need a title to match what we had on this original version. So I'll come back in here and we'll do labs and then on the x axis I believe it was all cap year and then the y, temperature, anomaly, and then I had c in parentheses. And then the title, I'm going to go ahead and copy this from the web page. I'll grab that also go ahead and grab the subtitle and we'll plop that in there and we'll put that in quotes, put a comma there. We then want this to be subtitle and we wrap that in quotes and we go all the way out to the right to put that in quotes and this credit was on a second line. So I'm going to go ahead and put in a line break. So we're gaining on it. A couple of things that I notice about this that are a little different from what's on the web page. Again, we don't need to be, you know, just strict adherence to what they have on the website, but I find that by forcing myself to copy what I see on other versions of a figure I'm trying to generate, it forces me to go into parts of R that I'm maybe a little bit uncomfortable with and helps me to learn better. So anyway, a couple of things I want to fix. The title is black on the web page. It's red. There's a bit of a spacing that needs to be inserted between the title and the subtitle. Also, my title here is justified on the y axis, whereas on the web page, it's more to the left, justified more on the overall plotting of the plot, right? So let's go ahead and see if we can fix that in the theme function. And so back here again, we can do plot.title.position, and we can then say plot. That will get everything over to the left. I can then also do plot.title, and I can do element text. And I can then give it a margin. And then I can give that the margin function. And the margin I want to put is on the bottom. So I'll say b equals let's do 15. I can also change the color here. So I can do color equals red. And I can say face equals bold to make it bolded. And then plot.subtitle, I can also do element text. And I'm going to make it maybe a little bit smaller. Let's do size equals eight and see what that looks like. I think I'm missing a comma over here. So that looks good. Maybe the margin here is a little bit too big. And I also would like a margin below my subtitle. So let's come back here and let's make that maybe 10. And then for my subtitle, I'll make a bottom margin. So I'll do margin equals margin, b equals, let's try there 10 as well. So I think that looks pretty good. At this point, the main difference I see between my version and their version is this legend, right? And so they have a legend indicating that the gray is the annual mean and the black is the lowest smoothing. One of the problems that we have with our code is that we haven't been mapping one of the variables to the color aesthetic. And so what I'm going to do is to force that, right? And so I can come back up here and where I have say color equals gray, I'm gonna instead say AES color equals, and let's make it one, okay? And so we'll put that in comma in parentheses. And we'll also do that down here for geom point. And so again, what this is doing is it's creating a variable, a dummy variable that I'm calling one that I'm mapping to color, right? And that way I can now come down to geom smooth and I can do the same thing, except instead of giving it one, I can give it the value two. And so now when we generate this figure, we're gonna have different colors for the data line with the points from the smooth line. And sure enough, we now have our salmon and teal lines here. And so hopefully you can see that we're gaining on something here, right? What we need to do is we need to change the color of our legend and we also need to move it into the upper right corner. So let's take this in stages. First things first, let's go ahead and change the color of our legend. And we can do that back here. I'm gonna put this with my other scales and do scale, color, manual. And I'll do name equals null. And then I will do breaks equals one and two. And then I will do values. And these are gonna be the colors I want. So I'll do gray for one, which was the background data. And then black for the foreground smooth fit. And then my labels. I'm gonna go ahead and copy from the webpage and paste that in. So annual mean and then lowest smoothing. And I think in R, they don't have that W, but I'm gonna leave it in there because what do I know? Anyway, so let's go ahead and run that. And so now we have our gray and our black. I again wanna move this up into the upper left corner of my plot. The other thing I'm noticing is that I'm getting this circle from geom point. So I wanna get rid of that. So I can get rid of that circle from geom point by coming up to geom point here. Show dot legend equals false. Good, that circle goes away. Let's move it now. We can come up into theme and do legend dot position equals. And we give it a vector. So the two positions in the vector are the relative position on the x-axis and the y-axis. So let's do like 0.2 and then on the y, let's do say 0.9. And so that gives us more real estate now for our plot to the right because we moved the legend out of the way and it put our legend kind of in the upper left but we wanna move things around a bit. So let's start out by moving it to the left. And so let's try to say 0.15. I think that looks good. One of the things you'll notice though is that there is, the legend seems to be a bit bigger than these two lines. And that's because there's a title in here that we set to null. But for whatever reason, it's still is leaving space for the title. So a trick that I have found to work in this situation is to make the font size of the title of my legend to be zero. And again, we can come down here and do legend dot title element text. And then we can do size equals zero. The other thing I can do to kind of squish things together a bit is legend dot key size, key height, let's say. And that's gonna take a unit. So we'll say unit and let's say eight and that's gonna be in units of points. And that puts it together a bit. One other thing I see around this is that there's a margin around it all. So let's go ahead and remove the margin. We'll do legend dot margin. And again, we'll give that the margin function with all zeros. And so that you can kind of see the grid lines are a bit tighter around those labels. I feel like now those lines are a bit too close. So let's go ahead and make the legend key height just a little bit bigger. Let's make that 10. And I think that gives it a bit more breathing room. One final thing that still bugs me a little bit about this is that we have a line here in the legend rather than the square. Again, if we look at the legend in the plot, they have a square rather than a line. So how would we go about fixing that? So to fix the legend, let's remember that we actually have three geomes for plotting. We've got the geom line, the geom point and the geom smooth, right? So I think the easiest way to get that square is to turn off the legend actually for the line and the smooth line and to turn back on the geom point and then we can override what shape is shown in the legend, okay? So it's okay, we can do this. So again, back here we had show legend false for geom point. So let's change that to true. I'll then go ahead and put show.legend equals false here and I'll copy this down as well for geom smooth. So now we've gotten rid of the line in the legend and we now have that circle plotting symbol but we don't want the circle, we want a square. So we'll come back in here and where we had our scale color manual so we can come back in here and do guide equals guide legend. And we'll then do override AES equals list. And so what we're gonna do here is we're gonna say take the guide legend, take the legend for color and we're gonna override one of the factors or one of the aesthetics that's in there. And so what we're gonna change is shape. And so if we do change shape equals 15 that should give us a square plotting symbol. Sure enough, we get a square plotting symbol. It's kind of small. So let's see if we can make it bigger. And again, we can come right back up to where we gave it this list and then I'll do size equals five. And here we go. We now have our large squares in the legend. And I think that does a pretty good job of approximating what was in the web version. Yeah. I mean, there's had kind of a weird border around it. I'm not gonna go through all the trouble of trying to figure out how to make an outer ring and middle ring and an inner core color. I don't think that's necessary. I don't think they intentionally did that either. I kind of like this version of our figure. And I think it works really well. I think it does a pretty nice job of displaying the data. Again, this is showing the average annual temperature that is normalized between 1951 and 1980 global temperatures. And I believe includes the land surfaces and ocean surfaces as well. So you could see right between 1951 and 1980 is right in round in here. And that is right about the zero line. And so then the temperature index is basically how far are we from that normalized, that average temperature? And as you can see, we're quite a bit above that. And I'm here in Michigan, it's in May, and it's been 85 degrees this week, which I think is, I realize that's weather and not climate, but I think that indicates something is off. Well, in the next episode, we'll look at another way of visualizing global climate change so that you don't miss that episode. Please be sure that you've subscribed to the channel. You've clicked the bell icon and you've given me a thumbs up, as always. Tell your friends about what we're doing here. Try your hands with this code and see if you can take some of these principles back to whatever it is you're working on in your day-to-day work. We'll see you next time for another episode of Code Club.