 I have a slope plot with 15 lines for 15 countries indicating the people in those countries willingness to receive the COVID-19 vaccine when they were asked about this in August and October of 2020 last year. Well, as you can see looking at this figure, the labels for those countries kind of overlap with each other. We've seen how we can use geome text to separate them a little bit using various nudge arguments and different position arguments. What I want to do in today's episode is take our labeling up a notch. We're going to go ahead and use geome label, which will put a nice border and background around each of those country names, but then we'll also use functions from the ggrepel package that will allow us to automatically separate those names so that they don't overlap with each other and give us a really nice look. And I think we'll get us really close to having a slope plot that we're not too afraid to show out in public. Hey folks, I'm patch loss and this is code club. I don't know if you know this or not, but I have a free weekly newsletter that you can subscribe to by going to rifimonis.org. The bottom of the page there. There's a little sign up sheet. I give me your name and your email address and every week, every Friday afternoon or so, I will send you some practice problems and some food for thought. The content generally, you know, parallels what I talked about in these different episodes. So if you want some extra practice, be sure that you subscribe to that newsletter. Now, let's go ahead over to our studio and get going with today's content. Here's the code that generates the slope plot that shows those vaccine attitudes for our 15 different countries. We generated this in the last episode of code club. So if you want up across the top here, I'll put a link to that episode. You can go back and check it out. But you know, why don't you watch the whole playlist? And there's quite a few videos in this series. And I think you'll find there's a really a lot of great content in there. Also, if you go down below this video, there's a description section. And there you will find a link to a blog post for today's episode, where you can get links to the GitHub repository for this series of episodes, where you can get the starting code and the ending code along with the data. So you know what, you can play along with me. Anyway, like I said, we have this code here builds out the data creates our labels. And what we previously used was GM text. And so GM text puts this nice country names next to each of the lines. And we did some styling up here with this country labels data frame, so that our country names are alternating left and right, you know, you could go and you could clean that up a little bit more if you wanted. But I think after this episode, you'll realize, I think it looks okay. So an alternative to GM text would be to use GM label. What we get out then is basically the same thing that we had with GM text, except now we have a white rectangle with a border behind each of the names of our countries, the border corresponds to the same color as our country name. And again, it gives us a little bit of a nice styling. However, because there is a opaque background, that white background, when we come down here and see like Italy and USA, they kind of overlap with each other, as does Japan and Canada, and some of these other countries here. So that's what we're going to deal with in today's episode, we're going to learn more about how we can modify the appearance of these labels for GM label. And then we'll go in and we'll use the functions from the GG repel package to see how we can get those labels to separate a little bit nicer. And so that our presentation looks a bit more clean. First of all, if I want to change the fill color of my label, I could come back into here. And I could either set the the fill color to be related to the country or some other variable up here in a yes, or I could do fill equals. So let's do a gray color, we'll do f a f a f a. And so that gives us a gray fill color for our label. That's not what I want. But you know, you get the idea. Alternatively, you could make it transparent by using an a as our fill. And this gives us a transparent background. Again, it looks gray, but that's because the background is gray, you can actually see the grid line here running through the Australia label. I'm going to go ahead and leave it with the white color that is the default. So I'll remove that line. There are a few other things that we can change about the appearance of our label. So one being the padding around the name, right? So we've got this border around the name India in this case. And so there's a spacing there, we can modify that again, coming up to geom label, and we can do label dot padding equals, and we can then give it a function unit to unit, we give it a value and a unit. And so I could say maybe let's give it two millimeters. And so I'll do two comma millimeter. And so that I think made the margin a little bit bigger. If we go down to one millimeter, it brings it in a bit more. And let's let's go with like say 0.75 or 0.8. So again, modifying that label padding value can adjust the amount of padding between the name and the border. And we achieve that by using this unit function, where we give it a value as well as the unit that we're looking at, we can also change the thickness of the line for the border by doing label dot size. And again, like we saw up above with the padding here, we give it the unit function. And so let's do one millimeter. And so this gives us a pretty thick border, probably a lot thicker than we really want. Let's go ahead and shrink that down a bit. Let's do say 0.2 millimeters. So that gives us a nice hairline look, the actual default is a quarter of a millimeter. So 0.25 millimeters. So we're not that far off. So the next thing that I want to change is the appearance of the corners on those labels. And to change that we can come back up to GM label, and we can do label dot R equals unit again. And if I do zero millimeter, this gives me right angle corners on my boxes. Of course, if I change that zero to another value, let's like, let's say one, I get those rounded corners to the box. So I'm going to go for something perhaps not so round and do 0.5. And I think that looks a little bit more subdued of a corner. And I like the way that looks. So again, within GG plant to we have GM text, which gives you the text without any kind of background to it. And then you have GM label, which gives you this background rectangle, and all these different things that we can modify. The font that we have in these labels is actually the aerial font family. If I want it to be Montserrat, why don't we do that? And we can come up here into my GM label again. And then I can do family equals Montserrat. And so that then gives me my Montserrat font family for the actual country names here. And that matches the rest of the serif font or sans serif font that I have going on in my figure. Very good, right? So we still have this problem that we have overlapping country names. And that's not what we want. What we can use instead is a special package from our called GG repel that works very well with GG plot to generated figures. So let's go ahead and use that and see just a basic introduction to how we can use GG repel to make these country names look perhaps a little bit more legible and not overlap so much. So I'm going to go ahead and load GG repel. And you want to make sure that you have that installed on your system so that you can use GG repel. I'm also going to go ahead and remove my nudge stuff for now. And I'll go ahead and comment this out so that when I post it to the blog, you're able to find it and you won't have to ask what happened to it. And then I also need to remove the nudge down here. So I'll go ahead and comment that and put this show legend on its own line. Let me just make sure everything works. So let's come back up to the GM label function. And what we'll find is that it's really convenient to use GG repel because I can take GM text and GM label and add underscore repel. So we now see that with that repel add on to GM label that our labels no longer overlap with each other. But we leave a little bit of the connection between our label and the actual data that they're supposed to represent. There's a few different arguments that we can play with to make it a little bit more clear. One thing that we can do is we can come back up into GM label repel one argument that we might add would be min dot segment length and let's do zero. And that adds a little segment to each of the labels pointing to the line that that represents. So that's pretty nice. And you can kind of call or trim out which ones you want to represent by increasing that segment length. If I use point two five instead, I see that the UK no longer has a line connecting it to the line. So again, you can kind of fiddle with the length of that minimum length of the segment to adjust where you get a line connecting the label to the actual point. Now we see that our labels no longer overlap with each other, which is great. But they do overlap with our lines for a lot of the countries. I would rather keep those labels on the outside of the plot outside of the slope chart. So we'll talk about how we can do that. One thing I want to call your attention to is as we've been playing around with these different values for the min segment length, you may have noticed that the labels move around a little bit. And that's because the placement of the labels is a little bit random and that it's using a random number generator to help kind of bump, if you will, the labels around so that they don't overlap with each other that by default the algorithm is trying to move things in both the x and y directions, we can kind of fix it to only move it in the x direction or only the y direction using the direction argument. So if I come back up to geom label repel and do direction equals x, we see that those labels then are moving along the x axis, which isn't great. And we see a little bit of overlap between some of those country names. But if we come back up and did y, so I think this direction y is kind of along the lines of what we want, we want to be able to keep things in the left and right margins and move them up and down to kind of get separation between our country labels. It appears to kind of be respecting the outward position, although that A in the China you can see is right on the grid line for that August and the IN right between IN for India is falling right on the grid line for the October 20. So I think if I add a nudge, I'll get things again pushed further apart. And then that direction y will give us a look that is a lot more of what we're looking for. So I will come back up here. So I'm going to add another if l statement to create a variable column that I'll call nudge x. And I'll grab this same if l statement. And so again, if it's August, then I want to bump it to the left and let's do 0.2. I'm not sure that that's the actual value I want. And then we'll do 0.2 to the right. And then we'll also want to export that nudge x. Then I can come down to my genome label repel. And I will then do nudge x equals country labels, dollar sign nudge x. And that gets things, as I said, further away from the points, perhaps a little bit too far. So instead of 0.2, let's try 0.05 on either direction. So I'm pretty happy with the way this looks now. It's so much better than what it originally did when we use geome text with all 15 labels on both sides. One little thing that I might go in and change is the minimum segment length. I don't know that we need that little tick there for China or India. I think that's kind of obvious. So I'm going to come back up here again to min segment length. And let's go back up to the default, which I think was 0.5. And so now we've removed a fair amount of those lines, which I think just kind of like clutter up the plot a little bit. So I think this looks really good. One thing I want to show you is again, the parallels between geome label and geome label repel, which is what we've been doing in geome text and geome text repel. I'm not also totally a fan of those labels with the background. So it's good to see what things look like. So if we do text, so this of course, then removes that label, the background rectangle from each of the country names. Again, it remains to be seen whether I prefer having that background or not. I'm one that doesn't like a lot of clutter in my figures. So I would prefer perhaps to not have it. One thing you notice that we lost some of those lines connecting the name to the point. That's again, because we don't have that padding around the country name, adding to the distance between the name, the label, and that point. So on the whole, I'm pretty happy with the way this looks. And I think it's, like I said, so much better than where we started. I'm pleasantly surprised. Anyway, I hope you found this discussion of geome text, geome label, and then the repel versions of the same functions useful. I really encourage you to think about how you could use this with your own data. Something that I've seen people do is that they might have, you know, kind of gene expression data where they have like thousands of points, and they want to highlight one point. Think about how you could use geome repel with that data to highlight one or two or three points, so that your labels aren't right on top of the data or the labels themselves aren't on top of each other. I think that would be a really useful application of these functions to those types of data. The jury is still out on this actual application. But I think for what we're trying to do with the slope chart, I think it is effective to use that those geomes with the repel, so that the labels aren't right on top of each other. So obviously, this is not the final version of the figure, we still have a fair amount of work to do. As we go through, I'm going to try to share one different aspect in each episode, so that you know when those episodes come, please make sure that you've subscribed and you've clicked the thumbs up icon so the algorithm knows you love what you're getting and I feel better about myself for putting this out here. Anyway, keep practicing with this and we'll see you next time for another episode of Code Club.