Using R to generate a figure with microscopy images?


Hey folks,

This week I’ve been teaching one of my 3 day R workshops as part of my official teaching duties at the U of Michigan. I really enjoy teaching these classes! I offer recorded versions of these workshops that use microbiome data or other types of data to help motivate my teaching of R’s tidyverse packages. If you would like to purchase your own version of these workshop click on those links! Also, if you would like me to teach a live workshop to your group, reply to this email and let’s start a conversation.

My newsletter and YouTube content is really borne out of a desire to build upon what I teach in the workshops. My hope is that the workshop gives you a foundation. Then the other content I produce is meant to give you an immersive environment to continue to learn and practice the concepts from the workshop.


Last week I stumbled upon this panel from Figure 2 of the paper, “A membrane-bound nuclease directly cleaves phage DNA during genome injection”, which was recently published in Nature.

I’ll have more to say about the rest of the figure in Monday’s critique video. But this panel caught my attention because it reminded me of the faceted pie charts that I discussed a few weeks ago in the newsletter and then recreated in the livestream. I imagined people logging off as I went on about pie charts while also saying that the interesting thing about the figure wasn’t the pie charts. I think this is the same panel. Except for the pie charts, we have pictures!

How is it similar to that figure? Hopefully, you can see the figure as a set of facets with rows and columns. You might attempt to make this using facet_grid() with time on the rows and the condition in the columns. But, the problem becomes the difference in spacing between the lysogen with empty vector (EV) or with SNIPE columns of pictures. Also, the two sets of columns have different headings over them that describe a hierarchy to the experimental design.

Do you recall the special package I used to create the pie chart figure? Right! It was {ggh4x}. This package has a function called facet_grid2() that I’m pretty sure we can use to create the layout of this figure. I encourage you to go back to the livestream to see how to use the functions from {ggh4x} for these types of facets.

I’m also interested in how we can use R to insert pictures into figures. In the past I have done this when trying to include logos and other icons when recreating figures originally shared in the media. In those efforts I tried a few approaches including {ggimage}. I think that is the most popular approach. Yet, I recall it was a pain to get things inserted and sized properly.

My first idea would be to use {ggtext}. I hear you cocking your head to the side and asking how that would work. Let’s take a step back. Imagine I want to put a big letter in each of the panels. We’d do that with geom_text() where the label aesthetic would be mapped to a column with the desired letter. I’d probably put the x and y positions at 0 and then scale the axes to be the same size. Perhaps we’d also use coord_equal() to make sure each panel is a square.

You’re probably saying to your self, “geom_text() is not part of {ggtext}, it’s in {ggplot2}”. Right! Well, let’s use geom_richtext() instead. To get the same appearance of the geom_text() version of the figure we’d have to remove the border, fill, and padding from the output of geom_richtext().

The nice thing about geom_richtext() is that we can use markdown or HTML syntax. I suppose I could use markdown’s syntax, but I think I’d go with the HTML <img> tag to insert the image. Imagine that the first picture is in a file named “phase_0min_ev.png”. Instead of a letter, I’d map the label aesthetic to a column of a data frame that has that file name. The result should be a facet panel that contains the desired picture.

Of course, we will likely need to manipulate the sizing, expansion, and spacing between panels. But, I think using geom_richtext() is likely the quickest way to get this to work.

Have you ever tried to use R to create a figure panel that had pictures in it? In biology, pictures are commonly used to display microscopy images like this and images of gel and blots. I suspect that most people compose these panels in PowerPoint or Illustrator. If this has been your practice, I’d encourage you to give it a try in R and see how it goes. It would be wonderful if you could report back to me on your experience using R vs one of the other approaches.

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 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! I just got back from a seminar. I’m still trying to stretch out my eyes from straining to see the small text on each slide! If you don’t know why I’m brining this up, then you must have missed the videos I posted earlier this week. I was discussing the factors we should consider when converting figures designed for papers to figures designed to a slide deck. You can see me critique a figure from my own lab here and the livestream where I refactor the figure can be found here. I’d...

Hey folks, I was a student-invited speaker at the Syracuse University Biology department this week. It was great to meet with them and hear how they are benefiting from these newsletters and my videos. As much as I love posting newsletters and videos, seeing people light up at ideas, laugh at my jokes, and tell me how they are using what I teach them is like jet fuel. I actually gave two talks. One talk covered what I’ve learned about data visualization by critiquing, recreating, and remaking...

Hey folks, If you missed Wednesday’s livestream, I encourage you to go back and check it out. I recreated a panel from a paper published in Nature that is pretty typical. It was made up entirely of photographs. Sometimes I feel like I’m the only PI that doesn’t merge panels into figures using Illustrator or Powerpoint. I prefer to use R with some help from {cowplot} or {patchwork} to do this for me. That way I can write a single script to generate the entire set of panels. The result is a...