 One of the things I absolutely love about R is the wonderful community of developers who are out there who can identify a problem, find the solution, and then they make that solution generally available to the rest of the R community through the use of packages. That's all well and good. But now when you're a user like me and you're trying to find those tools, how do you do it? And how do you learn how to use those packages? Well, that's exactly what we're going to talk about in today's episode of Code Club. Hey folks, I'm Patchloss, and this is Code Club. If you've been following along recent episodes of Code Club, you know that I've been building out a figure from a paper that my lab published several years ago. We've been critiquing it and trying to improve it along the way, as well as learning about using R, as well as a variety of different packages that we can use with R. Anyway, in the last episode, I talked about how we could make a multi-panel figure to replicate what we had in Figure 1 of that paper that we published those years ago. We did that using a package called Calplot. Now, there's another package out there that I've heard of called Patchwork that should do the same thing. I've never used Patchwork. I've never, I don't know that I've ever installed it in R. I've always used Calplot and people in my lab have used Calplot. I think the reason we use Calplot is because, well, somebody used Calplot many years ago. And we've kind of replicated that in subsequent papers that we looked to see how did someone do it previously, and then we reuse that code over and over again. That's very much in line with the philosophy of these Code Club episodes where we find a good idea, and we just kind of build off of that and learn how to use that idea, use that function, or use that package. I thought this would be a great opportunity for me to learn Patchwork, this other package that allows us to make multi-panel figures, but also to show you how I go about learning a new package. So this issue of packages raises a number of interesting questions. First of all, should you be even using packages? Second of all, how do you find the package that will do what you need it to do? How do you trust that the package is good and that's something that you should be using? And then finally, how do you go about using the resources within that package? Well, first of all, should you use packages? Well, I would say it depends, right? I held out for the longest time against using ggplot, dplyr, and other packages from the tidyverse. Why? I don't know. I think I held out because I knew how to do things in what's called base R. But what I realized later after someone patiently showed me how to use packages from the tidyverse was that the tidyverse made things so much easier than what I had been doing. It was easier, they were safer, it just performed a lot better. And so that convinced me that I should go and learn the tidyverse. Now, a few episodes ago, I showed you how you could put lines over the tops of your comparisons with stars or an ns indicate significance. What I didn't tell you is that there's a package or there's probably several packages that will do exactly that for you. Now, do I want to go out and learn that package? Probably not. You know, I know how to make those lines and put those stars on there fairly easily without all the extra baggage of going out and learning that extra package, whether or not you want to go learn that package versus using the skills you already have to do the same thing. That's your call. And ultimately depends on kind of the tradeoff between how much you have to learn versus what benefit are you going to get from learning to do that? So the second question then is, well, how do you learn about packages? How do you know what's out there? Well, I think you've really got to embed yourself in the community of people that are using R. Figure out where people are who are using R and listen to what they're saying. So go on social media, live on Twitter, if you will, look at who are the developers who are developing our packages and see how they're using R. What packages are they using? This gets to another point. But you know, if I see that Hadley Wickham who kind of pioneered the tidyverse is using a certain package, or if he developed it, then I'm going to believe it's credible and something that I feel safe using, you know, around Hadley, there's our studio, there's, you'll find that there's a community of developers. And like I said, you'll see generally what they're doing. Another thing you can certainly do is Google, right? So if you Google your question, something like, you know, how do I create a figure with multiple panels using ggplot, you will, you'll no doubt get tutorials or demonstrations of how to do that. So the third point of how do you trust a package? I already kind of got into that is that you kind of know who's in the community, right? And you know who's using it. Other things you might do is to look and see, you know, how recently has it been updated? What does the documentation look like? You know, so if it's been recently updated, if it seems like they're actively working on it, that's a good thing. It can also be a bad thing, right? Because if they're constantly changing things, then that's not so great. But again, if there's great documentation, then then you'll see that they're really dedicated to other people learning it versus perhaps being a package that, you know, one or two people use, and it's really for their own personal use. And they have really no intention of anyone else using it. So those are a couple of things to really look at. I would say the prestige or the notoriety of the developer, how often are they updating it? Does it seem like they're actively working on it? And then also, is there a good documentation? Are there demos? Does there seem to be a lot of information around the internet, social media, wherever about that package? So finally, we want to learn how to use that package. And that's exactly what we're going to do today. I'm a little bit nervous about this, because again, I've never used patchwork before. And this will be new for me as well. But I wanted to take this opportunity to replicate something we've already done with Calplot, and to show you how I go about learning and a new package and the syntax that I need to implement that I will come over to my browser and if I do our patchwork, I see that this pulls up a number of great hits, I guess 48 million hits, they're probably not all things we want to look at. But I just want to highlight a few things here in these first three hits. So I'll start at the bottom here. So if you see CRAN, that is like the comprehensive archive network or something like that. But that is the official repository of all packages that are kind of approved for use with R. That means that the package has gone through some vetting to say that is a legit package that isn't super buggy. And that again, performs well and meets some kind of minimum threshold for quality. If you're doing things in bioinformatics, there's another repository called Bioconductor that has another level of quality threshold to it. So I guess I would revise my earlier comments about how do you know whether or not to trust a package and that if it's in CRAN, then that again increases what you should have as confidence in that package. So this CRAN link here will bring us to kind of the official documentation that comes with patchwork. It tells you the versions, what other packages it depends on, where you can go to file bugs or problems that you're experiencing with it. There's links to patchworkdataimagines.com, github.com, github as we'll see later is where the live code lives for this, as well as a variety of different materials like readme's and news. There is a reference manual here as well as some vignettes like getting started. I'm going to open those tabs and we'll come back to those in a moment. The other link, thomasp85 patchwork, this is a github repository, which if you're not familiar with github, oh my gosh, you totally should be because it is a great resource for enhancing the reproducibility and transparency of your projects. And as you can see in the about, this is a composer of ggplots. So this tells me that this works very well with figures generated from ggplot. Again, this gives you a readme instructions, information about how to install patchwork, how to use patchwork to make, in this case, a two panel figure. And the syntax that I'm noticing is that I could do p1 plus p2 and I get side by side figures, which is pretty slick. And similarly I could do, you know, there seems to be some formatting that I can do in my code to get different layouts of the plot. So this looks pretty cool. I see a variety of different guides down below in the Learn More, just going to different things that I can do to get started or assemble plots to define layouts and so forth. There's clearly a lot here with patchwork that I can do. These are all good signs that patchwork is a great package. Let me come back to my Google search. The top link, another source of documentation that again looks very much like the readme. So this is kind of redundant in a way with what we've seen from both CRAN as well as GitHub. I would say that if you're more of a power user, then going into GitHub and looking at the code and the documentation associated with the code is probably more your speed. In here there are instructions on how you can get the development version of patchwork. So they perhaps only post to CRAN every couple of months. Well, here you can get, you know, the version that was produced today. If you're more of a user, end user, and not so concerned about the coding aspect of things, then probably this site here that we see with patchwork is where you want to be. So what I do when I want to learn a new package is I'm looking for examples. I learn from examples. That's part of the reason I make these videos is to give other people examples of how to use code to do different things with the idea then that I can take this example and modify it for what I want to do. Generally, what they will give you in these examples is what we call a reproducible example or a minimal reproducible example. This is a very simple chunk of code to get what we want. If you look back at my code, I've got 157 lines to generate four figures. They've got two lines to build figures. So this is much simpler allowing it to have then a heightened emphasis on what's going on here with the patchwork code. So coming over to our studio, I'm here in my figure 1.R. If you would like to get a copy of this, I strongly encourage you to do that. You can get a copy down below in the description. There is a link to a blog post that will give you the code that I'm starting with. People that have gone about getting the code and running it in parallel with me, experimenting on their own have told me they get so much more out of these episodes rather than just watching me perform coding or whatever. Also, if you're not sure how to get things set up, then up above here, I'll put a link to a episode where I describe how to install our studio, how to get the tidy verse and how to get everything set up so that you're good to go whether you're on a Mac or on a Windows computer. And also know that I teach three-day workshops demonstrating how to use the tidy verse and helping you kind of get a really good foundation on using functions from the tidy verse to build plots that are important for things like the fields of microbiology and just general science. The first thing that I need to do so I can play around with patchwork a bit is to install the package. So I'm going to come over to our studio to the packages tab and then in the finder window here, I'm going to type patchwork and nothing pops up. That tells me it's not installed. I can then click on install. And then in this middle packages dialogue window, I can then type patchwork. And this then runs install packages with patchwork. And it'll go ahead and install patchwork for me. I see now that over on the right in my packages tab, I now have patchwork loaded. So to play with patchwork a bit, I'm going to go ahead and open up a new R script. And I'll go ahead and copy this code. I guess I could have just clicked on this handy dandy icon there. And then I can paste it into my untitled and I can then go ahead and run these lines of code. And what I see is that I get side by side my MPG in my gear, my P1 and P2, which is pretty slick. If I did P1, I think I should get a three panel figure, right? Again, it doesn't matter what I'm plotting. I want to see how the package performs. And this is really slick that it knows that the addition sign, if I'm adding together two plots or three plots, we'll array them side by side. Let's look at some other examples here. Again, we've got it's giving us a couple more figures. So we can do P1, P2, P3 divided by P4. I'm reading this text here, Patrick provides rich support for arbitrarily complex layouts with full alignment. As an example, check out this very readable code for nesting three plots on top of a third. So we've got three on top of a fourth, because that's a typo. And so this gets me thinking about, you know, I want to have two on top of two. So maybe I could take this code, I'm going to copy this again. And let's go ahead and run this code and see what it looks like. Yeah. And so we get our three on top of a fourth panel. And so this gets me thinking, well, what would happen if I removed this, and I put over parentheses, then P3 hyphen, or vertical bar P4, cool, I then get two on top of two. And that looks a lot like what I'm trying to do with my four figures that we did with cow plot. And this is, I would say much easier syntax than what I was doing before. If I come back down to my my plot grid, I've got these four plots, I've got to say the number of rows and columns, I guess by default, it knew I would put it into two rows and two columns. So some of this isn't necessary for plot grid, right? Like this, this is basically the same code, although I'm not getting the labels. So I want to come back and think about the labels. So let's go ahead and let me run what I had here from figure one dot r and see what that produces. And so we've seen this figure several times already, but this gets us our A, B, C, and D. I'm, I'm like really eager to use this. And I always want to experiment with things. So instead of cow plot, I'm going to put patchwork here and make sure I've got that library loaded. And then I'll come down here and I'm going to comment out this plot grid. I'm going to copy that. And it's going to be like the vertical line. And this is in parentheses. And then there was the division sign. And then this is in parentheses. And this is with the vertical line. Now one thing I'm wondering is, what's the difference between the vertical line and the plus sign? I'm not sure. But let's go ahead and run these and see where we get. And so that looks very similar to what we had before with cow plot, except now I don't have the four labels. So we'll want to come back and figure out how we get that back. Again, I think the syntax is pretty simple. I don't have to worry about was it grid plot or plot grid was our underscore not an underscore, what's the syntax, things like that. This is really nice. And as I said, one of the questions that came to my mind was, do I need that vertical line or could I use a plus sign? So if I do plus, I wonder what we get here. And so that looks basically the same. I don't see any difference. So again, something I'm going to hold in the back of my head is why would I use a plus versus a vertical line? Okay, so let's go back to the documentation and see what else we can learn about Patrick. But so far, it seems really simple and really easy to implement. But there are some questions that are kind of lingering in the back of my head, like, how do I put ABC and D, this vertical line, the pipe character versus plus sign, and then how do I maybe change the width or height of each individual panel? So let's go ahead and I'm going to just open up all of these links. And I would say this is excellent documentation. If you're looking for a model of what documentation should look like, this is it as somebody again, that really gets excited by running examples and modifying examples. This is my this is my speed. Okay, so this is getting started. This looks like stuff we've seen before, where again, they're generating four figures. So as it says, the simplest use is the extension of the addition operator to add plots together. That looks nice. When adding plots together, the last added plot will be the active one and receive any additions of the new ggplot objects, such as geomes and labels and so forth, right? So as I can see, I do P1 plus P2 and then labs, that's going to add the label to the last plot, not to the first plot. Again, it will try to keep the grid square and fill it out in row order. Okay, let's give that a shot, copy this down. And instead of putting things in parentheses and whatnot, use addition. Yeah, it looks pretty slick. It does default to plotting things as a square. And so now we see this can be controlled the layout with the addition of plot underscore layout function, we could say the number of rows, and whether we want to do things by rows or by columns first. So now it moves on to stacking and packing plots, you want to place plots on top of each other besides each other rather than filling out a grid. So it's as well as can be accomplished by adding a one row or one column layout. Patchwork also has two operators that does this directly. So a vertical line, like we also call that a pipe, places the plots besides each other, whereas the slash will stack them. Oh, that's cool. You could do P1. And then you could do P2 divided by P3. So P1 is in the first column, and then the second column is P divided by P3. I guess I would think of the vertical line then as defining my columns. Yeah. So let's see what we can do with that. So if we put the strip chart on the left side, and then what if we were to then stack our three other plots? And so we could do like that. So we have the three rock curves stacked on top of each other with those slashes, but in the second column. Very cool. So we have our strip chart in the left column, and then our three rock curves in the right column. I actually really like this, because again, these rock curves are designed off of my strip chart here. And this is a design I really hadn't thought about using before. I don't know why. But anyway, that's pretty slick. And something I might want to do is perhaps control the height of this strip chart. So it's perhaps not, you know, from bottom to top, but maybe represents maybe half of the space in that first column. Anyway, that's really cool. And I think really, really powerful. Okay, let's keep coming through here. And so we have annotation of the composition. So we can put a plot annotation for the title of the overall figure. We also have tag levels. And so the tagging can either be Arabic or Roman numerals or Latin numbers and separate tags may give in different different nesting levels. So let's try adding a title, like figure one, and then tag levels. So we can then do plot annotation, I'll do title equals say figure one. And then tag levels. And let's do capital A for add. So that doesn't seem to have done anything. Something I wonder about is I noticed that this has parentheses around the plotting algebra, so to speak. Let me go ahead and try that. There we go. So it seems that we did need parentheses around the formula for the assembly. And then adding plot annotation as well. And so we see ABCD, we've got figure one across the top. Maybe I don't want that figure one. So if I do tag levels equals a we get rid of that. Something I'm curious about is if I do lowercase a, yeah, then it's lowercase ABCD. So I learned a lot here, right? The vertical line indicates that we're creating different columns, whereas the slash means for different rows, and that we can nest rows within a column like we have here. If I use the plus sign, then that's kind of filling out a grid. I can annotate with plot annotation. And that's, that's really cool. So one thing that I'm still wondering about is sizing, you know, if I wanted to make my strip chart to not be the full span of the column, how would I do that? Or how would I make, you know, one column, perhaps more narrow than another column? So I talked about plot assembly. Just going to kind of quickly look through this, see if there's anything new that I want to add. It shows that I can add other non gg plot content, like a table or something like that. Or something from like base R. I'm not worried about that, like I said, but it's good to know this is here. This is something I'll file away in my head for later. Talked about stacking and packing like we've already seen. And then kind of putting stuff together. And as we talked about earlier, when you modify a patch, it's modifying the last plot that was added. If you want to modify a different part of the patch, you can access it with doubles bracket indexing. So this is a little bit more advanced than what I want to do. I think I'm really just going to stick to making my plots as I like them, and then adding them together later. But again, it's nice to know that I could come back and I can modify things as I like. And then if I want to modify everything, so it says that the ampersand will add the element to all of the subplots in the patchwork, and the star will add the element to all the subplots in the current nesting level. And so there's operator precedence that has to be kept in mind. So it's clear this can get really complicated really fast, right? So let's go ahead and see if we can add theme minimal. So we would do add theme minimal. So yeah, that's added theme minimal to everything. One thing I noticed is that my x ladle here get kind of screwed up because I did use element markdown in theme. Again, I don't think I would use this. I think I try to make individual plots look the way I want them, and then add them together. But again, your mileage may vary controlling layouts. What I'm looking for is there a way to control the width of the column. And so I can add different size spacers. So maybe that's something I could do, right? Like that. And I think I need parentheses here. So this would create, no, I need those to be forward slashes, because I'm going to put spacer, my strip chart, my spacer in the first column. See if this works. Yeah, that worked pretty well. I still have this theme minimal on here. Let me get rid of that. Yeah, that looks pretty good. I might make this a little bit taller, I think, but I'm not sure if we're going to be able to do that. We'll see. Aha. Here we go. Our plot layout widths, C two and one. And so I think the widths will control, yeah, the widths of the two columns that we have. When grid sizes are given as numeric, it will define the relative sizing of the panels. So I see that I can set the widths and the heights. I think what I might need to do to get the strip chart to be a little bit taller than the others is to build it in two parts. Save this with commenting it out. And so I'll do like column one. And I will say this, right? And for good measure, I'll wrap this in parentheses, and then do plot layout. And in here, I'll do then do heights equals C. And then I'll do quarter, half quarter. Okay. And then column two, I'll put this stuff, column two, and that will be that. And then I'll do plot layout, heights, equals C. And this is gonna be like one, one, one, right? So they'll all be equal size. And then let's do call one, pipe, call two, and let's see if this works. Yeah, that looks pretty slick. Okay, now we need to add on our labels, right? So let's go ahead and do that. And then we'll do plot layout. And what was that called? No, no, it's plot annotation, right? Tag, tag levels, yeah. And I'll put a awesome, we got it. That looks really good. So one thing I'm noticing in the composition of all this, of course, is my star here is a little bit truncated. And so I might want to go back and maybe raise the y axis limit here a little bit, or maybe drop my star down a little bit. But I think that looks really good. I'm also noticing that my text in my three rock curves is getting a little bit messed up. But again, I don't want to get into messing around with that too much today. I mainly want to just kind of highlight how we can use patchwork and learn how to use patchwork to get these different effects. One thing that kind of sticks out to me that annoys me a little bit, I generally like to have these ABC and D to be bold. So they pop a little bit more, these seem to be kind of in a plain font. So let me come back to the tutorial here, these vignettes and see if we can figure anything out. So adding annotation and style. So we can do titles, subtitles and captions. So our overall plot can have a title and subtitle. That's really cool. Individual plots, we know we can do that with the labs function from GG plot. That's cool. We talked about tagging. I'd like that tag to be bolded. And so we have theme plot tag size equals eight. And so that changes the size to make it smaller. I bet I could also make it bigger, bolder, right? And it's showing us all these different things that we can do here to add prefixes and suffix things I'm not super concerned about. But I would like to make that bold. Let's put these on a couple of different lines. And I'll do theme plot dot tag equals element text. And I'm going to add face equals bold. That didn't seem to change the font face of my tags, the ABC and D. Let me look back at the vignette and see if perhaps there was something I missed. You know what, there's an ampersand and I used a plus sign. And again, I think that ampersand applies it to all of the plots, whereas the plus maybe only applies it to the last one. Regardless, let's go ahead and try it with the ampersand. So that did it. These tags now are bolded and look really nice. And I'm pretty happy with that. So I'm really happy with how this figure turned out. I wasn't expecting to end at a place where I had the strip chart on the left side and then my three rock curves in a separate column. I think this looks really good and actually a lot better than the original layout. I'm really happy with it. I think everything we've done here, we could probably have also done with cow plot. Frankly, I like the syntax a lot better using patchwork than cow plot. This algebraic manipulation makes a lot of sense to me. It also works really well with kind of the mindset and framework that we used with ggplot. I hope you found this discussion of patchwork helpful. I hope it also was illuminating how I go about learning more about a package and figuring out how to implement it. Again, the keys are for me is, you know, finding good documentation that has really good examples. We didn't do it here, but know that in the help documentation for individual functions, there will also be examples at the bottom of those help pages that again, you could do the same thing where you kind of put it into an R script, run it, modify it, tweak it, and then apply it to your own thing. Again, this is the whole idea of Riffamonus, that we identify something like what we saw here in this vignette code that we borrowed, and we start playing with it, modifying it and say, hey, that looks a lot like what I want to do. Then we do that with our own code and we get the result that we're really excited about. Please be sure to share this with your friends and tell them about Code Club. I'd love it if you left a comment below telling me which you prefer, Cowplot or Patchwork. Give them both a shot and see what you think. As always, appreciate your feedback. Give me a thumbs up if you don't mind. Be sure you're subscribed. And of course, we'll see you next time for another episode of Code Club.