 Looking at figures in the scientific literature, one of the things I get the sense of is that people really don't know what to do with the text that appears in a figure. Whether that's axis labels or the legend or annotations or the title of the figure, people really just seem kind of at a loss of how to work with that. And why do I get that sense? Well, as I've said in the past episodes, we still see those blasted underscores. We don't see proper italization. And I think sometimes people don't realize what tools are out there to make that happen. We've used a tool to do those things in the past using the ggtext package and we use the element markdown function within the theme function, right? And so that's again part of that ggtext package. Well, there's a lot more we can do with ggtext, and that's what we're going to dig into in today's episode of Code Club. Over the last several episodes, I've really enjoyed working with the element markdown function as part of the theme function. Element markdown comes to us from the ggtext package, which is developed by Klaus Wilka. I've talked about using element markdown in the past, and that allows us to italicize our bacterial taxonyms by using markdown, which is a very simple markup language, where we could put stars on either side of a word to make it italicized or to make it bold and so forth. We can also use it to add HTML and CSS styling to titles. So we could perhaps, you know, highlight an individual word, make it bold and make it a certain color. The ggtext package also has other functions that allow you to create annotation text boxes. So you can put text boxes at different places in a figure. Now, of course, ggplot2 comes with geomtext and geomlabel that do very much the same thing. But the cool thing about doing this with a function called geomrichtext from ggtext, getting all my ggs confused, so geom text, geomrichtext, can't get them straight. So geomrichtext is that we can add markdown to those labels, so that, again, we can have all the power of styling that we have from that element markdown button data that we're plotting into our figure. So I've been thinking more about the ggtext package in light of the last episode. Now, in the last episode, what we did was to produce this LDA score plot, which, again, are the LDA scores coming from a LEFSA analysis that we ran in mother. If you want to go check that out, go see that other video after you finish this video. And so one of the things you'll notice about my version of the figure is that the bacterial taxonyms are on the y-axis, kind of in the traditional placement for a y-axis. One of the things I kind of thought was cool about the figure in the published paper was that they put the taxonyms right next to the bar. Now, I really am not a fan of this figure. I think there's issues with it. But at the same time, I thought it was kind of a cool question of in our, how could I put the taxonyms right next to the bars? Now, what we're going to do is we're going to use geomrichtext. I got it right that time. And we're going to use that and put it right next to the bar. And we'll see how we can do that. I'd also like to kind of think about what are the other ways that I can make my figure look more like theirs. Now, one of the things we did in the last episode is we immediately who rejected this red green color scheme, because it's problematic for people that are color deficient and that they can't differentiate between red and green. They see it as just gray, right? And there's a few other things that I might like to tweak about the figure as we go along. But the main thing that I want to do today is put those taxonyms right next to the bars and see what we can do otherwise to make our figure a little bit more attractive and perhaps look a little bit more like the original figure. Going over to RStudio, I've got my Schubert tax analysis dot R script, which you can also get down below in the description is a link to a blog post where you can get this code that I'm starting with so you can follow along. Also, if you want the data and to figure out how I've got things set up, I'll put a link to a video where I did just that. Anyway, looking at this, we've got our variety of packages from the tidyverse. I think all four of these are installed when you install the tidyverse. But again, we need to run the library function to load their functionality. We get the taxonomy information. This is where we do a variety of formatting. You'll notice in here already, I've got br, which is a HTML tag to put in a line break. We've got these stars to do italization and other things. So we might come back up to this to do some tweaking later in the episode. We get our metadata, the shared file. We then run everything through lefse. And we then have this code here to generate a LDA score plot comparing data from people that did not have diarrhea to people that did have diarrhea and were C difficile negative. I'm only going to be looking at this one comparison. Again, one of the problems with these LDA score plots is that I had three groups, which would mean that I would need to create three LDA score plots. And why bother when I can show all the data on a single plot with relative abundances, the variation and statistical significance. So again, I'm not a fan of the LDA score plot, but I think there's some things we can learn from it that perhaps will be useful to learn so that we can apply them in a future project. As we saw earlier, this is the figure that the code generates. It looks a lot like the left LDA score plots. Again, we change the colors to be consistent with the scheming for other figures we've been generating. One of the things that I also notice as I look at it right now, though, is that this legend, it kind of takes up like a third of the real estate in this figure. And that if I kind of put those up at the top or got rid of them somehow, then I'd have a lot more lateral space to kind of see more separation of the data and perhaps make it easier to see how similar are these four bars to each other or these three bars. You can just get more separation, more air to breathe. So we'll deal with that before we finish today's episode. But for the first thing that I'd like to do is I'd like to go ahead and put these taxenames right next to the bars. I want Enterococcus and then the blue line. And I'd like to have this like unclassified room in a Caucasian OT8, just to the right of this gray bar, right? And then the same thing for everything else. Back in our code, we can come back to our geome call. Again, I like to put all my geomes together. I can do geome text. And that will then plot text at the x and y position. I'll go ahead in here and put label equals taxon. So I'm going to have the x axis be the LDA, the y be the taxon, and then I want the label to be the taxon as well. One thing that I'm thinking about here is why don't we go ahead and put x to be zero. And so that will put things at the zero mark, and that'll be a good place for us to get going. So great. We now have our labels right smack dab in the middle of our figure, and they look pretty bad. And so that allows us to think about, well, maybe not geome text, but maybe we could put in here geome rich text. What we now have from geome rich text is the formatting is observed, right, from our markdown or HTML. We use the HTML to put in the line breaks, and we use the italization with the markdown. And so we see that one of the other things you perhaps notice kind of on this unclassified room in a car case, is that the label actually is a rectangle with a rounded black border around it and the fill for the background. So I'd like to turn off the fill, turn off the background. But before we get to that, let's not get too far ahead of ourselves. Let's go ahead and kind of move the labels, the blue labels to the left of the blue bars and the gray bars to the right of the gray bars to go ahead and move those labels to either side of the bars. I'm going to come back up here to my mutate. And I'm going to create a variable that I'll call label underscore x. And I'm going to borrow this if l statement from up here with the LDA. And so if the class is non diro control, those are the gray bars going off to the left. Then I want my x position, I'll make that say one. Otherwise, I'm going to make it minus one, right, so that again, I have the the labels for the gray on the right, the labels for the blue on the left. And so that's good. I'm down here then in geome rich text, I have x equals zero. So I'm overriding already, the AES that we have up here for LDA. And so I'll do AES x equals label underscore x. And here we can kind of see that the labels are dodged that they're not totally lined up at that zero position on the x axis. And so what's going on is that the labels are centered that H just is centered at that position. So what I'd like to do then is make my blue bars to be right justified at the coordinate I gave it, and my gray bars to be left justified, right? So how do we do that? Well, to get that justification right, I can add to my AES statement here in geome rich text, h just argument and the aesthetic and I'll create a variable then that I'll call label underscore h just. And now I need to define that and I'll do that very much the same way I did for label x. And again, that will be label h just. So again, if non-diarrheal control is the class, so those are the gray bars, then I want to be left justified. So I'll put a zero. And if it's non if it's the diarrheal control, so it's the blue, then I want it to be right justified. And so I'll make that positive one. Wonderful, it's still messy. But we can see we have our labels next to our bars. So the next thing I'd like to do is to go ahead and remove that fill color as well as the border. Coming back up to geome rich text, I can say fill equals na. And so that will give a blank fill to the background. That's better than white because white isn't empty, it's not transparent, whereas na is transparent. And then I can do label dot color equals na. And that will give us get rid of those black borders. So now we can see that we've gotten rid of the ornamentation, if you will, around those labels, and things look pretty decent. Let's go ahead now and get rid of those y axis labels and that y axis bar, and see if we can't work with things a little bit more cleanly. So I can then come down to my axis text dot y, which I previously had as element markdown. And I'm going to make that element blank. So they got rid of those labels on the y axis, I'm going to go ahead and get rid of that line as well as the tick marks. And I can do that with axis dot line dot y equals element blank, as well as axis dot ticks dot y equals element blank. So I'm happy with the way this looks for now. We did get rid of that y axis. A couple of things that kind of stand out that I'd like to kind of solve in this next iteration is maybe bringing in the labels a little bit closer to the bars, maybe making that font a little bit smaller. This unclassified room in a car case, it goes over three lines and just takes up a lot of space. So maybe you can shrink the font a little bit there. Also, I'm going to go ahead for now and kind of turn off the legend so that we can have the full width of that legend. We'll see it soon enough. Don't worry, we're not going to totally get rid of it. So what we can do is we can come back up to geom rich text. And I'm going to make it smaller, let's do size equals four. That's a guess, we'll see we'll see what that does. And then also for label x, I'm going to make this 0.25. And then minus 0.25 to again, bring it in a little bit closer. And then I'm going to come down to my theme actually and show you another way that we can remove the legend. So up above in the geome, you could do show dot legend equals false. Also here in theme, we could do legend dot position equals none. Good. So we have more lateral real estate by getting rid of that legend. We did bring the labels in a little bit closer to the bars. My font change didn't really do anything helpful. So I think what I'll do is I'll actually put this unclassified room in a car case on two lines, rather than three lines. And that should make things look a little bit better. So coming back up to the taxonomy data frame, where we create that up at the top, I'm going to go ahead and remove the break between the genus name and the OTU, but leave the break between unclassified and the name. And now we see we have a much more attractive figure where we've got our taxa names on one line. I think that looks okay. It's right next to the bar. Maybe it could even be a little bit closer, but it's good. But certainly this unclassified room in our car case is no longer going over three lines. And I'm pretty satisfied with the way that looks looking again at this figure that I'm trying to emulate. I noticed that the x axis here, there's no line on the x axis. So let's go ahead and turn that off. Also, let's go ahead and put on these vertical grid lines on the major marks to get rid of the x axis. We can copy this line here, we had axis line y, we can also do axis line x. And we can add grid lines. We can add grid lines a couple ways. So let me show you two lines, two ways. So first we can do geom v line for vertical line. And we can then say x intercept, like we did before, seek from minus six to six by two. And I can make that line type equals dotted. And I can do color equals dark gray. And make sure I have a plus at the end of this line. So I'll put this over a couple lines. So it doesn't scroll all the way off. So good, we see we've got those dotted vertical grid lines, as well as getting rid of that x axis line. I'm not sure what I think about removing the x axis line, kind of like having it there. It's just kind of, it supports everything right. But we'll go with that. And so you can see that the grid line is now on top of the bars. We could of course put that behind by putting geom call after geom v line, right. And so now we see the bar on top of the grid line. If we look at the original, you'll see that those grid lines are actually on top of the bars. If you zoom in close enough, you can kind of see it's there, it gets pixelated if you zoom in too far. So that was one way of doing the grid line. I'm actually going to turn this off now by commenting it out and show you another way that we can do it within the theme. And so what we could do would be panel dot grid dot major dot x equals element line. And again, I'm going to grab this stuff that I had up above for that geom v line with the line type dotted and color dark gray. And I'll throw that into here. And again, we get those grid lines. In this case, they are behind the bars. So with theme, how would we get those grid lines to be on top of the bars if you wanted to I kind of see a value in having the grid line on top of the bars? I don't know, we can go either way, right? So how do we do that? So to get those grid lines to be on top of the data, we can use the panel on top and then say true. And so the default is false. So the panel is not on top of the data. So what we now see is that the panel theming is on top of the data in all the GMs that we can't see it here, right? And so again, what's happening is that this is a panel window. And the color of the panel window is actually white, right? So remember before I talked about fill equals na. Well, this is a case where we had fill equals white. What I'd like to do is make my panel dot background to be an element rectangle. And then make that fill equal to na. And that way then it'll still be on top of the data. And so as grid lines will go across our bars, but because the fill will be transparent, we'll still see the data. And I'm getting an error that it's wrecked not rectangle. And so we sure enough see now our grid lines on top of the data and on top of the GM. And this is much more in keeping with what they had in the original figure. One thing I want to point out to we talked about this before with heat maps, but I kind of see it here, is that again, the color of my grid lines is the same across the whole plot. And to me, over here, this point, the grid lines that across the gray bars looks a lot darker than the point crossing the blue lines, right? So again, our eyes do funny things, right? When we're trying to perceive what's going on with these visuals and with colors and the context that we see the colors. Okay, so one other thing that we did was we removed the legend. So let's go ahead and put the legend on top, because if we put the legend on top, then we'll still get that full lateral space. So we'll go top. And so now we see that we have the legend on top. And that looks pretty nice. That's also kind of the way that that lefts a figure from the paper looked. Maybe we'd like to move it over to the left. So it's not centered, or maybe we'd want it to be on the right. We can move the legend around by doing legend.justification. And I'll say left. And we see that it's justified over the left. And as you might expect, if I say right, and now it's justified over to the right. I think for a paper, I would prefer it to be to the left. I kind of like that look a little bit better. I think I prefer it over to the left. I'm not quite sure why, but it looks pretty good. You know, maybe I'd even want it on the bottom. Well, how about that? Let's do bottom. Yeah, that doesn't look so bad. I guess the downside there then is that you have to look to the bottom of the figure and then look up, right? At the same time, if we put the legend on the right, we're still looking to the right and then going back. So anyway, what I would really like to do though is get rid of the legend and give this a nice title. So I'm going to go ahead and turn off my legend and I'll say none. And we will then come back up to labs. And I'll do title equals these OTS are the strongest predictors of people who are healthy or who have diarrhea. Okay. And so let's go ahead and do that. And we'll get a title. And so what we see is that the title now runs off the right side of the screen. We can actually use another function from GG text to get this to wrap and to use our nice formatting. So I can do plot dot title equals element text box simple. And that should then get me the wrapping. And so that title wraps very nicely. A couple things I'd like to do here is I'd like to go ahead and make people who are healthy or healthy be dark gray and diarrhea be blue. We can do that by inserting HTML into our title. I can also put this across a couple lines. So we'll go ahead and do span and then style equals and then we'll do color equal or colon. And I'll do some hexadecimal here. So we'll do like 88888. So that should be a good dark gray. And I'm noticing that I'm getting some weird syntax text highlighting here coloring. And that's because I've got double quotes inside of double quotes. So I need a double quotes on the outside and single quotes on the inside. So let me go ahead and clean that up. So I'll put my my CSS in single quotes. And then I need a closing anchor for that. And then we will then I'll go ahead and put diary on the next line with span style equals single quote color. And then blue. So it goes red green blue. So it'll be 000ff for blue. I think we'll see. And then we'll close this out with the backslash span. Great. So now we've got the gray for healthy and the blue for diarrhea. I'd like to this to be kind of bolder and perhaps a little bit bigger. It almost seems like the same font size as my label. So I want this to stand out a little bit more. So down here in element text box simple, I'm going to add some arguments. So let's do size equals 20. Let's do face equals bold. Very cool. Now we have this bolded and the text is a bit bigger. The lines look a little bit funky just because I think of where the natural line breaks are with the words that are in the string. I'd like to go ahead and put a margin in to separate the title from the data a little bit. It just it seems a little bit close. So we can do that with a margin argument in element text box simple. So we'll do margin margin. And then we give it it's that goes top right bottom left. I guess it's the opposite because you're watching me whatever. So we'll do 0050. So good. So we get a little bit of separation now between the title and the rest of the text. I think what I'll do is maybe make that size a little bit smaller to see if that kind of fixes those weird line breaks. Good. So that gives us a little bit better formatting of our text on the title. I'm pretty happy with the way this looks. Again, I think we've done a pretty good job of emulating what the original figure had from the left of paper with those grid lines and the axis labels. Again, I'm not a fan of this figure of this way of representing this bit of information. What I wanted to do was learn more about gg text. And so hopefully you can see the value in that that even if we're making a figure that we don't really like, we can learn about our package like gg text by kind of saying, well, you know, I don't like the overall look of the figure. But I do like this one thing they did by putting the text right next to the data. How would we go about doing that? And so I think this was pretty useful for doing that. I'm not totally sure about the grid lines here. They kind of do get a little bit busy with the words. Maybe I would put them kind of behind, I don't know, whatever. But again, wanted to learn about gg text. And so we got that with geome rich text, as well as then using the title with that element text box simple to get us our nice wrapping. So we didn't have to worry about doing that. In previous episodes, when we put a title in like this, we used element markdown. And I actually then manually put in the BRs to dictate where the line breaks were. And that was a bit tedious, because I would put in a break, render it, look at it and say, oh, I need to be over a word, right? And then I'd kind of iterate that a bunch. Whereas here, it does it for me, right? It's a lot more simple and easier to work with. So that's pretty cool. And I'm happy with the way this looks. I think this would be a nice figure to go into a presentation or if I were to tweet this out or so, you know, the title is descriptive of what's going on. Clearly, if we're publishing this as part of a paper, we would want to put in a legend. And as we saw, we could put that the bottom top left, right, wherever to get whatever look we want. Hopefully you find this useful and interesting to learn more about GG text as a package. I think it's really cool that we can use HTML markdown CSS and put that into our figures to get any type of syntax you want. Anything you can do with HTML, more or less, I think you can do with these titles. Again, if you are out there reading the literature and you come across a figure and you say, gee, that's pretty cool. I wonder how you do that. I'm not really sure. Well, let me know. I would be more than happy to take that on and to learn how to do it myself if I don't know, and then to share that back with you and the rest of the community. At the same time, if you're trying to build a figure and you're just banging your head against the wall thinking, man, this is so hard. Why can't I figure it out? It shouldn't be so difficult. Let me know. Again, I'd be happy to take that on, help you out and then in exchange, help the rest of the community. I really would like to do more of this give and take between you all and me. I have no idea whether or not people are going to be interested in the video until I post it and I see how many people watch them. So I want this to be as helpful to you all as possible. Please keep practicing with this material. Really try to make it your own by applying it to the visuals that you are generating. As always, please be sure that you're subscribed and that you tell all your friends, your PI, your neighbor, whoever about these episodes. I've really been excited to see the growth of the channel. I really want to get this information out to as many people as can benefit from it and you can help me make that possible. So thank you for all your efforts to doing that and thanks for making it to the end of another episode. We'll see you next time for another episode of Code Club.