 Hey folks, I'm really excited for today's episode because we're going to make a new type of figure using a geome I have never used before, geomraster. We'll also combine that with facet grid so that we can make what's called a small multiples plot of global climate change across the earth between 1950 and 2021. My inspiration is coming from this figure that you'll see from the climate lab book made by our friend Ed Hawkins. He goes from 1850s up to 2019. Each row is a different decade and each column then is a different year in that decade. There's a lot going on in this visual, so thankfully they've provided us with a blown up version. One of the things that I noticed and that people that commented on this figure noticed down below in their comments is that there's a lot of patchiness to the data and you'll notice that you'll kind of see temperatures perhaps where there were shipping lanes in the 1850s going forward. What we noticed in the last episode when we plotted the degrees latitude and longitude that we had data for each year from 1880 up to the current is that we saw a big shift in the number of observations per year right around the 1950s. So what I want to do today is look at the global distribution of temperature anomalies from 1950 coming forward. Now this image is an example of a rastered image in which each pixel is indicated by a different color and that color comes to us from the temperature anomaly data. I don't think they've got it in here, at least I can't see it when I zoom in myself, but you could imagine having a globe underneath the colored images so that you could actually see where these different temperature anomalies are. It's really hard to see looking at this map you know what part of the earth am I actually looking at. NASA actually made an animation which I'm showing now showing over the decades temperature anomalies on top of the earth and so I think that works well because it's large enough that you can see the outlines of the continents and countries with the temperature anomalies on top. These images these little thumbnails if you will are so small that it's really hard to see that level of detail. What I'm going to do with my version of this figure is instead of plotting all temperature anomalies over water and land is I'm only going to look at the land-based temperature anomalies. I think what that will allow me to do then is to basically see the outlines of the continents. You know I would leave it to you to go back and get the other data set that also includes the temperature anomalies over the oceans. So heading over to our studio here if you want to get a copy of the code and the data and everything that I've been working on over this series of episodes down below in the description there's a link to a blog post that has all the coordinates on github that you need to get yourself set up. Great so I'm going to build off of the code that we used last time to build those ridgeline plots. You'll recall that we got the data from a net cdf formatted data set from the nasa website. Again go back and look at that episode if you want to learn all the intricacies of how we got that new binary formatted data into our studio and got it to be tidy. I'm going to go ahead and copy from that ridgeline.r script maybe the first I'll say I think the first 43 lines here. So I'm going to go ahead and copy all that and I'm going to paste it into an empty r script that I'm calling global temp anomaly.r I'll save that and I'm going to look through the different libraries that I've got loaded here. I don't need gg ridges because I'm not going to make a ridgeline plot so that should be good. I'm going to go ahead and highlight everything and run it so I have everything loaded here in my r session. Very good that is all loaded. That leaves us with this data frame t data. So looking at the data frame we see that we've got year longitude latitude t diff. We also have a t av column. So what I'm envisioning here then is a different panel for each year from 1950 forward. We've got the longitude and the latitude. We're going to plot as color the t diff variable. The t av was the global average temperature for each year. I don't need that so I'm going to go ahead and remove that. Also I noticed that my output here is grouped by year so we don't need that either so I'll go ahead and remove that and again now if I look at t data I see the columns that I'm interested in. So let's go ahead and start to make our plot. So we'll take t data and I'm going to focus in on a single year before I build out all 72 years. So let's do filter year equals equals 2000. This then gets us the latitude longitude t difference for the year 2000. So we can then feed this into ggplot as our x will be the longitude our y will be the latitude and then our fill. I guess I spelled that right. Our fill will be t diff and again that's going to be the color of the raster and so we'll then feed this into a new geome for me at least which is geom raster and again geom raster takes fill as the argument for the color rather than color. Very good. We can see those land masses indicated by those colors. Again if we wanted all of the ocean and the land masses there's another data set up on the gss website that we looked at briefly in the last episode. You could get that but then it makes it harder to see the land masses and again at the size that each of these panels will be in the final image I don't think we would see a plot of the map of the earth behind this or on top of it right. I just don't think that's going to work very well. It's just not going to be you know high enough resolution. So I'm happy to run with the land mass image. So there's a few changes that I want to make to this individual panel before I explode things and do 72 or so different panels. So the first thing I want to do is to alter the the fill coloring the gradient. The other thing that I want to do is work on our axes and then third thing I want to do is set the color for the background. So let's start with the gradient for the fill. So we'll do scale fill gradient two. We've seen this a lot already and as always we'll do low equals dark red. Mid is going to be white. High will be dark blue dark red actually. So I don't know the defaults for low is dark red and the max or high is dark blue but I want the opposite right because blue is a cold color and red is a hot color right. So we'll go ahead and do that and then we'll do midpoint equals zero. We can see our red being for hot and blue being for cooler relative to an average temperature anomaly at each two degree by two degrees spot between the years 1951 and 1980. So very good. The next thing that I want to move on to is setting our axes right and so we're looking at latitude longitude where one degree of longitude is the same as one degree of latitude. So I want to make sure that those proportions are always one to one. I can achieve that using chord fixed and so now this gives me a dimension that is much more in fitting with the shape of the earth. One other thing I'm noticing is that we have a bit of a margin as we always see with plots from ggplot2 there's always a bit of a space or expansion around the figure. You'll notice that this basically bar for Antarctica is lifted up off the bottom. We can turn that off with the expansion so we can do expand equals false and so now we see that we've got much tighter edges on our map and so that looks pretty good. I also want to go ahead and remove our labels on the x and y. I think people will know that these are longitude and latitude. I don't need extra text in here to kind of cloud up the story. So I'll do labs with x equals null y equals null. Maybe later we'll go ahead and put in a main title but I'm happy with this for now. I'm also going to go ahead and turn off the axis labels and axis text and ticks so we'll do that with theme and then we'll do axis dot text equals element blank and then axis dot ticks equals element blank and I forgot a comma here so I'll get that and so now we have a pretty trimmed down version of the figure. I'm going to leave the legend in here for now. We'll go back and modify that later when we've got the full figure. The final thing I want to do before we blow this up and make our small multiples version of the figure is to turn the background to be black. I think if we make it black that will really help the whites to pop and the distinction between the colors be more clear and so I'm going to go ahead and do panel dot background equals element rect fill equals black and then the same thing with the plot dot background element rect fill equals black and then we also want to remove the grid lines right so we'll do panel dot grid equals element blank. Very good we're well on our way so now what I want to do is to expand this to all of the years between 1950 and 2021 which is the last year that we have the full data for so I'm going to go ahead and comment out my filter where I was just looking at the year 2000 and I'm going to come down so I'll add facet wrap and we're going to do tilde year and so what that means is make a different small plot or a different panel for each year so for each year we'll get a different plot just like what we had for the year 2000. I'm going to then say n call equals 10 because I want the 10 years across and then I'll do n row equals 8 because again we have I think 72 years and so we'll have 10 columns 8 rows and there'll be a few slots in there that should be open I'll go ahead and add this into the overall ggplot pipeline very good we have our 72 different plots here unfortunately you can't see anything because the screen is very small but also the heading the label for each of the facets really dominates the individual panel so I'd like to remove that and so I'm thinking ahead about how I want this figure to look and what I'd like to have is in the left hand margin I want the decade label kind of like we saw in the version from the University of Reading I'm not going to put a year across the columns because I think that'll be obvious I hope and so one of the things that we could do is instead of using facet wrap we could use facet grid and so facet grid works well when you basically have a matrix of plots where you've got one variable on the x axis and one variable on the y axis and so what I'm going to put on my y axis is my decade and on the x axis will be the year I can then remove those labels and do something special with them that I'll show you so I need to make those two columns so we'll come back up here to t data and I'll go ahead then and do a mutate and so we'll do a decade and so for the decade we'll have something like 1962 and I want that to be 1960 right and so what I can do is I can divide the year by 10 right so that 1962 would be 196.2 and then I can do a floor on that so floor on that and maybe I'll come down here and show this in the in the console what I'm going for right so if I do floor 1962 divided by 10 I should get 196 and sure enough I do and if I then multiply that by 10 I get the decade right and so that is the strategy I'm going to use here so we'll go ahead and do 10 times the floor of the year divided by the 10 and so again if I do t data I get the decade along with the year and if I take t data and pipe that to tail I should get 2020 for 2021 I'm sure enough I get the right decade now what I want will be the year okay and so what I'll do here for the year is something pretty cool and so I'll take the year and I'll do percent percent 10 and so that percent operator is the modulus operator if you remember back to elementary school when you were learning long division you'd have remainders well the modulus is the remainder right so if I have 2021 modulus modulus 10 the result should be one right and that's what we get and in fact if I take um let's do 2020 to 2030 right that vector of numbers percent percent 10 I then see the year within the decade so looking at the output I've got a column for the year the longitude latitude t diff and the decade I've noticed that it turned the year like 1953 into uh it would be like three in that case right or the um in this case 1950 became zero and so what I'd like to do is perhaps call this something else so I can keep the year column just in case I need it down the road so here I'll do single instead of year t data I now see I've got the original four digit year the decade and single and we're in good shape now when I come down to my ggplot pipeline I can do facet grid and I can again use this tilde notation to put decade on the y axis and then single on the x axis I can go ahead and remove the n call and the n row and so now what you see is something a bit different in how these are being labeled right and so on the right side we have the decade but it's so small that it's getting truncated and across the columns we have that single year right so like this would be 1950 1951 52 and so forth right now what I want to do is go ahead and remove these labels across the x axis because I don't I don't need to say that you know what individual year that is and on the rows I've got those decades that I'd like to be on the left side rather than the right side I'd also like to turn them 90 degrees so they're horizontal to the x axis so let's go ahead and do that so the first thing I want to do is go ahead and move the label from the right side to the left side and so we can do that up here in facet grid where I will do switch equals y again that'll take y to the left side and so now I've got those years on the left side and now I can use the theme function to go ahead and get rid of those labels across the top and to do that we'll do strip text dot x equals element blank that gets rid of those labels across the top and then I can also do strip dot text dot y equals element text and then we'll do angle equals zero so nothing happened and I think what happened is that it is still expecting that this strip is on the right side and so there's an argument strip text y right and left and so I think what we'd rather have here is dot left I'm not sure why things don't always cascade down like they're supposed to through these different levels but I think what we want is left and so there we now see that we've got the decade on the left and it is parallel to the x axis awesome now what I want to do is go ahead and remove the background from the strip we've got that gray color and I also need to make the text white so in here for the strip text y left we'll do color equals white and then strip dot background I will do element blank we now see that we've got our decades properly labeled and it looks pretty good of course what we have down here in the lower right corner of our studio is way too small to see anything what I'd like to do is output this using gg save to a png that's going to be quite large so we'll do gg save figures global anomaly dot png width equals 10 height equals 4.5 and then unit units equals inches very good I like this aspect ratio I think it looks pretty good one thing that I keep noticing and forgetting is this white border around the plot and so we'll modify plot background to also have color equals black to get rid of that white border good we're making progress aren't we so one thing I notice is that when we went to this faceted version all of the colors got much more muted right and so if I look at my legend I see we're going from probably like 13 down to like minus 10 right and what we've seen before when we made those ridgeline plots is that most of the action happened between plus and minus four right so I think I'll do is modify my t diff column so that if the value is greater than four I'm going to make it four and if it's less than negative four I'm going to make it negative four that way then those extreme temperature anomalies will be that dark red color and that will then allow us to see have a better kind of positioning on our dynamic range of the colors to do that let's come back up to our t data and what we can do here then it would be a mutate on t diff so I'll use a case when so a case when is like a more complicated if else statement so t diff less than negative four tilde negative four so if it's less than negative four I want it to be negative four if t diff is greater than four I want that to be four and then otherwise if true I want that to be t underscore diff and make sure I've got all my I think I've got one too many parentheses here so we'll then pipe that into ggplot there we go now we have a much bolder set of colors and it's easier to see that you know in the 1950s 60s 70s that the colors are more muted more close to that zero range again the data were normalized between 1951 and 1980 but then as we move forward we can definitely see warmer temperatures in the plot so that's pretty cool the next thing I want to do is do something about our legend it's off here on the right and it's taking up valuable real estate I feel I would rather have it be at the bottom where we've got some extra space so to do that we can of course come back down to our theme function and we can do legend legend dot position equals bottom and so now we have our legend at the bottom and I was thinking that down in here this last row was at the bottom but really that's part of the plot we just have blank plots in here right so let's move that up into the window and to do that we can instead of using a position like bottom we can give x and y positions relative to the overall size of the plot so i'll do 0.75 and zero and so that gets me a vertical legend there so what I need then is to add a legend dot direction and we'll then say horizontal now what I want to do is go ahead and flip the colors if you will right so the white to black and black to white again we can come up into our theme so we can do legend dot text equals element text color equals white and then we can also do legend dot background equals element wracked fill equals black and I see that I've lost my legend title so we need another argument for that which will be legend title making that white and I think I'd rather have the title be larger than the text so the text being those numbers across the horizontal so for the title I'll do size equals six and so for my text like that negative four negative two zero so forth I'll make that size equals five just a small difference so I'm happy with the way this is turning out one thing I'd like to do though is give more to the left of negative four and more to the right of positive four again we do have values beyond those extremes that we're showing so again I can come back up into my scale fill gradient and I can do limits and let's do c negative five to five and then I also want to keep my breaks of negative four negative two zero two and four so to set the title of the legend I'm going to use the name argument and we'll do anomaly and then in parentheses degree c with the unicode u zero zero b zero c close parentheses quote right so I like that title I just don't like the position I'd rather have the title of the legend at least when I'm doing a horizontal like this be on top of the legend so we can do that with the guides function we'll do guides fill equals guide color bar right and then into here we're going to give a couple different arguments title dot position equals top and then we'll also do title dot h just equals 0.5 so the title position we'll put it on top of the guide color bar and the title h just will center that right so zero is left justification one is right 0.5 is in the middle that's exactly what I want so I'm happy with the way that looks now what we'll do is go ahead and add a title to our overall figure so to change the title I'll come back up here and then do title equals global annual temperature anomalies between 1950 and 2021 and of course I can't see my title because the default color for a title is black and I've got a black background so let's come back up here and I will then do plot dot title equals element element text color equals white and then we'll do face equals bold I am noticing that now that I added that title it's bumping my legend down a smidge so I want to raise that legend position I had it at zero so let's do like 0.05 I'm really happy with the way this turned out looking at this figure I think the black does a really nice job of highlighting the change from blue to white to red and those differences one thing I'm noticing is for the early 1950s is that we don't have any data for Antarctica right so as I mentioned and we saw in the previous episode the number of observations we had across the globe really increased in the 1950s I suspect a lot of that happened here in Antarctica as we kind of you know we do have pretty full maps for everything but Antarctica but that certainly adds something anyway I think we can totally see the trend and as you kind of kind of scan down you can kind of see the the little panels getting redder and redder something I wondered about would be would we maybe have a better visual if we had the decades across the row across the columns and the individual years across the rows right so we'd have basically eight columns and ten rows I will leave that to you as an exercise if you come up with that go ahead and tweet that at me I would love to see if you get that to work and if you think that works better than what we have here if I was doing this for a publication or something like that or for presentation I would probably go ahead and try it with that different orientation so there's a few things you're going to have to change along the way from the theming that we set up but I think you can do it keep practicing with these techniques and the various theming tools that we're using in different ways that we're trying to convey stories about certainly something that's very important like climate change anyway keep practicing and we'll see you next time for another episode of Code Club