 If you've watched any of my previous videos, you know that my color scheme for my clothes, for my room, it's pretty boring. I like blue, I like red, I like dark. I don't know why. I think mainly because I don't have a good sense of color and I don't know what colors work well together. So when it comes to making a plot, it's all the harder to pick good colors that go well together. In today's episode of Code Club, I'm going to show you the strategy that I use to picking colors that work well in a variety of environments. Hey folks, I'm Pat Schloss and this is Code Club. In each episode of Code Club, I use reproducible methods to apply them to an interesting biological question. The biological question or the question that I'm working with doesn't really matter because today we're going to talk about how we can change colors in a plot and how we can choose colors that work well together. Perhaps you've run the colors function in R and seen the hundreds of different named colors there are, things like thistle. What color is that? Much less thistle five, right? I don't know. So what we're going to do is we're going to use a online tool called Color Brewer to help us to pick a set of colors that will work well in our figures. If you recall the past couple of episodes, I put in a few colors. I think it was like black, red, blue, green. Again, primary colors. I don't know anything about color, but I know that those colors are bad in part because red and green do not work well together. A good fraction of men cannot differentiate between red and green. And so if you're giving a seminar and you're using red and green to highlight different treatment groups, know that if there's say 50 people in your audience that the odds are good that you'll have a few people that can't see the difference between those colors. And so that makes it all the more a good reason to pick colors that we can use to discriminate between your treatment groups that are accommodating the largest number of people in your audience. Along the way, we'll see a reproducible approach that we can take to make sure that our colors are consistent across figures so we have kind of consistent branding, if you will, within our own manuscript. I'll show you Color Brewer, which is really good for picking color palettes that keep in mind things like red, green color deficiency, and I'll also show you another color palette that I'm a fan of for a little bit more whimsical from the West Anderson color palette package as an alternative to see what other kind of color palettes are out there. Okay, let's go ahead to our project. And I'm going to, for now, open up my figures, my PDF files, and show you what we've got so far. This first one, where we are looking at the number of ASVs per R and copy is black with that blue line. And so that doesn't have any color that I need to worry about. And over here, for our rock curve, that's the supplemental figure one, and then this our lumping and splitting, which will be figure two, I think. Again, we have black, blue, green, red, those are not ideal colors. And so, but I have the same color scheme in both plots, because again, I've got the same four regions. And I want someone to know that this black in figure one, or figure two, rather, is the same black that's in supplemental figure one. Good. So that's our task today is to find good colors that we can put into these figures. Let's go over to our browser and a website I want to share with you is called colorbrewer2.org. It's designed, I believe, for cartographers, people that make maps. But I find it also useful for picking color schemes that go well together, as well as satisfy a certain set of criteria. So we have, we have four groups, data classes. So I'm going to go ahead and change that to four. And you'll see that there's sequential sources kind of like heat map rate, where you have like low value in this lighter color to high value in this darker color. There's diverging, which is kind of the same, but going in opposite directions. And then there's qualitative, which is the one that I'm going to work with. And so you can see that we've got four qualitative different colors for my four different classes. The other thing then we see here are that there are eight different color schemes that we could choose that would allow us to have four qualitative data classes. Down here, this four class accent, it that is the name of that class, I believe. And so this one is dark to and there's a variety of these different palettes. There's the hexadecimal code here that we could use in place of named colors like black, blue, red, green. I'll show you how we can do that. The other thing I like about this site is that you can have it show you only those palettes that are colorblind safe. And so that narrows it down pretty quickly to this four class paired, which doesn't give us a lot of choices. So thinking about it, what I might do is three classes and and I'll add black as a fourth class. So I don't like having two shades of blue there so much. I kind of like this dark again dark. I don't know why color palette. And so this again is dark to and it gives us those three hexadecimal codes. So I think I'm going to work with this. I like this and I like the confidence again that I'm picking a color palette that is accommodating of the most people in my audience and keeping in mind the sizable fraction of people, especially men that can't can't differentiate between red and green. Okay. So coming back to our studio. Let me go to my code directory. And I am going to go ahead and open up. Let's do the lump split first. And so here you'll see I have black, blue, green, red. Something to know is that there actually is an R package called our color brewer that allows you to do the same types of processing that I did here on the website. Over in our I personally find that this interface is just easier for me to work with. I encourage you if you're interested to explore it a little bit further and check that out. So what I'm going to do is I'm going to go ahead and copy these codes. So this is hexadecimal the first two digits. So there's a pound and that tells are that that's hexadecimal. The first two characters are for the red color. The second two are for the green color. And then the blue so red green blue. If this color were red, it would be ff 0000 and for green it'd be 00FF 00 and for blue it'd be 0000FF. And so again, these are hexadecimal codes to give you values between like zero red and full red. And so I'm going to replace blue with that green with this and then red with this. And then we will go ahead and I'm going to come back and do make figures. And this was lump split dot PDF. So I make files complaining at me. I'm not sure what that's about. We'll come back to that, but let's go ahead and open figures rock curve dot PDF or not rock curve. Sorry. It's complaining about rock curve dot tiff. Let's do open figures lump split dot PDF. And so we can then see if we zoom in here are four different colors for v19 v34 v4 v45. I think those those look okay together. Something I want to try is what happened if we instead of black did a dark gray. I think if we do like 999999. That should get off. Give us a dark gray color that's not quite black. And I think I'll go with the black. Again, some of this is a matter of personal preference. And, and there's not necessarily a right or wrong answer. It's again personal preference. And I've got a pound sign before the black complaining about, Huh, simple things get hard. And so we see that's black, and that looks good. that looks good. I like having v19 v1 v9 v black because that's kind of the reference for all the others, because that's kind of like the best case scenario. And so I feel like black is a reference to me, it signals reference. And then those other colors are other colors, right other other levels. So that should be good. I'm happy with that. And yeah, I think that I think that looks pretty solid. Now, I'd like to replicate that over in my my plot rock curve file. And maybe we'll figure out what that what it's complaining about there. So I'll put that in here, copy and paste that in. And then I will do make figures rock curve dot PDF. And let me open up that. And so here we see those colors. Again, I think that looks pretty good. I'm happy with that. Again, the more categories you get the harder it is to pick colors. I'm going to open up Adam and figure out what's going on with my make file. The error message I'm getting is overriding commands for target figures rock curve dot tiff. So I must have that in there twice. So line 136, 140 of my make file. Do I have this in here twice? I do. Ah, I must have been doing some copying and pasting. So let's make that tiff. We'll save that and let that run again. Error message goes away. We're up to date. One thing I wanted to show you, I haven't been committing with you because it's boring. I've done it a bunch of previous episodes is that when I run this r script, it automatically creates this file r plots dot PDF. And what I'd like to do is remind you that we have this file called get ignore that we can use to tell get to ignore certain things. So I'll say our plots dot PDF. And now if I do get status, that's gone. But we have modified get ignore. So I'll do a get add dot get ignore. Get commit dot m. And I'll say ignore our plots dot PDF files. Okay, good. All right. And of course, we still have these other things that are a work in progress. So what we've done is not entirely is what I frequently do, where we copy and paste that color line over and over again. But it's not dry. And we've talked about dry in the past, which dry means don't repeat yourself. So if I want to change my color scheme, I have to do that in both of these files. And that's not a huge deal. But it's, you know, especially with these codes, it can be, it can be hard to detect differences. What I'd like to do with you is create a new r script that I will call. I'll save this and call those colors dot r, and we'll put it into into code. And I'm going to take this line, this command, the scale color manual, I'm going to paste it in here, and I'm going to make this a function. So I'm going to call this custom color scale. And it's going to function. And and what should happen is that if we save this, and then source it, if we source colors dot r in here, if I do a source, let's do here, and then do code colors dot r. Well, let's see, maybe you need to run all this stuff too. I don't actually need that library knitter. I've got a plus sign at the end here. So that's not good. Alright, so we'll save that. But now we should have everything in good shape. That's nice. And so now if I do down here, if I say color, custom color scale, it adds that information. And I should be able to replace this scale color manual. So I'll comment this out. And instead, I'll put custom color manual. Alright, so now if we run this, I forgot to run everything else. So let's see, let's run everything else to regenerate this plot. This is the rock curve data. Couldn't find function custom color manual, custom color scale. Yeah, and that worked, right? And again, now if I look at my, again, if I look at my figure of the rock curve dot PDF, I see that it's got my colors. And I can then change my colors in that colors dot r file, and it'll update it there. So that's not such a gain. If there's only one file that we're doing that on, I'll go ahead and remove this. And I will put that into my plot lump split dot r. Again, replacing this scale color manual, put that in there. And I need up here to also do source quote code forward slash colors dot r. Save that. I'll save my plot rock curve dot r. And we're in good shape. So again, I could do make figures, lump split PDF, and then open figures lump split dot PDF. And it's got my colors. Great. So to show you how this would work, if I come back to colors, and instead of black, I make this red, I save that, I then make. Ah, I need it didn't do anything, because I need to add that as a dependency, right? So I need to add code forward slash colors dot r. And then also add that down here. And so now if I do figures lump split figures rock curve PDF, I forgot to PDF. And then I can do open figures rock curve PDF figures lump split dot PDF. And you now see that that red line that that black line is now red. And again, I can update it in that one file. And then it gets propagated across all my other scripts for building these figures. Okay, so I want to show you another color palette that is a bit whimsical, I'll say. And so if I Google search for r and West Anderson, this gets you to a GitHub repository for the West Anderson color palette package. There's a similar one based on colors worn by Beyonce and various photo shoots and whatnot. But these are color palettes inspired by West Anderson movies. And so again, these aren't necessarily the best colors for being friendly towards folks with red, green color deficiency. But I don't know, they allow you to personalize things a little bit yourself. So I kind of like this moonrise. I've never used this before. But why not give it a shot? It's got four colors. And so what I will do is we can install this package. If you don't already hear if you don't already have it installed on your computer, and it's this repository has instructions on how to install it. So again, if I come to our studio, I can do packages West Anderson doesn't look like I have it installed. And then West Anderson, it will install this. And I can then maybe in here I can do library West Anderson in my colors are and in my values. I'm not sure I want to totally get rid of this, but maybe I'll comment out that line and put in values. And then I can do what it said. West Anderson, where'd you go? Let's do moonrise two. So if we do West palette moonrise two, we can see in our studio here, if I do West Anderson, West palette moonrise two, I have to do library West Anderson. And then if I do that palette, I get those four colors. And so that is a West palette. And we'll do moonrise two comma there. And now if we redo our make on both of those files. And we can then again do our open figures rock PDF figures. lump split PDF. We again get our four different colors. You know, those don't look so bad. They're a little bit muted. Again, a lot of this is personal preference and what you think looks well together in terms of composition. And the nice thing about like the West Anderson, if you don't like moonrise two, that perhaps we could do. Oh, what would be another one to try? Let's try this chevelier one. I'm not a big fan of West Anderson movies. They I've always kind of found them a little bit tedious, but I kind of like the color palettes that they they concocted here. So and so here is that color palette. Maybe let me know what you think in the notes in the comments down below. What color palette do you like? Know that there's a lot of different are color palettes out there that you can choose from. Have fun with it. There are color palettes that correspond to like team colors. Am I wearing my cubs hat? Yeah. So you can get like cubs blue and cubs red, which is basically just the primary colors. Not not very adventurous there. I'm probably going to scrap this and go back to using the our color brewer colors here. That wasn't the one I want to delete that one. And so again, the nice thing about having this script, this colors that are script, is that I can update it and it will then update everything else. And so I can then put this back to black. And we'll regenerate those figures. And we can also then update our make our submission manuscript PDF, because it'll trigger manuscript PDF to regenerate, because the figures are prerequisites, or prerequisites of building our manuscript file. So I will then do open submission manuscript PDF. And if I come back down to the bottom, we see that I've got my four colors. And that all looks great. So one thing that caught my eye as we're scrolling back and looking at our figures is that I have our end here in plain font, plain font face. And operands are supposed to be italicized by convention, at least I think. And so what I'd like to quickly do is show us show you all how we can get our end to be italicized. And this is made possible by a package called ggtext. And so if you search for ggtext, see if you've got that installed. If not, go ahead and install it. ggtext. And that's installing. So what I'll then do is go back to my file of plot ESV rate. I don't know why I called it ESV rate, it's ASV rate, whatever. And so what we can do is up here with our libraries, I'll do library, ggtext. And what allows you to do is to use markdown to modify the labels in your plots. And so around our end, we can put a single star. And again, if I make that, and I then save that, and I do make figures, yes, ESV rate dot PDF. And then I open that here. Once this is done running, you'll see that we have the stars around our end. But what we need is with that ggtext, you get a new element called element markdown, which we can then be sure you put a comma at the end of that strip text line, we can do axis dot title dot y equals element markdown, let's save that. And then we will then remake that and look at what it's done. And sure enough, RN has been italicized. This element markdown is a game changer for those of us in microbiology, because sometimes we'll have values or labels on our x axis or y axis or wherever, where we've got bacterial names that we need to italicize, but we don't perhaps need to italicize other things in the figure. And so that's really nice. So I again, need to then update my manuscript and make sure that that gets integrated into the change, change the figure. And if we zoom in, we see that sure enough, the modified figure got integrated into our markdown document. So wasn't intending to cover that today. But that caught my eye and wanted to be sure that we covered that before we move on to the next task. So the next task, what is that? Well, if you look at our figure legends, they're pretty, pretty spartan, they're pretty minimalistic. And so we need to flesh those out. And then I think we'll be in good shape with our figures and ready to move on with the rest of our manuscript and editing and writing abstract and the important section and then getting ready to get it off our desk and submit it. Anyway, I hope you found this discussion today of colors and how we can get different colors into our figures in R. I hope you found it useful. I hope you also consider that this issue of red, green color deficiency, and that we need to do our best to pick colors that aren't red and green. Red is fine, green is fine, just not together. Know that there's a lot of other color palettes out there. We used our color brewer. We also saw the Wes Anderson. There's another one for Beyonce. There's sport teams. There's one called Veritas, all sorts of different color palettes. Aside from picking colors that stay away from red, green color deficiency problems, it's largely a matter of personal preference. Another thing to keep in mind is that we really want to keep the number of variables or levels of our variable to a minimum. We saw that as we go up the number of different classes that we need colors for, the options become more limited. I've seen this be a big problem for people doing microbial ecology studies where they're trying to do these stacked bar plots with like 30 different colors and nobody can differentiate between 30 different colors. And so we need colors that contrast with each other. And to make that most effective, we really need to minimize the number of different classes we have for each of our categorical variables. That's not always easy to do, but again, keep that in mind. All right. So again, I hope you found this useful. Thank you for spending your time with me today. Please be sure to share this with your friends and get them thinking about colors, get all of us thinking about what works and what doesn't work in our different visuals. I don't claim that these plots are the best thing ever, but I think they're pretty functional and they do a good job of showing the story that I'm trying to convey to my reader. Anyway, keep practicing and we'll see you next time for another episode of Code Club.