 Hey folks welcome back for another episode of Code Club. As you know I have been doing an extensive dive into looking at temperature anomaly data collected by GISS at NASA. What we've been looking at is temperature anomaly data. Basically looking at the deviation in temperature relative to the average temperature between about 1951 and 1980. We've looked at this on an annual basis, on a monthly basis, on a latitude and longitude basis. We've been looking across the earth, across all regions within one year. Well today we're going to look at that one more way. And so what we're going to do today is look at latitudinal variations. So if we take each band of latitude and perhaps we'll group it together by similar latitudes and then look at the variation of the temperature anomaly over that period of time to see how it varies at different latitudes. I found a really interesting figure at the science visualization studio at NASA where they created this animation. And again what you see is the earth on the left side and as it spins around it's segmented into different latitudinal zones. On the right then is what I would call a rug plot. It's not exactly a rug plot but close enough. And you can then see that the current year has a thicker bar and it's moving left and right with time to indicate the average temperature anomaly for that year in that latitudinal gradient. I think this is a pretty cool figure. I'm not going to try to take on the rotating globe that you see on the left side. Instead what I want to do is I want to make the rug plot itself. And it's not going to be an animation but what I foresee doing is making a rug plot where again we have the latitudinal gradients and we're going to generate this last version of the figure. Which again I think is pretty cool. And again it's called a rug plot because if you look at a rug from the side you'll see the fibers sticking up on end. And so if you look at this from the side it's kind of like a rug right? And so where you have more dense fibers if you will for the rug that is where you have more data that was more similar to each other right? And so you can see that you know for the the arctic latitudes that it's like been really wide right? There's been a lot of variation whereas perhaps at this southern hemisphere I think it's called like subtropic or I don't know subarctic that it's been much more consistent. Anyway I think this is pretty cool and this is what I want to generate with you here in our studio today. Reading down through this article we see that they have a link here for the data and again if you hover over this you'll see that it's a CSV file. So I'm going to go ahead and save the link or copy the link address rather. I'm coming into our studio of course if you want to get information on my setup the directories the data the code that I have for this whole series of videos down below in the description is a link to a blog post that should get you going. We'll go ahead and do library tidyverse so we don't forget and then I'll also do url equals the url to the csv file that was referenced in that blog post. We can then do read csv so I can see from the file name that it's a csv url this then will output the typical output from read csv where we have the year the global mean northern hemisphere southern hemisphere and then different bands of gradients. What we'll do is come back to this blog post and I'm going to highlight these different zones that they used in this image and so I'll paste that into here maybe I'll call that bands and then I'll go ahead and make a vector of these values and so I need to put quotes around everything so that each of these can be used as a different column so that I can use the select function to pull these out of to pull these out of the data frame to get the columns that we're interested in using to build our visual so great I've almost got them all here so I need to look through the column headings of the data that I've read in and make sure that those match what I have up here in my bands right so the first is 90n 64 north and so I see here that I've actually got the opposite right so this is 64 north 90 north and if I look here I've got 44 north 64 north so those are all flipped um and again if I look here I see I've got eq u so the equator to 24 so these all need to be flipped unfortunately so I'll very quickly do that um I might edit the video so that you don't have to watch me do all this because you know copying and pasting with pat's typos is gotta be up there with just you know highlights of watching youtube all right so we've got our bands and now I'm going to feed this into a select I'll do year equals capital year so that like just keep everything lowercase and then I'll do all of bands and so now I don't have any error messages and I have those eight different latitudinal bands that we want excellent now I'm going to take those eight columns for the eight bands and use them in a pivot longer so that I have a column for the year a column for the band and a column for the size of the anomaly we can do that with pivot longer we'll do everything but year names two uh let's call that zone and then we'll do values two and I'll do t diff very good we now have uh our three different columns like I was hoping for I need to go ahead and change zone to be a factor because otherwise when I plot these say like on the y axis it is going to put them in um alphabetical order rather than in uh the order of the latitudes right so I'll go ahead and pipe this to a mutate on zone and that's going to make a factor of zone and I'll set the levels uh to be the the bands that I have here right and so that's the order that I want them in we now have our factor set up and I will go ahead and save this as zone data and uh we can now plot zone data so we'll go ahead and do zone data pipe that in a gg plot um and then aes uh for our x position I want to put the t diff we're effectively making a one-dimensional plot right so we're plotting t diff for each of the four different bands we'll then do y equals um zone and so let's start with geome point that's not what we're ultimately going to use but let's try geome point so now we see on the right is effectively a scatter plot where we've got the temperature difference on the x axis and our latitudinal gradients on the y axis it's actually the opposite of what I would like to use so what I'm going to do is go ahead and flip this and so what I could do is I could take the bands vector and I could put that in a rev function to reverse the order of uh the data and so now we have 64 north to 90 north at the top and the equators in the middle and the southern latitudes in the bottom and this is the orientation I want and we can already see this kind of looks like what we saw over on the nasa nasa visualization website now what I want to do is take care of this geome point so instead of geome point what I actually want to use is geome segment and so geome segment allows you to draw lines um or segments of lines on your plot and it takes four different arguments x x and y and y and so x and is going to be the same as tdiff because I want to have again those fibers for a rug which will be vertical fibers right and so the x positions for the start and end of those segments will be the same for the y we want zone and y end will be a zone um and so if we do this we don't get any output and again that's because we're basically giving geome segment points rather than segments right and so what we need to do is perhaps do something like zone minus 0.25 and zone plus 0.25 and so what we find then is that because zone is a factor we can't add and subtract from that right and so what I'm going to do is come up here to my mutate under zone data and I'll do zone uh position and that's going to be an az dot numeric on zone and so again if I look at zone data I now see I have a position for that and so for my zone um instead of zone for y I can do zone position right and I can do that down here as well for the end and let me go ahead and put the y arguments on the second line for the x and so here we can see those vertical fibers and we basically have a rug plot for each of those different uh latitudinal positions I'm going to go ahead and make the color of those gray and so we'll do that here in geome segment I'll do color equals gray and I'll also do maybe alpha equals 0.25 and so we can now see um our fibers and again because we're using that lower um transparency of alpha of like 0.25 if you have four um lines on top of each other then it'll be a solid gray maybe I'll go ahead and make this black uh to make that a little bit more clear yeah we might go back to gray we'll see but that kind of gives you the idea again that where it's darkest we have the most observations for that latitudinal gradient let's go ahead and clean up our axes now so we'll do scale y continuous we'll do breaks equals one to eight labels equals bands so again now we have our labels on our y axis looking good um across the x axis we see that we have values going from negative two out to three but our values maybe should really go out to uh four so maybe we'd go negative three to four let's give that a shot so again we'll do scale x continuous breaks um equals uh seek negative three to four by ones and then labels will be the same things we'll do seek minus three to four by ones so we'll then add the limits uh from negative three to four so now we have our axes for the y and the x I'm going to go ahead and get rid of the y axis label so we'll do labs on x I'll say temperature anomaly and then we'll do degree c so u zero zero b zero c and then y will be uh null very good before I do too much more messing with the plot I'm going to go ahead and output this with gg save as a png so we'll do gg save and we'll do figures and I'll do latitude latitude anomaly dot png my width I'll make four my height I'll make three units of inches the next thing I want to do is keep with the theme of all the figures we've been making which is basically to make everything um black in the background and so all that worrying I did about these lines being black or gray I need to flip that so I'll make those lines white so what we'll do is before I forget about that I'll go ahead and make the color of the segment white so I'll go ahead and modify the stuff with the theme function we'll do plot dot background element wrecked fill equals black and then panel dot background element wrecked fill equals black and then we need to change the text to be white right and so then we'll do axis dot text element text color equals white and then axis dot title equals element text color equals white now I want to turn our attention to the grid lines and so the original that I'm trying to model after a little bit the major grid lines were present along the x-axis and those were white maybe they're gray I don't know it didn't have any grid lines that were horizontal so on the y-axis I'm kind of thinking though that maybe I would like to use the minor grid lines to give division between the different bands so let's give that a shot so what we'll do is panel dot grid dot major um x and we'll do element line color equals white and then we'll do panel dot grid dot minor dot x equals element blank and then we'll do panel dot grid dot major dot y element blank and again to get those grids between the majors like the one two three four five um so basically on the half I can use panel dot grid dot minor dot y equals element line uh color equals white and put that white in quotes and get rid of that extra space so we've flipped the colors and things are looking pretty good um the more I look at this the more I think there's just way too much going on with all of these lines I think I would like the major grid lines to perhaps be a little bit less prominent also I thought the idea was good to have those minor grid lines along the y-axis but I think it's just it just makes it too busy right and so maybe what I'll do instead is just rely on the density of the rugs for each of the bands to make it clear that that relates back to uh the latitudinal name the other thing I'm noticing is that if you look really close you'll see tick marks in there so let's go ahead and get rid of the tick marks as well so again I'll come back up here for this panel grid minor y and I'm going to make this a blank and then I'll also get rid of those ticks so we'll do axis dot ticks uh element blank and with these major grid lines maybe I'll make it a gray so it's not so prominent and I'll do a size of 0.25 that'll make it thinner the default I think is 0.5 so that's looking a lot better I'm really happy with that um one subtle thing that I keep forgetting is that there is a white border around the plot I don't know why that comes through all the time but let's go ahead and remove that and so we'll do color equals black uh to get rid of that line and so again that gets rid of that so now what I want to do is add some color to the data if you remember many many episodes ago I talked about a book by Cole Nussbaum or naflic who has a book called storytelling with data one of the things she suggests doing is taking a plot and making it black and white or making it monochromatic right then you add back in color to highlight what's really important so I feel like that's what we're going to do with this visual I'm going to now add on a line a thicker line maybe make it look like a bit of a pill um for each of the latitudes to show the anomaly for the year 2021 so to do that we'll come back up to our code and I'm going to take zone data and I'm going to filter that uh for the current year filter year equals equals 2021 again we get the latitudes for this year I'm going to call this current year great and now I can add another geome segment so I'll do geome segment data equals current year aes and then our x and y um x and y and positions are going to be the same but our color will be set by the t depth right so we'll go ahead and add that so now we can see the blue lines in there um of course I want to go back to my color scheme um and we can also get rid of that legend so to do that we'll come back up here and with our scales we'll do scale color uh gradient two so we'll flip the defaults by doing low equals dark blue mid equals white high equals dark red and then our midpoint will be zero and I want to get rid of the guides so we'll do guide equals none and so now we can kind of see the bands in there so what I'd like to do is make them bigger so we can do that with size argument to geome segment and so I'll do let's do size equals let's try three okay maybe that's a little too big let's do size equals two um right there so what I'd like to do is maybe give it a little bit more polish and make those ends rounded to do that we can come back up here to geome segment and we've seen this before um before when we made that kind of tornado looking figure of the temperature anomalies if you recall that from a few episodes ago we can do line end equals round I really like the appearance of this rounded end to those colored segments and I think that looks really nice now what I want to do is go ahead and put a title on this figure so it's clear to our audience what they are looking at so we'll come back up to labs and for my title I'll say variation in uh annual temperature anomaly by latitude 1880 to 2021 and again um with the flipping of the colors the title is there but it's black so we'll come back up here and I'll add to this we'll do plot dot title element text color equals white and I'll make that bolded so we'll do face equals bold so that's running off the right side of the screen also as we've seen before the title starts on the y axis I'd rather it start on the plot and so again we can come back up to where we had plot title and we'll do plot not title dot position equals plot good but it's still running off the screen after the word buy and so after the word buy I can put in a line break there wonderful I think I'll go ahead and put in a subtitle to indicate that the bars that are colored are for the year 2021 and so coming back up to labs I will add a subtitle and say bars for 2021 are colored by the size of the anomaly and then we need to come back down to plot title and we need to do plot subtitle to make that white um and then the face will be bold actually I'm going to make this gray so it's a little bit more subdued it's actually maybe a little bit bigger than I want it to be so let's come back up to the plot subtitle option and I'm going to do size equals six and maybe that's too small let's go up to eight and I think that'll be pretty good great I think that looks really nice so again this is the rug plot that I had in mind we don't have the spinning globe off to the side here I'll leave that for you to figure out how to do if you figured out let me know I'd love to see that in action anyway again I like the appearance of the rug plot at the different latitudinal gradients again where we can see it is wider that's where we have more data because those fibers if you will are close to each other or they're on top of each other and because we use that alpha of 0.25 when you have a white spot that means there's at least four lines or four years worth of data at the same position also I like this effect of coloring the current years worth of data I think you could also see how you could use the gg animate tools that we've seen in previous episodes to animate this to give kind of the horse race appearance that you saw in the visualization that we saw on the nasa website I'll leave that for you and I think you can definitely figure out how to do that without too much effort on the whole I really like this one thing that I'm not such a fan of are these labels over here for the y-axis but I don't really know what to do differently other than having a spinning globe over here as my y-axis and that's just kind of showing off don't you think anyway I hope you enjoy this I would be really interested to hear what you think would be effective for a rug plot type of visualization like this if you're from the field of ecology microbiology like me I'll often see rug plots used on the x and y-axis I think it's called geom rug actually to to generate that and that is useful for when you have like an ordination where you have a lot of points on top of each other and it's kind of hard to see where the density of points are and so on the x and y-axis then they draw these rugs so that you can see where the density of points is along the two axes anyway give that a shot let me know what you turn up with and we'll see you next time for another episode of Code Club