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, It has been great to see the high level of engagement with my weekly critique videos on YouTube. I have really enjoyed making them and have learned a lot about current practices in data visualization. The one problem with these videos is that they’re a bit like an autopsy. We can figure out what went well or what didn’t work in a published figure. But we can’t do much to improve the published figure. What if we could do critiques before submitting our papers, preparing a...

Hey folks, This week I want to share with you a figure that resembles many a type of figure that I see in a lot of genomics papers. I’d consider it a data visualization meme - kind of like how you’re “required” to have a stacked bar plot if you’re doing microbiome research or a dynamite plot if you’re publishing in Nature :) This figure was included in the paper, “Impact of intensive control on malaria population genomics under elimination settings in Southeast Asia” that was published...

Hey folks! I hope you enjoyed last week’s series on the radial volcano plot (newsletter, critique video, livestream). I think it did a good job of illustrating the various reasons I think it’s valuable to recreate figures, even if we don’t like how they display the data. Something I didn’t really emphasize in last week’s newsletter was that by recreating a figure, we can make sure that the data are legit. I’m surprised by the number of signals I’ve been finding where authors using tools like...