 A big challenge of making effective data visuals is coming to grips with the constraints that we have to operate in, right? That I would love to make the perfect data visual. But there's all sorts of limitations, and we call those constraints. What are some of those constraints? Well, I'm working in a 2D medium often, right? Whether that's on a paper or this projection screen of giving a research talk or on a poster. That's a constraint, two dimensions. In the world around me, you know, I've got a phone. It's a 3D thing. I've got a mouse. It's a 3D thing, right? I can look at it. I can inspect it. I can look around it, right? It's three dimensions. Yeah, I might have many dimensions to my data, but on a printed page or on a presentation or on a poster, all I can see are two dimensions. How do we deal with that? Well, I'll tell you one thing. We certainly don't print three-dimensional figures in two dimensions. Hey, folks, I'm Pat Schloss, and as you maybe can tell, I get a little bit animated about these types of things. Well, hold on to that idea of animation, because we might see something about that here in a few moments. Anyway, something that I've been seeing a lot in the microbiome literature especially are figures that people are preparing of ordinations like the ones we've made in recent episodes of Code Club, but instead of being a two-dimensional presentation, they actually show you three dimensions. They'll show you, say, an ordination with NMDS or PCOA, where they're showing you three axes. And the reality is that you can't see in three dimensions on a 2D page. Maybe there are some tricks you can pull to get a little bit more of that Z dimension, but it's really hard and you're asking your audience to do way too much work. Oftentimes, if you're trying to show things in 3D on a 2D page, there's usually a better way of doing that. Well, in today's episode of Code Club, I'm going to show you some alternative approaches that we can take. We'll leave behind the world of publishing and manuscripts and the printed page and think differently about how we can present our data to actually see our data in three dimensions. So I'm here in our studio with the basic code chunk that we've started all of the previous episodes with for the last several episodes, reading in the tidyverse and read Excel to get my NMDS 2D axes file, as well as my metadata file. And then we're joining those together and creating those different levels of disease. Because I'm going to work in 3D today, I'm going to use the NMDS 3D axes file. So I'll change that to a three. I'll go ahead and run all of this. So one other package that we're going to talk about today is called RGL. There's a lot that could be said about RGL. I'm really only going to introduce you to the basic fundamentals of this package. It will allow us to work with a data visual in three dimensions to make some really cool visualizations of our data. So if you don't have RGL installed, be sure you go ahead and install that. We also want to make sure that we've got that loaded. One thing that this might require is if you're on Mac is X11. Usually when you install RGL, it does that type of installation for you. So go through the prompts and do what needs to be done. One of the things to know about RGL is that it is not part of the tidyverse. And so if you recall the last episode, I talked about different ways to get columns out of a data frame. One of those ways is what's called with the dollar sign notation. You can also use the double square braces. So I'm going to use the dollar sign here. And one key function in RGL is plot 3D. And you'll give it X, Y, and Z. Again for my Canadian friends, Z. So we'll say metadata, NMDS, dollar sign, axis 1. And then we will copy that over to get axis 2 and axis 3. So let's go ahead and run this and see what we get. And bam, that opens up a weird new window. And wait for it. I can grab the vertex of this square of this cube and spin it around and look at what's going on, right? The kind of the three-dimensional structure of that data. It's pretty slick, huh? Cool. So that's going to be the foundation of what we're working on today. So something we would like to add is color. And so we need a column in metadata underscore NMDS that I'll call color. That doesn't exist yet, right? And so like in GG plot, we map a column to the color aesthetic and then we define the colors. Here we actually need to name the colors for each of the different samples in our data set. So I'm going to go ahead and add that to my mutate line here. We'll all do disease color. And I'm going to use a cool function called case when. And so case when is like a big if else statement. And so I'll show you how I do this here. So disease stat equals equals non-diarrheal control. So when disease stat is diarrheal control, I want that to be let's do gray, right? That's what we've been doing. And then disease stat equals equals diarrheal control. When disease stat equals diarrheal control, we want that to be blue. And so we've seen and then disease stat equals equals case will do tilde red. And again, what's happening is that it sees disease stat and asks is disease stat for this row equal to non-diarrheal control. If it is, then the value for disease color becomes gray. If it's not, it comes down and says diarrheal control. Then it's blue. Usually you want to end this with a true statement because you want there to be a true statement for every row in the data frame. Perhaps there's a disease state value that's not one of these three for some reason. It's good hygiene to have, like I said, that last value in case when to be true. And so if that is true, then I'm going to go ahead and I'll make this na underscore character underscore. And so that's going to return an na value. And so now if I look at metadata and MDS, I see I've got disease color there. And so I should be good. Double check things. I'll double check my colors. So I'll do metadata underscore at MDS. I'll pipe that to count on disease color. Ah, and I see I've got na. I shouldn't have any values with na. And I notice I misspelled control here. So I'll go ahead and rerun that. And then I'll rerun my count. And now I've got all my colors. Looking at my plot 3D again, I see I have color instead of disease color. Run that. And you know what? I think this is actually supposed to be call instead of color. It would be nice if all the interfaces have the same argument names, but they don't. So run that. And so we now see our ordination has our gray points. So if you think of this as a sphere as a ball, then kind of the points over here are gray. I guess to your side, maybe they're over here are all gray. And then down here are red and blue. That looks good, but they're really small. So an argument that I like to use to make those points a little bit nicer shaped and sized is type equals S. So the default is P for point, but S is for sphere. And so boom, this suddenly looks like someone of those like chemistry models, right? And so one of the things I just noticed is that I haven't told you is that if on my mouse, if I scroll in and out, I can zoom in and out as well as grabbing and dragging. So what I'm doing here is I'm holding down my mouse button and moving the mouse button around. And when I pan like this or zoom, I'm rolling in and out, which is kind of cool. But those balls are way too big. And so I can change this to say size equals one. And so let me zoom out again. And so there my balls look a little bit more of a decent size. And they look pretty nice, right? Cool. I just love this interface and it just annoys me to no end that all of our papers go straight to PDF because if we truly read everything in a browser, as I'll show you, we could implement this. We could insert this into a web page. If we were to read our papers, say in a browser, like many of us probably do. Anyway, we'll get there. So one of the thing I noticed is that these axes labels are the names of the data frame and the column. I'd like that to be a little bit nicer. So why don't I go ahead and add X lab. And I will say NMDS axis one. Y lab is NMDS axis two. We put a space here. And then Z lab is NMDS axis three and finally a comma. And again, now we see we've got our axis labels corresponding to the three axes. And if we, I guess if X is axis one and axis two is Y, we have X, Y and Z. And that looks reasonably nice, right? And again, I wouldn't want to print this into a image file like a PNG or TIFF because we lose a lot in the 3D. Translating 3D immediately into 2D on a printed page. So if you were happy with this type of visual, which I don't think you should be, you could do snapshot 3D. And then in the parentheses, you put the name of the file. So I could do Schubert NMDS 3D dot PNG. So you can only save it as a PNG. You can't save it as a TIFF. And so it has a little less resolution than we're used to seeing. And so again, here is a PNG version of your visual. Again, I don't think this looks very good. I think it deceives the audience quite a bit because we really can't see into the screen. We can't see the 3D. Also, I haven't really picked an orientation that allows you to more easily see where those gray points are. I think they're actually kind of on the back quarter of the sphere. If you wanted to, other things you could do say would be to make the background black and the frames white. I don't want to go into that because I frankly think that looks pretty bad. I prefer to have the white with the black frame and having everything kind of pop out out of that. Anyway, you do what you want to do. But I would really encourage you to dig into the different options that are available for manipulating and playing with how this all looks. So this is nice in an interactive framework right where I can manually manipulate this. But rarely am I actually going to be able to give this to an audience member to kind of open up my version of our studio or instance of our studio to play with the graphic. But there's two things I want to show you here that we can do to add interactivity with the visual. So one is to make a movie version of this where we have RGL spin around the visual so people can see kind of the more of the 3D aspects. And then also if we're doing things in HTML, how we would then go ahead and embed this in HTML so that the audience could interact with it. Or if it's in an HTML based presentation application, as you're presenting it, you yourself as the presenter could move things around. Anyway, I think this is really where RGL shines. So how do we do all this, right? Well, there's a function called movie 3D and this will take our current plot and make a movie out of it. So we can do spin 3D and then we give it the axis that we want it to rotate around. And I want it to rotate around the Z axis. So I'll do C0, 0, 1. As you can see from the pop up help here, if you put that one in different slots of the axis vector. So in slot one, you would rotate it around the X axis and the second slot the Y and the third the Z. And then RPM, five RPMs. And so if you made a movie that was a minute long, it would spin five times. Let's go ahead and make a, let's make a 15 second movie. And so I'm going to do RPM equals four so that over the course of that 15 seconds, it spins around once. And then for my movie 3D, I can get duration equals 15 for 15 seconds. And then I need to tell it what directory to put things in. So I'm going to do dur equals and then period forward slash. And that should then put the movie in my current working directory. So I'll go ahead and make this and I think that means that I don't have a movie or a 3D plot in memory. So let me go ahead and make this larger. And so again, that's going to spin around the Z axis and the Z axis is this axis here. And now if we run this, it does it. And so what it's doing is some fancy matrix algebra to spin the ordination all the way around. You know, it's going to do four RPMs, but it's running for 15 seconds. And we see that that gray patch did get back to the back quadrant again after it ran. It basically created 150 PNG files. So it does 10 frames per second. You could increase that if you wanted better resolution. And then it uses a cool package called image magic to then take all those PNGs and compress them together to make a GIF or GIF, depending on how you pronounce such things. If I then open up my new movie that GIF file, let's open that in Chrome and here it is in Chrome. I now have this GIF of it spinning around. And so I could take this GIF and I could put it into like a PowerPoint slide deck then so that when I'm giving a talk, I could have it spinning around. Now, maybe you would want this to be a longer movie so that, you know, as you're talking about what people should see here, you know, perhaps you want it to spin more. Or perhaps from the perspective you want to highlight, you might want to spin it around the x-axis or y-axis. Again, you can change the vector you're giving it in that axis command. Again, that movie is pretty slick. Maybe I don't want to put it into PowerPoint, but I wanted to have some interactivity that my audience coming to a web page could interact with or I as a presenter could interact with more directly. So how would we do that? I'm going to go ahead and create an R Markdown document. If you don't know about R Markdown, I've got some older videos using R Markdown to write papers. It's really phenomenal for writing reports and making HTML based pages. And if you look at my Rifomonas materials, all those instructional materials are written in R Markdown. So this is what it looks like. Again, I'm not going to spend a lot of time on this. I'm going to remove the default code and create a code block that has the three back ticks and then R in curly braces. And I'm going to go ahead and copy this code that I had before. And I don't want to make the movie 3D or the snapshot. But what I want to put in instead is RGL widget. And then I'm going to go ahead and do element ID equals NMDS. And this should work. So let's go ahead and save this as Schubert 3D web. Save it, knit it, and let's see what happens. And I think this D is supposed to be in lowercase. So save that again, knit. There we go. And so again, it creates this web page. It's HTML. You can see HTML there. And as we zoom in, it's a web page again that we can interface with. And I can come back to my finder and see I've got this HTML page. That again, I can manipulate that like that. So if I want to get rid of that code, I can go ahead and do. Message equals false, echo equals false, save and knit. And again, we see that we've got the interface that we're moving around, right? And it's again stored, saved as an HTML file that you can play with. What's so cool about this, even beyond being able to have an interactive 3D, is that I can go to our markdown again and make a presentation. And so I'll do 3D in slides. I'm going to use RMD and I'm going to use ISO slides, IOS slides. And so what IOS slides is, is a way to make presentations in HTML and through our in our markdown, which is really cool. So I'm going to go ahead and copy all this whole code chunk. And again, my goal isn't to give a tutorial on making slides, but you can make a new slide in this, our markdown document by putting two pound signs. And then I'll say NMDS diagram, and I'll plop that in there. And I will then save this as NMDS presentation.RMD, save that and knit it. And this will then create a slide deck in HTML. So again, I can come back to NMDS presentation.HTML. And so again, there's a lot you can do to play with formatting the slides, the appearance, the look, the layout. But what I want to show is that we now have a slide in my slide deck that if I'm giving a presentation, I can manually manipulate this slide deck. And again, because it's HTML, I could post this to my personal website. And maybe I put a bitly link on the first slide of my presentation. And so the audience could, you know, they could play with this themselves then, right? And so again, I think this is just such a slick way of working with 3D interactivity using this RGL package within R and RStudio. Anyway, there's a lot more to be learned about making slides and web pages using our markdown. That's not the point of this discussion. What I wanted to share with you is the ability, again, to make movies with as gifts or as these interactive 3D diagrams that we can manipulate and play with and we can do some styling on. You can make legends, but in my experience, they look kind of funky. And so again, titles and legends. And even as you kind of see the access labels here, leave a little bit to be desired. The formatting isn't as clean and crisp as we see with ggplot. But again, these are constraints, right? These are trade-offs that we deal with when we go between different types of media in presenting our data. And if you're presenting this in a web-based framework, yeah, maybe you have to learn that web-based framework, but it opens up all these other opportunities for interactivity and being able to share your data with your audience so that they can also interact with the data, which is so much cooler than getting a static, lame 3D picture in a 2D medium. It makes no sense, right? And so I think this is like the wave of the future of publishing. It gets me really excited, but we need to figure out how to do that with publishing. So for now, we can make cool presentations, cool websites, and maybe even cool supplemental figures like this. So I have published one of those gifts as supplemental material for a paper where I had a supplemental movie showing a 3D ordination in all sorts of different planes. I don't know if anyone's ever watched it, but I thought it was pretty cool to make. And you know what? Now you can do it as well. You can go in and you can make these animations, maybe upload it as a movie, as a supplemental figure to your great research. And so giving people more of that 3D flair than you can just embed in a 2D page. So always be mindful of the constraints that we have. Yes, we would like to make the perfect visual, but it's just not possible. Also, always be learning, always be improving and learning about things like RGL will certainly make it easier for you to communicate with your audience using alternative media. Well, with that, I encourage you to practice, see if you can replicate this movie, these interactives, with your own data. Let me know what you find. Are you successful or not? Where do you run into some roadblocks? Let me know. I'd be happy to answer those in a future episode. As always, if you see a figure out there in the wild in the literature and you'd like to know how to make it, or how to make it better, please let me know. I'd love to talk to you about it. Perhaps have you on an episode to discuss it, and we'll all learn and we'll all get better. Anyway, please tell your friends about Code Club. Be sure you hit that thumbs up button so that I know that you appreciated what you learned here today. And it also makes it easier for others to find this video as well. Keep practicing and we'll see you next time for another episode.