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

Hey folks, We had a lot of fun last week with my first workshop on the theory of data visualization! If this is something that you’d be interested in participating in let me know. At this point, I don’t have anything scheduled. So, if you have suggestions for days or times, please let me know This week I have a fun figure to share with you from a paper recently published in Nature Microbiology, titled, “Candida auris skin tropism and antifungal resistance are mediated by carbonic anhydrase...