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’m writing this newsletter the US government is in shutdown mode with no clear signs that things will get going anytime soon. I’ll withhold my own political take except to say that my family has been running without an official budget for about 25 years. I don’t recommend it, but we know basically how much money goes to our mortgage, insurance, groceries, charities, etc. and how much money we generally have left over. Somehow we still are able to spend money on living a pretty...

Hey folks! This week I have a figure for you from the New York Times based on a poll they did with Siena that describes Americans’ sentiments concerning Israel’s actions in their war with Gaza. What does it say to me? This plot is saying that more Americans think that Israel is intentionally killing civilians than they did in December 2023. The change in percentage of people in the other categories seems to decrease accordingly. What do you like? I love slope plots! I think they’re a great...

Hey folks, This week I have an interesting figure for you from the Financial Times from an e-mail newsletter they distribute each week describing some visualization related to climate change. Before reading further, go ahead and spend a few minutes with the image. What does it say to you? What do you like? What don’t you like about it? How do you think you would go about making it in R? I’d encourage you to write down any of your answers to these questions before reading what I have to say....