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, Earlier this week, those of us in the US celebrated Memorial Day. For many, this marks the unofficial start of summer. I suppose the clock is now ticking until Labor Day, which is the unofficial end of summer. Let me be the jerk to tell you that you have 100 days left to accomplish all of your summer goals. I suspect that for many of you writing papers and putting together conference posters and talks are on your list of goals. Generating attractive visualizations of your data is...

Hey folks, I’ve been getting asked to give more talks about data visualization and my experiences critiquing visualization. It’s been a lot of fun to engage with live audiences. I enjoy learning about their experiences, motivations, and limitations. As much as I love this newsletter and the content I post to YouTube, it’s clear that it isn’t a substitute to talking to people without the filter of email or a chat box. So, if you’re interested in working with me on an individual or group level...

Hey folks, The more I peruse the literature, the more I see that researchers need help designing figures to help tell their stories. I don’t just mean the mechanics of creating a figure in R, Python, Prism, or Excel. Rather, if someone had a box of dry erase markers of various colors and they had to give a talk without any slides, what would they draw to tell their story? I don’t mean to trivialize the difficulties. It’s hard! There are many figures I’ve published that I wish I could have a...