 Hey friends, in this episode of Code Club, we're going to take this slope plot and we're going to convert it into this slope plot. Notice the difference? Yeah, we moved all that text for those 15 different countries from the right hand margin into the actual body of the plot, so that we can more directly label each of the 15 lines. Of course, this is not a perfect solution because there's a lot of data there. But along the way, as we go about inserting the text next to those lines, we'll learn a lot about different geomes that we can use from ggplot2 as well as other packages to put text within the plotting window. So let's fire up our studio and you can grab the code that I'm starting with down below in a link to a blog post that's for today's episode. There you will also find the data that I'm using at the very bottom of that blog post. You can take that data if you haven't already and plopped it into a file called August October underscore 2020 CSV. Everything should work. Let's hope. Also in there, I'll put the final code that we finish with. But as you'll find over the course of today's episode, we'll do a fair amount of meandering. So it really would pay to follow along as best you can. So you can learn about these great options we have for placing text within the the figure. So here is the slope plot that we've been working with over the past couple of episodes. It still looks a bit garish with those 15 different colors for our 15 different countries. But what we see in the slope plot is declining intent to receive the COVID-19 vaccine. But it's hard to see what what line corresponds to each country because again there's so many different colors. And the legend is so far off to the right relative to the position of the lines. We're going to talk about that in today's episode and one strategy that we might use to help solve that problem. But before I dig into that, I want to come back and review something we discussed a couple episodes ago, which was how to format the text associated with the theme of our figure, the text that's not associated with the data. So things like our title, our caption, our y axis, and our legend text. So one of the first things that I'm going to do is come back to my R script. And you'll see that we loaded two fonts from Google font. So the first one's patch one. And that's what we're using as the title that nice bold, attractive font. We also loaded a font that we didn't use, but we used in a previous version of that dumbbell chart, which was Roboto. Roboto looks a lot like Ariel, which is the default font. So I'm going to change this and use a font called Montserrat. And I'm going to name the font family then Montserrat, make sure I've got that loaded and then go ahead and run that show text auto to make sure that that font family as well as patch one one are visible to ggplot. Now what I'm going to do is to modify the appearance of all of the text in the figure, I can add an argument which I'll call text. And so this text argument takes what kind of function do you think element text, right? So you could give it element text, element markdown, whatever you want, but I'm going to use element text. And so I will give element text the family argument to again change that family to be Montserrat. And that needs to be in quotes, because that is the name of a font family. And we see that that syntax is very similar to what we had down here for the plot title. Of course, I also needed a comma there. And we see that the font has changed in all of the textual aspects of our figure. You'll notice the title didn't change. And that's because ggplot's theme works on a hierarchy. So text you can think of as being at the top level, everything inherits from the top level. So when you get the plot title, it would use Montserrat, except we said to use family, patch one, and that's why the title is patch one, and everything else is that Montserrat font. One other thing that I would like to change is the line height of my caption. Do you remember how we could do that? Yeah, if we come back to plot caption, we see that we used element markdown, which again is very similar to what we see for element text as well as element text box simple. And the argument that we can use, we see that we used up here for plot title, which is line height. And so I don't want to and wanted to be double spaced, I do want a little bit of separation between those two lines. So maybe what I'll put in here is 1.5. And now I can see a little bit of separation between this first line of the caption and the second line of the caption. And I think that looks a little bit more attractive than having those two lines compressed together. The third thing that I want to review with you is how we can change the color of the font in our labels. So looking at our y axis, this is a black font, which to me is pretty bold. Obviously, it's not as bold as the title, but it kind of stands out. And I don't really want the y axis to stand out so much. I want I want the data to really stand out. And then if somebody wants to go look at the value on the y axis, they can see that I want their eyes to be drawn to the title, and then to those slopes. So we're going to go ahead and change the color of that font. And we don't have anything here for the axis arguments already. So we'll go ahead and add that. And we'll do axis dot title dot why because I'm going to change something about the title on the y axis. And here again, we can do element text. And I can then use the color argument to change the color of that y axis title. And I'm going to go back to my hexadecimal and I'm going to do pound and I'll put in six eights here. That gives a nice medium gray color. And so now I see that my y axis title is gray. I'm especially you can kind of see that relative to the color of the numbers. I also want those numbers to be that same color gray. So I'll add axis dot text dot y element text. And I'll again do color equals that 88888. So now our y axis labels and title are that more subdued gray color. And I think that does a pretty nice job of allowing us to really focus on what's going on in the main body of the figure. Yeah, you know, we could also change the dates here. But I want it to be clear that the dates that we're looking at are August and October of 2020. Again, all of these are matter of personal preference and your own personal sense of style. And so I'd encourage you to play with this to get kind of the appearance that you want. Okay, enough review. Let's talk about moving these country names inside of our plotting window. So I'm going to start by using geom text to move these country names to be right next to the lines that they represent. I can do that by coming back up here to my pipeline for where I'm building the plot. And I'll do geom text. And so geom text takes x and y and label and a number of other aesthetics. But those are the most important ones for us right now. I will go through some of those later. And for my label, to set my label, I want that to be the country name. And so I can do AES label equals country, because I want the label to be the country name. So oh my gosh, we have a lot going on here, right? We have the country names right on the points that are at the, you know, the August and the October of the data. We also see that we still have this legend over here. One thing I want to point out about the legend is that it's adding the text, a character for the text. So A, to indicate that the A, the red A on top of the red line is India, as if we couldn't tell because we have India written right there. So let's go ahead and get rid of this legend, because one of the purposes of putting the text on the line, or at least next to the line, is that we don't need the legend then, because the data, the text is going to be right on top of the data. Again, to do that, to remove that legend, we can come back up here into our two geoms and say show dot legend equals false. And we can then do the same thing for our geom text. And there we go, we've gotten rid of the legend. And now we have our country names on top of the point that is represented for the August and October, you know, willingness to receive the vaccination value. So obviously, this is a hot mess. It's very busy. There's a lot going on here. And so what I want to focus on is how we can use geom text and a couple of other related functions to help clean this up. The first thing that I want to try, though, is to alternate what side the country name is written on, right? So I don't really need China on both sides. I don't need India on both sides. It just kind of, you know, makes it more busy than it really needs to be. I think we're already going to have a few problems because I think right here, we have USA, Spain and Italy, all like right on top of each other. So if I can maybe leave Spain over on the right and put, I don't know, is that Italy there or whatever, right? If I can kind of free up some space on the on the left and right, that'll make it a little bit more attractive and easier to read. So to get those country names to alternate left and right, I'm going to come back up and we'll review what is in our data data frame. The data data frame has the country as well as the month and the percent, right? And so that way, then on the x axis, we can put the month, the y axis, the percent, we can group by the country and we can color by the country. I'm going to create another data frame that will be used for plotting the name of the country. And so we'll say data, and we'll pipe that into a pivot wider. So we just made things longer back up here on line 21. Here, we're going to pivot wider to make it wide again. And I'll show you why, but let's do this first. So we'll do pivot wider. And I will then say names from and I'll say month and values from and I'll say percent. And so now what I see of course is I've got my countries and my two months, which is good. And what I can then do is I'm going to mutate to create another column. And the column is going to indicate what row each of the countries are in, because I can then use that value to indicate whether it's an odd or even column and whether or not I want that to be on the left or the right side. So I'll say n row equals one colon and parentheses. So that n parentheses function tells you the number of rows in the data frame. And now we can see that we have n row. So now I want to indicate the actual month that I want to report back for each of those countries. And to do that, we'll then say month equals, and I will do an if else as the function. And so if else, and row, and I'm going to use a special function called the the modulus function, so it's 2% signs to equals equals zero. So that's actually even. So the modulus function tells you the remainder. So if I do five modulus two, the remainder should be one, right? Good. If I did five modulus three, the remainder should be two. Good, right? So if I do five modulus two, and I get back one that tells me that five is odd, if I do four modulus two, four modulus two, I get back zero because that's an even number. If the row is even, then I want to turn August tick 20. And then if it's odd, I'm going to then give it to the October. So that will then be October tick 20. And we have these poorly formatted month names, because we use labels up ahead here. If you remember the last episode where we talked about factors, we now see when we look at this that we then get our country, and then the month that we want. We also now want to get back the percent that we want, right? And so we can repeat this to have a column percent. And I'll grab this same statement, bring it down. So if it's even, I actually want the value that's in the August 20 column. And I can get that by using those back ticks to indicate the column name. I'll do the same thing here for October. And now what I see is I've got new columns month and percent to indicate what position on the x axis I want and what position on the y axis I want. And I can then do a select to get back the country and the month and the percent. And that's exactly what I need to then place the country names next to the data. So I will go ahead and plot this as country labels. And so now I've got that data frame loaded. And then I can come back into my geom text. And I can say data equals country labels. I then have my AES for my label equaling country. I still have these AES values up here for month and percent. So that should work very good. So now we have our country names alternating left and right. Again, that cleans things up a little bit. And I think we'll hopefully help to make this plot a bit more attractive than it is. One thing that we notice, of course, is that our country name is smack dab on the middle of the point. It's not exactly what we want. What I'd rather have is the points on the left to be right justified so that that A in China is right next to the point for August 20. And the points on the right side to be left justified so that I in India, the first I is right next to the October 2020 point. We can alter the alignment within geom text by using a special argument called age just. We've seen this before when we're formatting the positioning of the caption at the bottom. Recall that's naturally right justified. We made it left justified. And so we can make things left justified by using a zero. And so we see now that all of our country names are left justified. Alternatively, instead of that zero, we could also put in left. And that gets us, again, left justified, if you can't quite remember the numbers, or you could use right to get things to be right justified. Instead of right, you could also use one. And you could also center things like the default by writing center, or you could write 0.5. So again, the age just values are zero for left justified, one for right justified, and 0.5 for center, or you could use left, center and right for that justification. So there's also two other special keywords that you can use with age just. And so one is inward formats things so that the things on the left are left justified and things on the right are right justified. We could also use outward to make things outward so that the things on the left are right justified and things on the right are left justified. And this is what I want. Very good. And so again, this is pretty nice as a way of cleaning up the text a bit. We still see a bit of problem with say like USA and Italy writing on top of each other. You know, it's really hard to read that Canada and Japan are not so great either or Australia, Mexico and Germany, Africa. It's a lot better than what we had. But it's still not as desirable as we might like something we could do to help clean this up a little bit would be to make use of two arguments in geome text, which are nudge x and nudge y. I'm going to illustrate this for nudge y. So I'll add a column for nudge. And I'm going to create this nudge column value using case when. And so case when I'll then do country equals equals USA. And then the amount I want to nudge it will be three units. And for country equals equals Italy, the amount I want to nudge it will be minus three units. And then for everything else, so I'll say true, I want a tilde to be zero. So the nudge is nothing. Alternatively, instead of changing the nudge value, we could change the percent value to kind of bump things a little bit. But I want to demonstrate how to use those net nudge arguments. And so now if we look at country labels, we see that I didn't export using select the nudge column. So it's always good that we check those things here. So I'll add that to nudge. Now, if I look at country labels, I see that I've got that nudge value. And I can then come back down to my geome text, and I'll add nudge y equals country labels, dollar sign, nudge. And so now I see, well, that's a pretty big nudge, I probably didn't need to nudge it three, maybe I could do it by one. So I'll come back up and modify that. So that looks a lot better. It's not perfect, of course, but, you know, it looks a lot better than them being on top of each other. We could, of course, come back through and do the same type of thing for Canada and Japan, Australia, Mexico, Germany, and South Africa to give it a little bit more separation so that the things aren't writing on top of each other. I think I'll save that for you to work on as an exercise. Okay. So again, that is how we can use geome text and some of the arguments, specifically thinking about that nudge underscore x nudge underscore y we use nudge underscore y, but you could use also nudge underscore x to move things left and right. As you can see this approach to labeling the lines using the information from a legend is quite a bit different than what we typically do in the scientific literature. It's perhaps more akin to what you would see in the popular media in the popular press. What do you think? Do you think there's a place for this type of labeling in the scientific literature? I kind of think there is. One of the things I really like is that it puts the label right next to the data so that my audience member doesn't have to be scanning back and forth from the data to the legend to figure out what goes with what. I think this is pretty nice. Of course, we still have some overlap of our labels and we have all these different colors, which makes it kind of hard to see what's going on. So again, this isn't a perfect figure. At the same time, if you've subscribed to this channel and you've clicked on the bell icon, why haven't you already? Come on. You will find out that in the next episode that I have ready for you, we're going to use the Gigi repel package to go ahead and automatically separate those labels that they don't overlap on each other. And we can make it a little bit more clear what line corresponds to each of the countries. Keep practicing with this material. See if you can't implement it with your own data. Be sure that you've subscribed to the newsletter that I have. Each week I put out a newsletter. It typically kind of tracks along with these videos. So if you want extra practice problems that correspond to these videos, you need to check out that newsletter, which you can find if you go to riffimonis.org. The bottom of the page, there's a form that you can fill out. The newsletter is free. It's not spammy. And I think you'll really get a lot out of it. Anyway, practice with those and we'll see you next time for another episode of Code Club.