 Hey folks! I am in the midst of a series of episodes looking at different approaches of representing global climate change using temperatures. The data that I'm using I got from NASA's GISS department where they've been tracking global temperatures since 1890 all the way up to the current date. In the previous episodes we've looked at a line plot, we've looked at colored on a gradient bar plots. In today's episode I want to look at a kind of iconic representation called a warming stripe. These warming stripes you can find at showyourstripes.info and they're originally created by Ed Hawkins at the University of Reading. I've heard of people taking these weather stripes and knitting afghans out of them where you basically make a different stripe or different row a different color a different shade of blue or red depending on the annual global temperature. So what I want to do is I want to recreate this visual in R. So there's this labeled version and there's also the unlabeled version which again you could imagine this being an afghan or perhaps a necktie who knows. I will make the labeled version in R along the way we'll also see how we can make the unlabeled version. Perhaps you want to take that and you could you know use that as a plan to make your own afghan or necktie or who knows what but this is a bit of an iconic visualization of global climate change and I think certainly tells the story that as time goes along again each column here represents a different year and as time marches forward things are getting warmer. Their data was normalized I think between like 1870 or 1971 and 1990 or maybe 2000. Our data from NASA is normalized between 1951 and 1980 so it'll look a little bit different but it'll be the same idea. So I have a fresh R script with library tidyverse already there so that I don't forget to run it. I'll go ahead and load that. If you want to get the code and the data that I'm working with down below in the description there's a link to a blog post that'll get you everything you needed. So I have my project organized so that I have a directory for my code for my data and for my figures and so I have the csv that we need that I got from NASA in my data directory so I'll start with read csv and then in quotes I'll put data forward slash and one of the nice things about our studio is I can hit tab and it'll tell me the different files that are in my data directory so I don't have to remember this horrible file name who does this ah NASA come on anyway so we'll do that I also know from previous episodes that the first line of this file is a header row that's not important it's not column names it's descriptive so I can do skip equals one to skip that first line of the file also I know that it represents na values with three stars so I'll do na equals star star star this loads our data frame and we can now see we have a year column and then columns for each of the months I've got this kind of blown it up a little bit kind of zoomed in and so there's extra columns that don't fit on the screen and you'll see there's also December and then there's a j through d for January through December the calendar year as well as d through n December through November and then each quarter of the year I'm interested in the j through d so I will go ahead and pipe this into a select where we'll get the year and the j through d I'll rename this to be lowercase year and then I will call this t underscore diff people on previous episodes have commented that I should learn the janitor package because it'll do this for me easily and you're right I should learn it but you know this isn't so bad for the minimal amount of cleanup I have to do on this data frame so maybe if you'd like to learn more about the janitor package let me know down below in the comments and I'll see if I can't cook up a little episode to teach me and you how to use that package because I've seen it in a number of places now all right so we have this data frame 143 rows for each year and we also then have a column for the temperature difference great so I'm going to go ahead and call this t underscore data all right and then I can take t underscore data and I can pipe that to ggplot aes and when I think about those warming stripes I see a heat map right and I see on the x axis the year the y axis is like one value so maybe y would be pinned at one or some constant and then the color the fill of the tiles of a heat map is indicated by what I'm calling t underscore diff suppose you could probably make this as a bar chart but I think that would just get kind of messy because I think by default it's going to put spaces in between those bars so I'm going to use a heat map using geom tile to do this here so again on the x we'll do year on y like I said I'm going to set it equal to one as a constant so it'll have the same y value for all years for fill I'll do t underscore diff and then I'll do geom tile and there we go we have our warming strips it's got a ways to go but don't worry we'll get there soon enough what I notice about the default temperature gradient or fill gradient that it gives us is that it's a monotonic color change right from like dark blue to light blue but we have negative values and so that monotonic color change works well when you're going from like zero up to another number but we're going from like a negative number to a positive number I like to set white to be my color at zero right so in the last episode I talked about various scale fill gradient and scale fill steps functions if I look back at the original version of the warming stripes these are discrete colors mapped to temperature changes right this is not a continuous change right there's like a five or six year window in here where everything's the same color blue so they're binning the temperature differences into discrete temperatures uh discrete colors right and so instead of scale fill gradient which gives you the continuous change I'm going to use scale fill steps to get the stepwise discrete change right because my values go through zero I'll start with scale fill steps two so we'll do scale fill steps two and then I can do low equals blue uh mid equals white and then high equals red those aren't the actual colors but we'll come back and change that in a moment and so what I get are four different breaks are four different colors in here so it's picking breaks for me going from blue up to red let's go ahead and add more breaks and so I'll go ahead and do end breaks equals let's say 12 and so now I get 12 breaks from like minus 0.5 up to say like 1.1 or so whatever the top level there is what I'm noticing is that the lower level isn't the same intensity the same saturation as the upper level right and so this level of blueness doesn't match this level of redness again when I look at the warming stripes that is matched right so like this is the lowest temperature and that's got the same saturation I think that's the right term um as the maximum red color so I'd like to match that right so the trick that we saw in the last episode was to instead use scale fill steps n and then we'll have color and I will give that a vector of color so I'll do blue uh white uh red and I don't need those um low mid high and we can go ahead and do that and that needs to be colors not color so now I see that the minimum level has the same saturation as the maximum value right but we're gaining on it honest but what we see in the middle here that white is occurring between like 0.2 and 0.3 so what I need to do is rescale my colors to match the scale of my temperature differences and to do that we're going to go ahead and load a library that comes installed with the tidy verse called scales and what we can then add in here would be values equals rescale and so we're going to rescale the tdiff values and so we're going to give it a vector of values that we want to rescale and so I'll do the min of t data dollar sign tdiff uh so that's the lower end zero so the lower end goes with the blue zero we'll go with white and then we need the upper end which will be the same thing as this but instead of min we'll put in max and again if we take this uh function call uh I missed the parentheses there but you get the idea um that we see now though that we've got na 0.5 na I think what's happening obviously is that with this min that there's an na value and I think that's because the year 2022 this year has an na value so if I do t data uh tail yeah 2022 has an na so I need to come back up here and use a pipe I'll do drop na and now that should get rid of that so if I rerun this rescale yeah now goes 0.321 and that it's rescaling my tdiff column between zero and one with 0.32 getting assigned to the white the zero the blue one the red missing a comma here and so now we see that our lower end is that bright blue the upper end is that bright red and right at the white around zero is the white right good so let's see if we can't do a better job of matching the colors in the original heat map if we come back to this heat map I can leverage a great tool that comes installed with the mac operating system I know windows also has this tool but it's basically a color picker in mac land it's called a digital color meter and there are similar types of apps that you can get as plugins for chrome and other browsers but basically the idea is that I can put my cursor over a color and then it shows me the RGB hexadecimal that I can then plug in as a replacement for blue or red right so this is 0 8 36 b so I'll do 0 8 36 b and then for the dark red that's going to be 6 7 000 d 6 7 000 d and now I see I've got the dark blue and the dark red matching more or less what we saw on the website excellent we are gaining on it slowly but surely so now what I want to do is take the warming stripes that we have here and start to reformat it to look like what we had over on the web page one of the things to notice about both the labeled version as well as the raw version is that the warming stripes go all the way to the edge of the plotting window looking at our version of the figure we see that there is a bit of a margin around the warming stripes but within the panel and so we can get rid of that border by going into chord cartesian and then doing expand equals false you can also set the expand to be zero for scale x continuous and scale y continuous but I figured let's do it in one step and now we see that the warming stripes go to those edges which is great I'm going to go ahead and remove the legend for the heat map and up here in geom tile I can do show dot legend equals false now we can go ahead and get rid of the text and those tick marks and I'm going to do that in one step with theme void and so now we see our warming stripes go to the boundaries of the window right there's no margin everything looks pretty good and I'm pretty happy with the way that appears what I'm going to do at this point is go ahead and output the figure to a png so that it's in the format the size that I want it to be because when I start moving things around to kind of add those black margins and titles and stuff I want it to look more or less the way you know I want it this is also the raw version of the warming stripes image so I can do gg save I can put this into my figures directory and I'll call it warming stripes dot png and then my width I'm going to make eight and my height I'll make 4.5 so there's our version of the warming stripes without any annotation it's the same idea as we saw from the University of Reading again things are scaled slightly differently ours are scaled from 1951 to 1980 theirs was scaled more recently right so that gives a bit of a different appearance to the overall plot now what I want to do is go ahead and add those dates across the x-axis and we'll also add the title so I'm going to go in here and do theme and we'll do axis dot text dot x and I'll do element text and I now see that I've got those dates across the x-axis it starts at 1880 and that 18 gets lopped off again because we did that expand equals zero what I'd rather do is perhaps have it start at like 1890 and go every 30 years I think it will go to 2010 again to achieve that we can come back up here and do scale x continuous and I'll do breaks equals seek from 1890 to 2000 let's I'm going to go ahead and do 2020 if it doesn't get to 2020 it's not going to complain and again what that seek function does is build a vector from 1890 and 30 year increments stopping at 2020 right and so now we see these are going to be the years that will be on our x-axis and yeah sure enough it ends at 2010 go ahead and add a plus here so that puts our dates in a better place what I'd like to do is turn the background to be black and then set the font of my dates my years to be white and I can achieve that again here in element text I can do color equals white and I'm going to go ahead and add a margin so I'll do margin equals margin and then my top let's go ahead and add like five points in the bottom maybe let's do 10 we'll do unit equals a pt go ahead and put this on a separate line so it doesn't wrap around and then we'll do plot dot background and we'll do element rect fill equals black very good we now have that black bottom border with those dates nicely placed let's go ahead and do the title now and so the title was global temperature change and then the range of years so up here I will do labs and I'll say title equals global temperature change and I'll do 18 xx to 2002 x I'm going to go ahead and plug in the actual numbers here in a moment let's do that also this is going to plot as black text rather than white text and so if it's black then we won't see it so let's go ahead and I'm going to copy this down and we'll then do plot dot title and we'll do white and then you know let's let's try that for a margin so I think I'd rather flip the bottom and top margin to have a little bit extra space at the top and less space on the bottom I'd also like some of the space on the left there so I can again switch on plot title the t and the b and I'll go ahead and do l and let's say 20 I like the flipped top and bottom margin for the title it didn't respect what I put in for the left margin I think the for the title it really wants me to use the horizontal justification to place things along the x axis so let's go back into our code and instead of l equals 20 what I'll do is I will give it h just and let's do say 0.1 so h just of 0 be left justified 1 will be right justified 0.5 will be centered and then if we give it kind of increments in between it'll space it accordingly obviously that did bump it over I think I want to move it back half ways so I'll come back in here instead of 0.1 I'll do 0.05 I like that placement a lot better the last thing that I want to do is take on these dates so I could hard code it to be 1890 to 2021 or whatever it is but if you came along next year and did this then you know you'd have fresher data but the the title wouldn't have been updated so let's go ahead and programmatically insert the date into the title to do that we're going to use the glue package so I'll do library glue glue comes installed with the tidy verse so all you need to do is library glue and down here in the title I'm going to wrap the title in glue and in here in the parentheses then I'm going to put a set of curly braces like so I guess maybe not quite like so remove all that stuff and then in here I'm going to put a min and max function so I'll do min on t data dollar sign year and then for my upper bound I'm going to put in max right so instead of min max there we go we now have that updated year range again you could come along five years from now rerun this without having to insert the actual dates and that would go up to 2021 to 2025 or 26 or whatever the year was that you would have data for when you ran this code and so that's pretty cool right so I think we've done a very good job of reproducing the original version of the figure what do I think of this well again I think this is good for telling a story maybe for some shock value the the temperature you know losing a legend I guess the scientist in me wants that legend I think a line plot does a better job of showing the change this is a lot more artistic and I think tells a story in a different way right you know would I put this into a paper a scientific paper probably not but would I put this up on a billboard yeah that would be really compelling right so I think that looks really good the main difference at this point is the source of data and what the data were normalized to again my data was normalized between 1951 and 1980 the Ed Hawkins data from the University of Reading was was more recent and so that's going to give a different color profile right anyway I think they tell the same story that things are getting warmer that is not good right anyway encourage you as always to run through this code on your own see if you can't you know take this further and try to you know improve it and put your own spin on these warming stripes keep practicing with this tell your friends what we're doing here so we can get more people to watch I've been really just you know really pleased to see the positive feedback that people are giving me for this series of episodes I can tell people are really excited about it if you've got ideas for other things or other ways to represent climate change data by all means let me know and I'll try to work it into this series we'll see you next time for another episode of Code Club