Creating a multi-column bar plot in R. How would you do it?


Hey folks!

Here in the US, vaccines continue to be a hot button issue. I feel like this issue is really an amalgamation of multiple issues including the decline in respect for authority figures, frustration with COVID, inability to assess risk at a personal level, and parents feeling like they are losing rights. Do people really want their kids to get sick unnecessarily? I doubt it. It’s also in the news because the Secretary of Health and Human Services is a vaccine skeptic/denier with many odd ideas.

Some of these trends are seen in a recent poll done between the Washington Post and the Kaiser Family Foundation (i.e., KFF). Because the report on the KFF is free for all to access and the news article on the Washington Post is not, I’ll share the KFF version of this week’s figure.

The report has two general types of plots. Ones with this columnar style and another with horizontal stacked bar plots. I’ve done stacked bar plots recently. So, I’ll roll with this columnar style of bar plot. This plot has a few interesting things that I had to think about how to implement in R.

First, the main plot is a set of bar plots that are laid out horizontally. That’s not too hard to achieve. Map the percent of parents who think it is very or somewhat important for children to be vaccinated to the x-axis and each group of people to the x-axis. But what about the four columns for each disease? I’d use facet_wrap() with 1 row and 4 columns. I was thinking of using facet_grid() with the disease mapped to the columns and groups of parents on the x-axis. But then I noticed the line for “Total parents” didn’t have a header like there was for “Party ID” or “Among Republicans”. With facet_wrap(), I’d make “Party ID” and “Among Republicans” a group of parents, but I would put NA or for the percent who think it’s important for kids to be vaccinated against each disease. That way no bar would show up for those categories. Then I’d label the three bolded headings with markdown and use geom_richtext() from the {ggtext} package to get those to be bolded.

Second, the numeric text in each bar has some nice formatting to think about how I’d implement. The font appears to be bolded similar to how the group titles are bolded. Also, the MMR and flu percentages are white, except when the percentage is below 40%. I’d likely use a case_when() or if_else() statement to create a column with the correct color and then use scale_color_identity() to display the color. Also, the position of those values below 40% are to the right of the bars rather than being inside them. I’d likely adjust the x-position of the label for those values. This should be relatively straightforward since we’ve done this type of stuff in other recent videos.

Third, the title have some interesting things going on. At the top there’s a “tag” indicating “Figure 2”. We can actually set this with the tag argument in the labs() function where we normally set the title, subtitle, and caption. Interestingly, the caption only goes across the figure about 60% of the way. I’m pretty sure we can achieve this with element_textbox_simple() using the width or margin arguments.

Finally, the KFF and The Washington Post have a logo in the bottom right corner. I think we’ve done this in the past with geom_image() from the {ggimage} package. It’s been a while since I’ve done this, so I’ll definitely have to consult the documentation again.

What do you think? I’d challenge you to see if you can come up with a similar overview for the horizontal bar plots in the KFF report. Let me know how it goes!

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, What a year! This will be the last newsletter of 2025 and so it’s a natural break point to think back on the year and to look forward to the next. Some highlights for me have been recreating a number of panels from the collection of WEB DuBois visualizations on YouTube, recreating plots from the popular media, and modifying and recreating figures from the scientific literature. I guess you could say 2025 was a year of “recreating”! I have found this approach to making...

Hey folks, As 2025 is winding down, I want to encourage you to think about your goals for 2026! For many people designing an effective visualization and then implementing it with the tool of their choice is too much to take on at once. I think this is why many researchers recycle approaches that they see in the literature or that their mentors insist they use. Of course, this perpetuates problematic design practices. What if you could break out of these practices? What if you could tell your...

Hey folks, Did you miss me last week? Friday was the day after the US Thanksgiving holiday and I just couldn’t get everything done that I needed to. The result was an extra livestream on the figure I shared in the previous newsletter. If you haven’t had a chance to watch the three videos (one critique, a livestream, and another livestream) from that figure, I really encourage you to. In the first livestream I made an effort to simplify the panels as a set of facets. Towards the end a viewer...