 If you spend any time at all with GG Plot 2, you quickly appreciate there's a variety of theme functions that come with the package that allow you to manipulate the overall appearance of the figure that you're working on. These are themes like theme underscore gray, theme underscore classic, there's maybe half dozen or so. That might seem a little bit limiting, but there's also other R packages out there that allow you to install additional theme functions. Theme functions that allow you to make your figure look like it was generated by Microsoft Excel. Why? I don't know. Or perhaps it came from 538 or The Economist or any other news vendor or place that you might want to see your figure finally published. Well, did you know that you can create your very own theme function? Well, that's exactly what we're going to do in today's episode of Code Club. Hey friends, if you've been watching these previous episodes of Code Club, you know that I kind of like a bright screen when I'm coding, but not everybody likes that. Many people like what's called the dark mode, where you could take your R studio session or your Chrome or any other browser or interface that you're working in, and instead of having a white background, you can make it have a black background. Well, why is that relevant? Well, if you've been following along in recent episodes, you know that I've been building a slope plot that shows people's willingness to receive the COVID-19 vaccine from August of 2020 compared to October of 2020. Yeah, the data are over a year old now by the time I'm publishing this, but it's a useful data set for thinking about how we can work with paired data. Now in the last episode, we talked about a variety of ways that we could use coloring so that the line in our slope plots could be colored by the degree to which the willingness to receive the vaccine changed across our 15 different countries. Now, for some of the countries like Canada and India, there was no change in people's willingness to receive the vaccine. And so those were colored in white. Well, if we have a white background or even a light gray background, you really can't see those lines. So what we did was we got a darker background in the plotting window. And that kind of looked a little bit odd, right, to have this rectangle in the middle of the plot that was gray, or surrounded by a field of white. What I want to do in this episode is create a dark mode theme that we can apply to our figure so that the whole plot is dark. And then all the other text is light or gray or white, so that it sticks out, you know, conversely to how you're having a white background in black text, you know what I mean? So that's exactly what we're going to do in today's episode. I think this will be a useful illustration to get you thinking about how you might create a theme for your research group or kind of your just overall general shtick. If you look at my previous videos, you know, I kind of have the same type of themes that I'm changing in the theme function that I turn on or off, and that could be useful to show up in my very own personalized theme function. Well, like I said, we're going to go ahead and create a theme dark mode theme in today's episode. And along the way, we'll take this slope plot that we created in the last episode and convert it to this and make it look a lot more sophisticated and a lot better. So I've got my R script loaded up here in our studio. If you want your very own copy of this, let you can follow along with me. Again, down below in the description here, you might have to expand the text box, you will find a link to a blog post for today's episode that gives you links to my GitHub repository that I'm using at the current state of the repository right now, as well as at the end of the episode. Also at the end of that blog post, there is a set of data, the data that I'm using, and that you can save into your very own file called August, October 2020 CSV, and you can follow along, and you'll get so much more out of it if you follow along. You know, perhaps you want to watch this through, great, get the idea of where we're going, and then come back and do it yourself. And then the best next step would be to take what we're doing here and apply it to your own data. Let me know how that goes. So this is the figure that we're starting with in today's episode. Again, what I want to do is to convert this to a dark mode, where the white margin, if you will, or the padding around the plotting window is also dark. And so everything's the same color. And to do that, of course, we'll need to change the color of our text and perhaps some other things so that they stick out and so that our labels don't kind of blend into the background. Before we do that, I'm going to come down to the end of my pipeline for building out that figure. And I want to show you a couple of things. So I'm going to go ahead and comment out all of my code for modifying the theme. So something to know is that the default theme is gray, theme gray, right? Again, if we don't apply any of our own personal theming, this is what we get. You'll recall that we used element text box simple and element markdown. We've manipulated this quite a bit, as you saw in all those lines that I commented out. But this is the default appearance of the figure. So I'm going to come back up to the top of this pipeline for building out my plot. And I'm going to sign this as a variable P. And so P is going to hold that plot. And believe it or not, yes, you can store a plot, a figure as a variable in R, which is pretty slick. So why would I want to do that? Well, let me come down here and remove the comments from around my own theming. And I can then do P plus that. And of course, then that brings us back to the figure that we've already seen. So it's a way to split up the construction of your figure. And I think we've seen this in other episodes, kind of like where we had the dumbbell chart. And I was putting labels at the top. I think I built that in several parts by assigning a figure to a variable name. So what I'm going to do here now is I'm going to say P plus theme dark mode. And what I want is for this then to generate what we just saw. Okay, but in dark mode. And what I will do is move this down below. And you'll notice that of course that I don't have a theme dark mode. So again, what we can do is we can create a function called theme dark mode. And we're not going to assign any arguments to it at this point. And then we'll put the theme inside of curly braces. And what we want to do is we want to start with one of the default themes and then add on our extra theming. So I'll do theme gray and then add to that the rest of my theme. Okay, so now we can do theme dark mode. But again, we see nothing has changed yet. Before we get going revamping our figure and our theme dark mode, we need to add one more thing. And we're going to replace this plus sign with a special version of the plus sign. And that's gonna have a plus. And we'll say plus replace percent. So percent plus replace percent. And that is going to take the content of plot title and replace all of the previous values of plot title. So let me show you what this means, right? So if I open up my console down here, and if I do theme gray, as a function, then I get all of the different attributes for everything that's in here, right? If I do theme gray, dollar as a function, dollar sign, plot dot title, I get those attributes of plot title. And so it tells me that my size by default is 1.2, 1.2 bigger than the default size coming into the figure, right? And it also tells me my margin and in all those other things, right? Which is great. And so what we're going to do is we're going to replace all that with what's in my element text box simple. So now, again, if I load my theme dark mode, and I do theme dark mode with plot title, I now get all of that other stuff. But what I notice is that my size is now 28. And that's because I had plugged in 28 as my actual size, right? And so that's replacing the other size. If I actually remove size equals 28, and then go ahead and reload theme dark mode as a function. And now I come back and I look at the size. I see that the size is null, right? It's no longer the 1.2 as the relative size or 1.1 forget which one it was, right? And so the replace takes all of the values that I give it here and replaces the current values, right? So when we do theme, add theme, we're only changing one thing, right? So we're only changing the font family to Montserrat and element text. So as another example, if I again do theme gray on text, I get the variety of default formatting and default size. So the default size is 11 point, right? And tells us the face is plain, the color is black and so forth, right? But if I do the same thing, but with theme dark mode text, I now see all those things are null, but my family has changed to Montserrat, right? So again, the replace is allowing us to deal with the blank slate, not worry about what is being carried over from theme gray. So hopefully this makes sense. I'm going to work with using the percent plus replace percent. You can maybe give it a shot using the simple plus again, the challenge there is that there might be some baggage that you're carrying over from theme gray that you don't anticipate. So anyway, we'll start from scratch using this replace. So I'm going to put back in my plot title, the size equals 28. So let me come down and I'm going to make my plot background. I'm going to make it black. So that's going to be the zeros, the six zeros. So we'll go ahead and put in plot background and element rect, fill equals black, we could write black, but I like using the hexadecimal because I feel like it allows us to more finely control the different colors we'll get. So that's dark, right? Great. So there's a couple of things that stand out that we need to work on. So first, the thing I notice is that my y axis title has not been turned 90 degrees. Again, that's because we use that percent plus replace percent. So we'll need to turn that our title is also in black, except for that part where we use CSS to turn it blue. And the other thing is that our legend background is white. So we'll go ahead and update those things three things and then we'll find some more things to fix. All right, so axis title y, I'm going to go ahead and here and do angle equals 90, that should get that turned. And we will then also do plot title. I'm going to make this color equals white. And then I also want to do legend background equals element rect. And I'll do fill equals that black again. Good. So we pivoted that y axis label. We got our title to be white and our background to our legend is also black. So now we can't see the values in our legend. So let's go ahead and turn those back to white. We also lost the labels of our different countries. So let's also turn those to white. So we'll then in here do I'll come up here and do legend dot text element text color equals white. So it's going to be six F's. And we also want to come back up to our geome texture pal. And our color, we want to set to be a white. So we'll do color equals the hash FF. So this is looking a lot better already. Something that sticks out to me as being just a little bit off is the size of the caption seems a bit larger than it should be, as does the size of the text in my legend. And I feel like the text over here in my axis text also seems a bit larger than it should be. As I showed you, we can come back and we can look at theme gray. And we can then look at what is the size of our plot dot caption. And we can see that the size is relative 0.8. And so that means it's 80% the size of whatever was coming in, which I think was 11 point, we can also do the same thing with legend text. And there again, we say that the size is point eight, we can also look at axis dot text, and see that that's also 0.8. Good. So let's come in here to our legend text, and we'll do size equals rel 0.8. And I'm going to copy this because we're going to repeat this for my caption text, and then for my access text. So let those font sizes seem a bit more proportional to what I was expecting. The access text, it looks a little bit too small. So I'm going to take that out. You know, we don't have to do everything that we saw in theme gray. So I'm going to go ahead and take that out. And I think that looks pretty good. A little stylistic thing that I want to change in my version of this theme dark mode is to remove those axis ticks. They're just kind of floating there and not doing anything. I'd also like to put in a really thin grid line, going across the figure. And I'll also go ahead and put the vertical grid lines in here because we might want to use this theme for something other than for these slope plots, perhaps we'd want to put it in there for, you know, any other type of figure. I'll go ahead and turn those off eventually for our own slope plot here. But for now, let's go ahead and remove those tick marks. So we'll come back up here to our access section of this theme, and it'll do access.ticks equals element blank. And that will remove those. Also, I see that my panel grid, we had turned them off. So I'm going to go ahead and replace blank with element line, and I'll set color to be white. Now we've got our white grid lines. We also have those minor grid lines in there, which I don't want really want only want the major ones. And maybe I want them gray and I want them to be a bit thinner. So to clean up those grid lines, I'm going to come back up to my panel grid and do panel grid major and do element line the side the color I'm going to do eight, six, eight to give kind of a medium gray color. And for size, I'm going to do 0.25 We'll start with that and maybe we'll have to adjust. And then we'll do panel dot grid dot minor. And I'm realizing I misspelled major as I do. And then we need to give panel grid minor element blank to not represent any line for the minor grid lines. So yeah, we've gotten rid of our tick marks on the y axis and on the x axis, I don't think they were adding anything. And I like having this thin horizontal grid line only on those major points. I think that looks really good. It's pretty clean and subdued. The y the vertical ones, I don't think we need for the slope plot. Again, if we're doing some other type of visual, we might want to leave those in. So to remove those vertical grid lines, I'm going to open up a new theme function. And again, here we will then do panel dot grid dot major dot x element blank. So there's one more thing that I want to do with this theme. And that is to allow the user to provide a background color, a primary color and a secondary color, right? So our background color here is black, our primary color is white, and then our secondary color is this gray color here in my function definition for theme dark mode. I can give arguments. So I'll do bg, primary and secondary. And I can actually give default values for each of the arguments in here. So for my background, I can give it black, right? So hexadecimal for black. For my primary, I can give it white. And then for my secondary, I can give it that gray color. Right. And so now I want to replace wherever I had black with bg. And so where do we have those zeros? That would be down here. So bg. And then wherever I have white, so all fs, I want primary. And so that's right here. And I think that's so the only one. Here we have a white written out. So let's go ahead and put that there. That looks good. And then our secondary is anywhere that we had a gray color, right? And so here we have gray. So we'll replace that. We'll replace these eights. And we'll replace that. Good. So I think we've replaced all of the colors with our variable names. And so now we see that we have our dark mode, right? So I could do something silly, like put a bg equals, I'll just put in red, and our primary equals yellow. Let's see how bad this looks. That looks horrible, but isn't that cool. So something I do notice, however, in this mode is that our rectangles for our backgrounds actually have a black line around them. So let's go ahead and turn those off. But anyway, let's stop and admire just the goddiness of this, but also the coolness, right? So we've created a dark mode, but you could, you know, set those three different colors to get the contrast that you want. And again, that's a powerful thing about having this as a function. Let's leave that and let's come to these backgrounds. So I'll do color equals na. And I'm going to copy that down for these three different backgrounds. And so now those black borders go away. And I don't want to say it looks good, but we've got it cleaned up on alternative that I can do is I can name a variable for these different colors, right? And so I could then say this is my background, primary, and a secondary. It's okay if our value or our variable name is the same as the argument name, so I can define the variables background to be the black, and then my primary to be white, and my secondary to be that gray, and my primary spelled wrong, and voila, there we are. We've gone back to the original dark mode version of our figure. So that works. And of course, then we can come back up and we can change our colors. So, you know, we can put in red for a background, update that, and then we can come and rerun all this. And there we go, we have our red background again, again, we don't want to do this. But again, for demonstration purposes, to show that we can very easily manipulate the appearance of the figure with these three different color variables. All right, so I'm going to put this back to black. Don't worry. But one thing that sticks out to me about this figure that I'm not quite a fan of, is that if we look at Australia and Germany, there's a thin line, the grid line, going behind the name of the country. What I'd like to do is get rid of that, right? And so, you know, you might do a bunch of different tricks by manipulating the grid line. But what I want to do instead is manipulate the label itself for each of the countries. And so what I'm going to do is I'm going to go from geome text repel to geome label repel. And I'm going to turn the background of the label to be the same color as the background of my figure. And you won't see this strike out effect like we see currently with the grid line for again, Australia and Germany. Before I forget, let's go ahead and put this background back to black. And we're going to come down to our pipeline where we made P. And instead of geome text, we'll do geome label repel. So for color, I'm going to replace that white with actually the primary variable name. And then for the fill, I'm going to use the background variable. So that gets us the appearance that I was kind of going for. There's a few things I want to change. So the first thing I want to change is the padding around the name. I want that to be as tight as possible. I don't really want the label. I also want to get rid of that border. So I'll do label dot padding. And I'm going to say zero equals zero. And so you can then see that that label gets a lot more compact around the name. I can now get rid of the border of that label by coming back up to my geome label repel. And I can do label dot size equals zero. And that then removes the border of the label. One other thing that I kind of noticed is that we have, for some reason, these short segments between the label and the line. Let's go ahead and increase that minimum segment length. I'm going to up that to say 0.5. And so that gets rid of most of those. Now I have an idea. As I mentioned in the last episode, one of the challenges with this visual as it stands is knowing easily, at least, what line corresponds to which country, right? So you might say, well, what does Japan correspond to here? Or is it the red line, or is it the blue line? I don't really know, right? So because we're working in dark mode, and, you know, if it is white, if the country's name is white, like Canada, it'll still stick out. So what if we colored the label, the country name, by the percent change? Let's give that a shot. So let's come back up to our geolabel repel. And then I'm going to put in here as an aesthetic for the color to be the change. And so I also need to remove this color equals primary. So it's saying change not found. And if I come back up to geolabel repel, it's using the country labels data frame. So if I come back up here to country labels, let's see what it looks like before we do that select, I think we forgot to select it. So there is a change column here. So let's go ahead and add the change column. I'm happy to see that labeling the country by the percent decrease in intent to receive the vaccine works really well and makes it a much easier to see which country corresponds to which of the lines. And I like the trick that we used of using that label with the background so that we no longer have that strike mark through Australia or through Germany. So I'm really happy with the way this theme dark mode has come together, something I would like to change on the overall version of the figure, perhaps not necessarily in the theme is the appearance of my legend legend dot text equals element text, I could do then color equals let's do 999999. Of course, I need the pound. So it's going to be a little bit brighter than our standard dark color. So those numbers now are a bit more subdued. They're not so, you know, bold, you know, I really want to use the white to highlight what's most important in the figure and use the color to highlight what's most important. I don't know that the actual values here are super important. So by making them gray, they're a bit more subdued. I'd also like to make them write justified. So I can do that in my legend dot text dot align. And I can then say one. So one being right justified. And so again, that then right justifies those makes it look a little bit cleaner. Finally, I want to go ahead and get rid of those tick marks that are actually in the legend to do that. We're going to use a special function that I think we've maybe seen in a previous episode, we should do a full episode on this but guides color equals guide color bar ticks equals false. That then gets rid of those tick marks. I know I keep saying one last thing, but one last thing that I want to do is I want to go in and change my primary background. I don't want it to be so jet black. Why don't we go ahead and make it a little bit more of a like a midnight. And so instead of all zeros, let's do all threes. And then let's bring this down a notch and let's make these all fives. So we'll make our secondary a little bit darker or background a little bit lighter. Yeah, so I really like having this dark gray background rather than the jet black background. I think it really helps the colors and the whites to really pop out more, believe it or not, than having the jet black background. I don't know. You do you let me know what you think works good for a three color combination. You know, maybe you actually do want a red background or maybe a yellow background who that might be pretty horrible, right? Anyway, I think this looks really good as a slope plot. And I'm pretty pleased with how this all came out. Again, the motivation of having our theme dark mode is that now I can use all this styling in future versions of whatever figure I want to make. If I want to make a line plot, I can use this theme dark mode there to automatically style it without having to go through all these changes. I get a style I like, and I can replicate it, right? That's that's the heart of reproducibility. I'm perhaps not to the point of wanting to make my own package of my own themes. But you know, we could get moved there. One thing I'd want to do before I made a package out of this theme was to use this theme in a bunch of different contexts. You know, maybe make an ordination, maybe make, you know, a line plot, maybe make, I don't know, any other type of plot and make sure that the choices the design choices I've made here hold up for those other types of figures. Anyway, I've gone on way too long already in this episode. But I hope you found this interesting as a way to think about how you can encapsulate your own sense of style, develop your own theme. Let me know what you come up with. Maybe we could kind of make a Code Club theme package where we could put everyone's theme in it together. And we could, you know, we could we could share each other's aesthetics. Wait, that'd be kind of fun. Anyway, let me know what you think. And we'll see you next time for another episode of Code Club.