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! I’m appreciating the positive feedback on Monday critique videos. They’re a lot of fun to think through and make. I think I might start looking at figures that are drawn from the scientific literature since many of you found out about me from my science work. Let me know if there are plots or practices that you’d like to see me talk about. I’ll see if I can work them into the queue. Also, if you’re working on developing figures for a presentation, poster, or paper and would like to...

Hey folks! I continue to get positive feedback about my critique videos. This has me quite excited that I’ve perhaps scratched an itch that people have been struggling with. Would you like to meet with a group of other people who are committed to making their data visualizations better? I’m forming groups now that would meet once a week or every other week to give each other constructive feedback on the visualizations they are making for their work. Alternatively, if you have ever thought, “I...

Hey folks! I posted two videos last week! On Monday I posted a video critiquing the diverging bar plot that I described in this newsletter last Friday. My goal in this video was to think through a “constructive” approach to interpreting and critiquing data visualizations. As scientists, I think we are too worried about hurting each other’s feelings. So we don’t critique each other. At the same time, many of us think before we speak and can come off overly harsh. My goal is to create a...