 do you ever read papers where on the x-axis they have really long labels and so they put them at an angle or maybe 90 degrees and you're you're just kind of kind of have to crane your neck and then for the rest of the day after you read that paper well you're kind of like this yeah well in today's episode of code club we'll see what we can do about that ah my neck that is not a great practice but too often i think we are left feeling if we don't have many options well in today's episode of code club we're going to look at long axis labels and what we can do to make them better in previous episodes we created a plot where the labels on the x-axis are rather long and my solution at the time was to put in line breaks to kind of make them go over three lines and that way they wouldn't be so long they overlap we wouldn't have to put them on an angle we'll revisit all those different approaches and we'll throw those by the side and i'll show you an even better approach at the end so be sure you stay tuned all the way through all right folks here's my code here in our studio schubert diversity dot r again remember if you want this starting code go down below in the description you'll find a link to a blog post for today's episode where you can get this starting code chunk also if you want the data you want to see how to install our studio r get the packages i'm using be sure you check the video that's linked above that will tell you everything you need to to get going very good so again this is my schubert diversity dot r script um leaving off basically where we were at the end of the last episode um if you haven't seen that go ahead and check it out it's not required uh let me go ahead and run this so you can see what we get in the end this is where we left off at the end of the last episode more or less i did change one subtle thing and that was to remove my line breaks here on the x axis labels and so you can see the problem right these labels are rather long and we'd rather them not be so long so what can we do well the solution that most people take is to rotate those labels so that they're easier to see and so they don't run over each other there's a couple ways that we have to to solve that problem of getting the rotation so let's go ahead back to our code and if we come down here to our element markdown for axis text x and if you're using element markdown or element text it doesn't matter the argument's going to be the same that we could put an angle equals 45 and that will rotate the text 45 degrees so let's give this a run and see what we get sure enough we've now rotated them 45 degrees but something you'll notice is that the label is centered at where it crosses the x axis right and what we'd rather have it be is perhaps right justified so the right end of the label is down here at where it crosses the x axis how do we do that i thought you'd never ask so sure enough down here we can do h just so h just is horizontal justification there's also v just for vertical justification so let's do h just equals zero i always get my zero and one confused i think though this is going to be left justified and sure enough it you'll see that it is left justified so let's do one instead and that should make it right justified and sure enough we now see that our labels are right justified and are pretty well justified again we have this problem i guess for the way you're seeing it you're gonna again crane your neck and and you can play with this angle you know if you want it to be 90 degrees so that it's up and down i think as people get more labels and more things across the x axis that angle kind of shifts to end up being more vertical to the point where you're really just kind of tilting everything so an alternative to setting the angle and the h just in element markdown or element text is to come back up to scale x discrete and to do guide equals and then we'll do guide axis angle equals 45 and so this will set the angle for us as well as the justification we now see that we've got that angle set for us and of course we could do any angle that we want here in this guide axis we can you know do 60 and we'll get a steeper angle to everything and and things start getting pretty messy right let me get rid of that another nice feature within guide axis is that you can give it the option n dot dodge and what n dot dodge does is you give it a number and it basically makes different rows for each of your labels and so if you say n dodge equals two then you get two rows and it'll dodge our reposition your x axis labels so that there's two rows let me see show you what we mean so what we see in this output is that we now have two rows of x axis labels they no longer overlap with each other however this third one the cdiff positive one does run off the right side of the figure but what we've got are two rows of labels so healthy and diarrhea plus cdiff and then in the second row diarrhea and cdiff negative i'm sure this works in some situations but in this situation i'm really not feeling it it's still quite long and kind of gets kind of messy here in the text so let's go ahead and put the line breaks back in i am using element markdown which allows me to insert html into the labels and um and so what i can do here is i can use the br tag which is a line break in html if you were using element text you would use backslash n instead of br so i'll put another br here and i'll copy this and and paste it in to put my n on the on a third row so we'll go ahead and run that and see what things look like and i still have my dodge on but you can see that i have those over the different line breaks let me replace n dodge with angle equals 45 again to show you what this looks like with a little bit more compact representation and that looks a little bit better i'm still creating my neck to see what it looks like or see what i'm reading again if i did angle equals 90 again you're creating your neck so that you can um see what's going on the other thing that i'm noticing is that as the label gets longer or the angle gets more steep it is adjusting the y axis but my text that i have to indicate significance is getting more and more compressed so i would if i if i went with this i'd have to go back and readjust the position of those things looking at this i'm reminded of an episode i did previously on the z pattern and that when we approach a figure at least in western cultures we read the figure in a z orientation and as i was thinking about this figure and thinking about the z pattern i was thinking well the information that's important is actually at the bottom um and you know if we if we could perhaps rotate our plot say 90 degrees that that would be pretty slick because then i could have i could have my x axis labels on the left side and because they're long i can read them without granting my neck um and i then have the important information about you know what the different groups are on the left and then i can see across to the right the different values of what's going on so let's give that a shot something that we might end up doing in the end is we could rewrite our gg plot to switch our x and y but say we're not really ready to make that type of investment how might we change things well i'm going to come down to the bottom here and i'm going to add cord flip and what cord flip will do is flip my coordinates 90 degrees so let's go ahead and give this a run and see what we get and so we see is that it's flipped at 90 degrees a couple problems though first of all my x axis labels um are definitely running over each other and i think that's because i forgot to turn off the angle equals 90 in that uh guide underscore axis function also before i had um my healthy in the first column followed by uh diarrhea seed of negative followed by seed of positive so my order here is all out of whack also i've got my uh significance indicators you know they're kind of in the wrong place so let's see if we can't clean this up a little bit first thing we can do is let's get rid of that angle equals 90 and i will also then come down and access text y and so that already looks better i'm not craning my neck it's a lot cleaner to read i think so the next thing that i want to take on is the order of my three groups i i think i like this and so i'm going to stick with it so i think i want healthy up top followed by seed diarrhea and seed of negative and finally diarrhea and seed of positive i set the order for that up here where i was reading in metadata and define disease that as a factor and i set these levels so i could copy and paste and and change the order of these things or i can use the rev function which is short for reverse so to show you how that works if i run rev if i run rev down here at my prompt then i get case dire real control non dire control reverses the order of everything so i'll go ahead and run everything again and those three groups should be flipped sure enough i now have healthy diarrhea and seed of negative diarrhea and seed of positive so the next problem is that my indicators of significance are now in the wrong place this bar and the ns need to be dropped down and this bar and the star need to be raised up if we come back down here to where we are adding those things at position two three which is position two three on the x axis is now position two three on the y axis this starts getting confusing we'll come back to that i'm going to change this now to one two and i'm going to put ns at one point five so let's do little steps we'll give this a run and we now see that we have ns and our bar at the right place so the next thing i want to do is move this bar and the star up and so that is going to start instead of at one it's going to start at three and come down to one point five so we now want to move the star and so that's going to be midway between three and one and a half two point two five and sure enough we've got our star i just want to bump that star and the ns over a smidge so i'll put that to 25 and this the 34.5 and i think we'll be in good shape now so that looks great again i think this is a lot more readable sure we can turn the angle on our axis labels but should we hmm i kind of think not something that you might also want to play with is where do we justify these labels what we could do again is we could say h just equals zero and we see that that's now left justified i still like i kind of liked it the other way where it was right justified because now we have this big gap between healthy and the axis so maybe we'll leave it the default which was h just equals one so one last thing that i'm thinking about with this figure that would maybe make it read a little bit better but again it's going to be a little bit different than what we're used to is this x axis label which is really you'll recall the y axis label is on the bottom and so i have to look through all this stuff before i look to the bottom and see what it is what if i actually put this up at the top how would i go about doing that well it's actually not that hard what we can do is we can add a scale y continuous and so we'll call it because we flip the coordinates scheme um what's y is now x and what's left we actually want on the right because that's going to be on the top staying with me so we can do position equals right and that will put it on the right and then it will flip it it should be on the top voila well what do you think i i think this is kind of cool um another way that we could have gotten to this is perhaps by having a title across the top telling people what they're looking at in terms of what variable we're plotting again for scientific publications we generally don't have titles at the top of each of our panels and a paper um i kind of like this um i know it's quite a bit different than what people are used to seeing we're used to having that x axis at the bottom um but again it shows the reader immediately what we're looking at let me know down below in the comments do you prefer do you like this look with the x axis on the top or on the bottom um i think i i'm kind of liking this a little bit um and kind of excited about it but at the same time i know that this is going to be really different from my audience um to think about one thing that i'm noticing as i talk to you and to think through the logic now of how we're working with this plot is that everything is different right so right is really top x is really y y is really x um it gets confusing and so while cord flip might be useful for um exploring our data long term it's going to be a pain right it's really going to cause problems as we're trying to think through and maintain our code long term so what would i do well actually would probably at this point get rid of cord flip and change everything to get it to look the way i want using kind of naming things the way they're supposed to be named so i'm going to go through that with you to show you how we would do it without using cord flip and so what we'll do basically is that everywhere we have an x we'll change it to a y and so i expect errors as we go through because things are going to get a little bit sloppy um and so again wherever i have an x i'm going to put a y and a y and x and we'll see if we can do this with minimal problems so again instead of right we're now going to be top um this all looks good um and then this is going to be axis text y so again here is a situation where it gets kind of confusing because the text is on the y axis but we used cord flip and we define the text on the x axis so this is a case where we still want it to be axis axis text y and i think we're in good there here now we want to change our x and y um and uh y uh y and x y x y x and i think we're good so we'll give this a run again something that you know i i think we could quibble about is where do we want that x axis label do we want it on the top or in the default position on the bottom um in terms of readability i kind of like it up at the top here because it's immediately clear what you're looking at sure you know it doesn't take much to look to the bottom and see you know it's inverse simpson index um but i do kind of like having everything referenced at that top row right like i'm using healthy as my reference position that i want my audience to compare everything back to so why not also have the default values of those index reference values for the inverse simpson index also at the top you know i could go either way again let me know down below what you think let's take some time and compare these two representations of the same data in the original version um again i have long long labels but i think the the better look for this would be to kind of wrap those labels like we ended up but leaving them um on the x axis and i so i think this is a clean representation so the problem with the angled version is that again you have to kind of crane your neck to the side to read it and to understand what's going on that's not so desirable also one of the things that was in the back of my mind as i approached this problem was this idea of the z pattern and that we like to read things in a z pattern or if not a z more of like an f um where again we kind of go left to right and kind of moving down i think this new representation of the of the data really embodies and encapsulates that z pattern or the f pattern um as again i can read across to see i'm looking at the inverse simpson index i can see the value range for my um x axis and then i can look at healthy i can then look at diarrhea and cdiff negative diarrhea and cdiff positive and i don't have to crane my neck to the side to figure out what's going on i really prefer this new version a lot more to the other i'm surprised at how much more i like it um again let me know uh what you think and if you like this better i know sometimes in this field of microbial ecology we can tend to have very long variable names um on that x axis where we might have some bacterial uh name uh as you know basilis sodalis or escherichia coli and then in parentheses we might put you know some some name or ot designation or some other bit of information and so those labels can get really long and so flipping the coordinates like this really improves the readability so you don't have to read about changing things on the angle that being said if you do need to move things on an angle or dodge things you can do that using the guide axis function in scale x discrete or you can do some of that also in the theme function for the the axis text and we also saw how you could use chord flip to flip the x and y axes but in the long run that gets difficult to maintain because it's hard to think about what's left what's up what's down what's x what's why and so if you like the flipped orientation i would encourage you to go back and recode your figure while it's still relatively simple and while you remember what you're looking at so that x means x y means y we're making making our yes mean yes and our no mean no right anyway i hope you found this episode interesting and transformative i dare say i want to remind you that i started with a critique right that there's something that is not at me about the figure and so by working through that critique and always trying to make it better i think that we have come upon a figure in the end that regardless of where you think that x axis belongs is a lot more readable and interpretable than when we had originally again always be willing to critique what you're working with and share what you've done with your friends and um and other people that that you know might be stakeholders so to speak in how to interpret those results anyway enough said uh again i hope you enjoyed this please be sure to share this with your friend if you're a student or a postdoc i'd be really intrigued if you share these two visuals with your pi ask them what they prefer do they prefer having the columns vertical with labels at the bottom or do they prefer having the columns go to the right with the labels on the left ask your pi what they think um and again let me know down below uh what you find out anyway see you next time for another episode of code club