Creating a dendrogram that spans multiple facets


Hey folks,

I appreciated the emails I received from people after last week’s newsletter. I hope that even if people didn’t agree with what I had to say, it was thought-provoking. Regardless of how a plot is made - R, Prism, Excel (gasp!), or AI (oh my!) - we need to train our eyes and sense of taste to make the most compelling visualization of our data. If you’re interested in working with me on an individual or group level to achieve this goal, let me know. I am offering consultation sessions focusing on improving your data visualizations. If you are interested in learning more about what I can provide you, please sign up for a free 30-minute exploratory meeting.


Several weeks ago, I recreated a relatively complicated figure that included four heatmaps and a dendrogram using the {patchwork} and {ggdendro} packages. This week I want to try something related. Check out this set of panels from a paper recently published in Nature Cancer titled, “Reprogramming of stroma-derived chemokine networks drives the loss of tissue organization in nodal B cell lymphoma”.

This figure is created from single cell sequencing data. I don’t really know much about how these data are generated or the ins and outs of how they’re analyzed. Regardless, several things caught my eye about this visualization. First, there are the three plots (not sure why they’re grouped into 2 lettered panels rather than 1 or 3). This tells me that this is a chance for more practice with {patchwork}. I like to practice new things! Second, the legend in the horizontal stacked bar plot in panel f likely uses some of the concepts I discussed in last Monday’s how-to video on using multiple legend keys for the same category. More practice! Third, they have two dendrograms - one of which appears to span 5 facets. Oooohhhh… something new to try!

How would we make a faceted dendrogram? Yes, there’s a {ComplexHeatmap} package that appears to do exactly this. Actually, that appears to be how the authors of this paper generated the image. But how would I create this effect with tools I already know?

You might recall from the last dendrogram I created how we used the dendro_data() function from the {ggdendro} package to extract the dendrogram data from the output of using the hclust() function that we used to cluster the data. From the output of dendro_data() we used the segments() function from {ggdendro} to obtain a data frame containing the starting and ending x and y coordinates for each segment in the dendrogram. Previously we had to transpose the x and y coordinates to make a vertical dendrogram. In this case we want a horizontal dendrogram so we can use the coordinates as they are. We saw how we could use geom_segment() from {ggplot2} to draw the dendrogram.

Here’s my idea: let’s manipulate the x and xend position values to add the gap. Looking at panel e, there are gaps between the 5th and 6th and 6th and 7th positions on the x-axis (there are other gaps too, but let’s look at these for this discussion). Basically, I can imagine using a case_when() statement where I’ll add some increment - maybe 0.5 - to the x-position for anything that starts or ends at 6 on the x-axis. We’ll add twice that increment to create the second gap so that anything with an x coordinate position between 7 and 10 will have 1 added to it. We can add 1.5 to create the third gap for things between 11 and 17, and 2 to anything at 18 or greater to create the fourth gap. The y-axis positions for the faceted dendrogram aren’t affected by the facets, so they’ll stay the same.

What about the heat map? Same idea. Remember we create the heat map by mapping one variable to the x-axis and another to the y-axis. In this case, we could create a variable that we’ll map to the x-axis that uses the same spacing we used in the dendrogram. Then we can use scale_x_continuous() to map the text labels to the x-axis positions. Then when we use {patchwork} we’ll have the leaves of the dendrogram aligned with the columns in the heatmap. Cool, eh?

The key to all of this for me was remembering that we’re mapping data to the x and y-axis. By modifying the x-axis positions we can create horizontal facets. Similarly, modifying the y-axis positions would create vertical facets.

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 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...