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 in proposal writing mode again. Unfortunately, I am finding my weekly search for a data visualization to share with you is leading me down unproductive internet rabbit holes. So, I thought I’d share an idea with you that I hope resonates. If you have any reactions, please send them my way! For the past year or so I have been recreating other people’s data visualizations in an attempt to learn new techniques with R and expand the type of data that I normally visualize. The idea...

Hey folks! Sorry for the hiatus in getting you a newsletter into your inbox. The end of the summer/beginning of the academic year has been pretty chaotic. Actually, I had what I thought would be an interesting plot to recreate, but then I wasn’t able to find the original data and I wasn’t really interested in simulating it. Oh well. I’m also finding it hard to come up with interesting data visualizations from out in the wild. One of my go-to’s, Philip Bump, stopped working for the Washington...

Hey folks! The summer is nearly over - where did it go?! Many of us are getting ready to send our kids off to school and start a new academic year. If you’re subscribed to this newsletter, I suspect you are interested in improving your data visualization skills. You can certainly continue to receive this newsletter and watch my weekly livestreams on YouTube for free to help increase those skills. If you want a more concentrated or personalized opportunity to develop your data visualization...