 Hey, folks, I'm Pat Schloss and this is Code Club. In the last episode, we converted the dumbbell chart or barbell chart into a slope chart. Again, there's many problems with this visual, but one of the big problems is that we don't tell the audience what they're looking at. So in this episode, we're going to use the labs function to go ahead and add some text annotation to the figure to add a title to add an annotation to play with the x and y axis labels to modify the title of our legend. Then we're going to use the theme function to modify everything you'd ever want to modify in the text of those different attributes that we're going to modify. I have the code opened up that we created in the last episode where we converted that dumbbell chart to our slope chart. You can grab your very own copy of this code. By going down below in the description, you'll find a link to a blog post for today's episode. And there are instructions on how you can get the code as well as the data, as well as the final code product from the end of this episode. As you can see, I have the tidy verse and show text packages loaded from show text. I also have these font add Google functions as well as show text auto. This will allow us to add special fonts so that we don't have to use the boring aerial font in building our figures. We then go through and build out the data frame pivot it longer. And then we go ahead and make the slope chart. Let's go ahead and run this, make sure we have a good looking figure. Well, it might be a bit of an exaggeration to call this a good looking figure. It generated and looks the way we anticipated. So let's go ahead and now we will add the title and captions and start modifying the rest of the text that we see in this slope chart. I'm going to go ahead and add some text to this. And so we can add text labels and titles and whatnot using the labs function. And so we can say title. And for now, I'm going to put in title in all caps. And I just want to give us a sense of where these different titles or labels appear in the figure. We can do subtitle, subtitle. We can do caption equals caption. And then we can do x equals x title and y equals y title. So again, hopefully you can see where all this placeholder text went to in this rendered figure. The tag naturally goes to the upper left corner. Where I might use this in science is to, you know, if each figure file I need to submit to a journal, I might say in the top left corner figure one, some journals want you to have that kind of label, or you might, you know, if you've got four parts to a figure, you might put a in that upper left corner, and then the next one be and then C and D. I don't use tag too much. And we'll probably ultimately be removing it from here. The title is the title of the figure. Again, for papers, we generally don't put a title in here. But you know, if you think about it, most of the figures we present aren't in papers, they're in presentations. And so it'd be nice to have a title for a figure, as well as a subtitle that's then below the title. The caption is right justified. And so it shows up in the bottom right corner. And then we have our x title and our y title. One other title that we can modify actually is the title of our legend. I normally modify this in my scale color manual function where I can then say name equals country and perhaps capitalized, or normally more likely than not, null to remove that name. So let me show you how we can actually do that because that's something that I actually just learned recently. So I could then say color equals and I'll put in capital country. So that puts country capitalized now, right? So I went from lowercase, which was the column name to being capitalized. So again, all of these different text elements, we can modify using the labs function. The one thing we can't modify by the labs function are the actual variable names on the x axis or the y axis. And that's because those are more data centric than being data independent. And so what we're talking about today is how to set and modify those elements of our figure that are text based, but that aren't being driven by the data. Something that I often find is helpful is to set different labels to null. If I go ahead and say set the color to null, so that got rid of the title of my legend, I think it's obvious that these are country names. And so I don't really need to have a title for that legend. I also think it's obvious that my x title would be month, right? Because these are two months. So let's go ahead and set x equal null. Again, that got rid of the x axis title, cleaning things up a little bit. Let me show you something that I commonly see people fall into as a mistake. And that might be putting null within quotes. That then says that I want my title to be null. So no on its own without quotes will get rid of the title. But if you put null in quotes, then you will get null to be the title of that axis. One other thing that I sometimes see people do is they might set x to be a pair of double quotes, that then makes the title of the x axis to be a blank string. And as you can perhaps notice, there's now a bit of a gap between August and October and caption. And that's where that blank string is going. So again, if you don't want a title there, go ahead and use that null function. So I'll go ahead and put that in. And you can then see that there's no longer that gap between my two months and the caption. Again, I'm trying to create a slow plot version of that dumbbell chart that I created in the last episode. And that plot does not have a tag or a subtitle. So I'm going to go ahead and remove those by setting those values to null. Of course, the default value for subtitle and tag is null. So I really don't need those lines in here. And so again, I can say a subtitle equals null. And to kind of show you what I mean, I can remove that tag line. And that got rid of the tag and also got rid of the subtitle. I'm of mixed opinions on whether or not to leave in null for things that are by default null. In one hand, it's nice because then it, you know, it makes it easier to know what the actual value is, rather than using the default value. So, you know, I think for this plot, I'm going to go ahead and put tag to null. And again, the plot looks the same. And we now have the placeholder text for everything that I want to add to my figure. So of course, I don't want my title to be title, and I don't want my caption to be caption. I want to go ahead and put in the actual values, the actual string that we used in the previous episode. So if I come back over to the labs function that I had in my August, October 2020 makeover dot r script, I'm going to go ahead and copy that and plop that here in my code. And I'll grab that title and put that in for title. And I'll take this caption. And I will put that in here for caption. So I'll go ahead and delete those lines that I copied over. And I now notice that I've got a variety of red X's in the left hand margin that are a little bit disturbing. And I also noticed that my text goes from the arguments being black to being green. And so this all tells me that something happened when I copied my text in. If I put my cursor over one of these X's, I see unexpected token quote comma tang equals null X equals null Y equals quote. And so that tells me that perhaps something is off at the end of this line. And sure enough, I've got something underlined here with the red squiggles. And I see that I have two quote marks, I'll go ahead and remove that second quote mark. And now I see that all of that horrible red look goes away. So the title and caption came in. And well, they don't look good. Things look like they are where they're supposed to be. So for my y axis label, I'll go ahead and do percent willing to receive vaccine. So again, we now see that y axis label. Now I want to turn to talking about how can we format the text using the theme function. So if we look at the help page for element text, we'll see the documentation on how to use that this useful function element text can be really handy for modifying the appearance of the different text attributes in our figure. We see that we can set the font family, the face, whether it's bold italics or bold italics, the color, the size, the horizontal and vertical justification, the angle of the text, the lion height, the color, the margin. And that's pretty much it. So you'll notice that I said color twice, once is with British spelling of color, and one is with American spelling of color. So we'll use the element text function as a function to assign to a attribute or an argument in the overall theme function. And so we can then say plot dot title, and I can then say element text. And let's look at a couple of different things that we might try. So we could try family equals serif. And I'll put that in quotes. We could say size equals 30. And we could then say color equals blue. And I could also say h just equals one. And so what this did was made a really big serif font in blue that is right justified on our plot. This is obviously not what we want, but I wanted to show you a few different things that we could modify an element text to get this appearance. So what I do want to use for my family is patria one. Let's turn the size down to maybe 25. And I'm going to remove the color equals blue, as well as the horizontal justification equals one. So now our title is that nice strong patria one font family looks pretty nice. So one other thing to notice about the title is that it's actually left justified on the panel the plotting panel, which is this y axis position. I would rather be on the plot as a whole kind of move it over a bit on the figure, so that perhaps we can make have more real estate left to right horizontally, we can then make our font bigger. So the title looks even more striking. To do that, we can add another argument to the theme function using plot dot title dot position. And then we can say plot. So now we see the title is moved over to the left edge of the plotting window rather than the plotting panel. This now allows us to use a bigger font. So let's go ahead back in and let's try size equals 28. And that then gives us a larger font and a more striking title. One other thing to notice in here is that in my title, I put a backslash n. And so if I remove that backslash n, I now see that my title truncates that indent, but it's actually really running off the right side of that figure. That backslash n that I inserted after intent is a line break. And so that is what's called a meta character to indicate a line break. So again, if I remove that and put that before the intent, you'll now see that I have a line break after vaccination. And so I would rather it be after intent, because, you know, it's kind of like a hierarchy to the title, right? So if I say COVID-19 vaccination intent, and then the punchline is decreasing globally, kind of like having that line break there after the intent. The next thing I want to talk about is the caption. And so you'll see what looks like HTML in the caption. And you're right, that is HTML. And so let me go ahead for now and remove that HTML. And now what you see is you have the caption, but instead of not seeing the right side of the caption, like the title right when I moved that line break, the title ran off the right side, here the caption's running off the left side for some reason. And that's actually because the caption is right justified rather than left justified. You'll recall that when I put in that caption tag as a placeholder, it was justified at the right edge of the plotting window. We can again change that by using element text. So we can say plot dot caption equals element text. And here what we want to use is h just equals zero. And so zero is left justified, one is right justified and point five is centered. And so now we see that that caption instead of being right justified is left justified. But again, the position is to the panel. So how do we get it to be positioned relative to the plot? Well, yeah, we go back and we can use plot caption position. And we'll say plot. So now our caption is left justified to the plotting window, like our title is, and things look to be in place. What I'd like to now show you is how we can change the font face. And here again, we can come back up to our plot caption. And we could say face equals bold. And so that makes our font face bold, we can also say italic. And so now we have italicized font, you might recall from the makeover version that our caption actually had two lines. Well, we saw how we can make two lines using that line break right with the backslash and could put that in after countries and we'd have our two lines. But what we have going on here is a little bit different that the first line is italicized, and the second line is a normal font. How do we do that? Well, that gets a little bit more challenging, but really cool and really powerful. This is where the HTML comes in. So again, I can come up here into my caption equals and in the quotes I can insert HTML tags. And so I can put in an anchor for italics. So I'll put that before the base. And then after the countries, I can do the forward slash I to then go ahead and indicate where the italization should end. I can then also put in a br, which is HTML for a line break. The other thing I need to do is remove the face equals italic from the plot caption. And now what I see is what we started with just a couple moments ago, where we have the HTML code joined to our text, which isn't exactly what I want. I want that HTML to be rendered. How do we do that? Well, again, it's really cool. Here in plot caption, we could use element markdown, rather than element text, and all the arguments will be the same, except it will now ggplot will now use markdown, which includes HTML to render the figure. But there's a hitch and that to use element markdown, we need to load the library ggtext. So I'll come way back up here to the top, and I'll do library ggtext, make sure I've got that loaded. And now what I see is I've got this line break caused by that br tag. And I have italization of my first line, and my second line then is a normal font. I could also make that second line bolded, perhaps, right? So down here, I could do be and add the tag, and then add the tag to close it. And so now I've got an italicized first line, and a bolded second line. The other thing that you perhaps noticed was that in the makeover version over here, the font is a gray, more subdued color, right? It kind of fades into the background a little bit more than all the other text. To do that, we of course could come into here in our element markdown, we could do color equals gray. So now we have that grade font, I think I'm going to go ahead and remove those bold tags from the source ipsos because it doesn't make sense to bold something, and then to kind of gray it out. So again, I can modify that by removing those HTML tags. And it's really nice, again, to be able to use these HTML tags within our text to stylize the appearance of the font. So you'll recall in our caption, we use this br HTML tag to put the two lines on separate lines. But up here in the title, we used a backslash n. We could of course make that a br to put in a line break. And then for our title, instead of element text, we could do element markdown. So now we see that we still have that line break. Again, in this case, it's there because we had the HTML br tag. And it's taking that whole title and wrapping it across two lines. One of the challenges with inserting line breaks like we did into the title where, you know, we're trying to take that really long string and then manually snake it across our figure as a title is that if I change the size of the font or change my font or change the font face, then where that line break should go is going to change. And so that's really kind of tedious to have to mess around with. But if we're using element text or element markdown, that's really the only option. But thankfully, ggtext as a package has another really useful function called element text box simple that will do that snaking for us. It also recognizes any kind of markdown attributes that we put into the title. So I'm going to go ahead and remove this line break and come down to element title and do element text box simple. We now see that we have our title automatically snaked across two lines of the figure. If I were to come back to my font and say make this 40, we now see that our title goes across four lines and that it automatically figured that out for me without me having to put in those line breaks. Of course, that's not what I want. Again, the element text box simple function recognizes markdown and HTML. So I can do some really cool things. So I could go ahead and put in a span tag around decreasing to make decreasing look red. So I could do span style equals color and then in colon and then put my hexadecimal so ff 000 and close that out and then after decreasing close out the span. And so now I see decreasing is red. And if I wanted it to be blue, I could very easily move those f's to the end of the hexadecimal and make it blue. Again, I find it to be just really powerful to be able to insert HTML into my title or any of this text that we have. It's just really empowering to be able to raise the styling just up many, many orders of magnitude. It's really cool. Of course, I don't need that in there. I'm just showing off what's possible with this element text box simple. So I'll go ahead and remove that span for now. So two things stick out to me about the title that we can go ahead and modify with our theme function is that the lines seem a little bit more separated than they were with element markdown or element text. So let's go ahead and change the line height. And again, we can come back to element text box simple and I'll add line height. Let's let's try one and see where we're at. So that's line height equals one. It does make it a bit more compact. If I did line height say 0.1, just go for the extreme. It's not going to get those lines to overlap, but basically kind of removes any of the space between those two lines. And if I were to make line height two, then I have basically double spaced my title, which is not what I want. So I'm going to go back to one. And I think that's got pretty good separation. So again, the line height argument allows you to adjust the height of the line. So if you have multiple lines for your title or for your caption or, you know, even your y axis label, you can use line height equals something to adjust the spacing between those two lines. There's one final thing I want to take on today for this figure. And that again is looking at the title and noticing that the bottom of the G's and the Y basically touch the upper border of the plotting window, which, you know, eventually I'm probably going to turn the background to be white. And so I won't see that so much. But of course, this gives us another opportunity to talk about another argument in these element text, text box and element text box simple and element markdown functions. Again, all of these functions have basically the same arguments. I can give argument margin and assign that the margin function. And so the arguments for margin are trbl. So I think trbl. That's what it says in the documentation. And so it's top right, bottom left. I guess the way you're looking at it is top right, bottom left. Anyway, you can then put in a number next to that argument. And it'll be that many points away from what you're looking at. So I want to adjust that bottom margin. So I'll do B equals 10. And now we see we have more breathing room between the title and the top of that margin. Of course, we can do that with any of the titles or any of the text that we're modifying with our theme function. Again, I could come down to my plot caption. Again, element markdown element text box simple element text have all the same arguments and for this application. And again, I could do margin. And I could then do margin, T for the top margin, and I'll say 10 there, that then gives a little bit of breathing room between the caption in the bottom and the rest of the plot and giving it again, a fair amount of space. And I think this looks pretty attractive, at least for the text, we have a nice framework for our figure. Now the background, the colors, all that other stuff, that still needs some work. And we'll talk about that in another episode. But hopefully you can see from what we've done here, is that all of the text that we see can be easily modified using the theme function using a few nice commands. So element text, element markdown, element text box simple, element text box. So I use element text box simple more than element text box, I think element text box without the simple has a little bit more power to it. But sometimes that's just like, I don't know, I like the defaults of element text box simple better than the defaults for element text box. Anyway, go ahead and play with these helper functions to the theme function. Know that you can find the titles and all the stuff that you want to modify in the theme function. Again, by going to the help, looking at the theme function, and look through the different arguments of the theme function. And anywhere that you'll see something like plot title here, right, plot subtitle, sub plot caption, plot tag, strip text, all of the things that use text, or where you see on the right that it uses the function element text, you now know how to use element text to modify that argument, that aspect of the figure, right, that here we have axis text, axis text, x, y, x, x top top bottom left, right, right, you can modify all those using element text. And so now you're empowered to do that. So I really hope that you got something out of this. And if you did, wouldn't you please give me a thumbs up down below. I would love it if you took what we talked about today in modifying text and the appearance and the theming of that text and apply it to your own figures. Let me know down below if you run into any problems, or if there's any kind of styling to the text that you feel like I didn't quite cover, or that you would like to see me do a little bit more in depth in another episode, and I'll I'll be sure to add that. All right, keep practicing with all this and we'll see you for another episode of Code Club.