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, If you missed it, on Wednesday I did a livestream where I made a stacked barplot and pronounced it good. No, I wasn’t drinking anything! But it’s a reminder to think about the question before finding the best data visualization strategy. I think this highlights the value of the constructive approach I’ve been trying to take to critiquing data visualizations. The first steps are to establish the question and figure out the question. If you aren’t a “regular”, I think you’re really...

Hey folks, As I mentioned last week, I’m exploring the possibility of holding live, in person, workshops again like I did before the pandemic. If this is something that interests you, please let me know. My thought would be to hold them at an affordable hotel near the Detroit airport (DTW). But, if you would like to host me to teach a workshop, I would be open to that as well. This week, I want to call your attention to a plot that I would not encourage you to make. This comes form “Targeted...

Hey folks! I’m hoping to host two workshops in March and April. The first would be a Zoom-based workshop on the principles of data visualization (I taught a version of this last month). This would be a code-free workshop and would run for about 3 hours. I don’t have a date yet. If you are interested, please reply to this email and let me know if there is a date and time in March that would work best for you. The second would be an in person 3 day workshop taught near the Detroit airport. I...