Adding additional layers of text and titles to an x-axis with ggplot2 in R


Hey folks,

Did you miss me last week? Friday was the day after the US Thanksgiving holiday and I just couldn’t get everything done that I needed to. The result was an extra livestream on the figure I shared in the previous newsletter. If you haven’t had a chance to watch the three videos (one critique, a livestream, and another livestream) from that figure, I really encourage you to. In the first livestream I made an effort to simplify the panels as a set of facets. Towards the end a viewer suggested making the facets vertical rather than horizontal. That gave rise to the second livestream. In that session another viewer shared their experience using {patchwork} with map(). These are the types of interactions that keep me coming back to do more livestreams. Thanks for all the feedback and time people spend watching these! I know me fumbling over the introduction to the videos, suspect audio quality, and other issues might have the feeling of watching a train wreck. All of this makes me even more grateful for your patience and interest. Thanks!


This week’s figure is taken from a cool microbial ecology paper recently published in Nature Microbiology by Bakkeren and colleagues. The paper’s title is “Strain displacement in microbiomes via ecological competition”. I really enjoyed reading this paper and thought they did a very nice job of exploring fundamental concepts of ecology with microbes. There’s a lot of similarity across the figures, but I’m going to focus on Figure 2. I’m still not which set of panels in this figure I’ll focus on in my livestream. Today, I want to think about panels f and g

If you’ve seen my recent critique videos, you’ll appreciate that I love this type of plot. Each panel is a jittered plot and there is a line through the points for each category to indicate the median. No boxes. No bars. Just a simple jitter plot with a line for the median. Well done. I won’t go overboard with praise, they do list P-values out to 4 significant digits :)

Let’s use figure 2f for our discussion. What intrigued me about this panel was the two-layered x-axis text. The labels on the “Invader toxin” line will come from the data frame. We might need to make a special variable for the label that would have values like wt_null, wt_e2, wt_k, etc. Then we would change those values to “-“, “E2”, and “K” using scale_x_discrete(). This step should be pretty straightforward.

The labels on the “Resident” line will require some more thinking. I could imagine making the panel as a facet with one facet for each of the WT and mutant resident strains. The WT and the ΔsrlAEB could be the facet titles and those can be moved to the bottom. But, I think that won’t really make the labels any easier. We’d still need to put the axis titles off to the left and add the bracket.

I think it would ultimately be easier to add the text with geom_text() or {ggtext}’s geom_richtext(). Which function would we use? There’s a Greek symbol (Δ), which we can actually insert with unicode. But the mutant name is italicized while “WT” is in a regular font. We could do both labels with two geom_text() calls changing the value of the fontface argument in each. That doesn’t seem very elegant. I would use geom_richtext() instead and add the markdown around the mutant’s name. We could also place the “Invader toxin” and “Resident” labels using either geom. The titles are right justified while the text is centered. You can have both in a single function call, but it might be easier to work with if you split up the titles and text. I’d probably split up the titles and text, but it doesn’t really matter.

The next element are those brackets. Save your email, I know there’s a package to draw these (and the significance bars). I generally prefer to draw them in with geom_segment(). I find it easier to reuse functions I know than to learn a new package to do something I rarely ever do.

To add the text and brackets to the bottom of the plot (and probably the P values at the top of the plot), we’ll need to plot outside the panel. Step one will be to set the x and y-axis limits to match those in the plot. Next, we’ll want to add clip = "off" to coord_cartesian(). Finally, we’ll also need to add margin to the bottom of the plot to make room for the brackets and text. Perhaps the hardest part of placing the brackets and text will be figuring out the y-axis position since the y-axis is on a log10 scale. I’m going to guess that the y-position for the horizontal part of the bracket is 10^1^ and the “Resident” line is at about 8 or 9.

Thinking about those P-values, we’ll do a lot of the same things we did for the x-axis. Again, I’d use geom_richtext() because the “P” is italicized and the rest of the text is in a regular font face. See how practicing little things for one application make them easier to see and execute in others?

I would love to have time to do panels b through e, but I don’t think I’ll have that luxury this week. Let me know which set of panels from this figure you’d like me to work with by replying to this email!

One other thing I want to point out about the figures in this paper. The methods section says everything was made in GraphPad Prism. I've never used Prism, but can usually tell people are using Prism because the styling of the plots is thick and ugly. I don't know what they're doing differently, but for a change from the typical Prism plots, these really looked nice.

Workshops

I'm pleased to be able to offer you one of three recent workshops! With each you'll get access to 18 hours of video content, my code, and other materials. Click the buttons below to learn more

In case you missed it…

Here is a livestream that I published this week that relate to previous content from these newsletters. Enjoy!

video previewvideo previewvideo preview

Finally, if you would like to support the Riffomonas project financially, please consider becoming a patron through Patreon! There are multiple tiers and fun gifts for each. By no means do I expect people to become patrons, but if you need to be asked, there you go :)

I’ll talk to you more next week!

Pat

Riffomonas Professional Development

Read more from Riffomonas Professional Development

Hey folks, Did you know that you can do statistics in R? HA! Of course it is. As the first sentence of its Wikipedia entry says, “R is a programming language for statistical computing and data visualization”. I rarely discuss using R for statistical analysis and focus far more attention on the data visualization power of R. This week, I’d like to share a set of panels from a figure in a paper recently published in Nature, “Lymph node environment drives FSP1 targetability in metastasizing...

Hey folks, I’ve really enjoyed the flow of combining these newsletters with a Monday critique video, a Wednesday recreation video, and occasionally a Friday remake video. A few weeks in, I feel pretty good about our ability to engage in constructive critiques. Of course, we have to train ourselves (myself included) to use those tools and not just resort to immediate and emotional responses - “I hate that plot”. We need to engage, get in the head of the original creator, and try to understand...

Hey folks! I’m appreciating the positive feedback on Monday critique videos. They’re a lot of fun to think through and make. I think I might start looking at figures that are drawn from the scientific literature since many of you found out about me from my science work. Let me know if there are plots or practices that you’d like to see me talk about. I’ll see if I can work them into the queue. Also, if you’re working on developing figures for a presentation, poster, or paper and would like to...