Replacing a stacked bar plot with a dot plot in R with ggplot2 using sentiment analysis data from the Pew Research Center (CC352)
Pat uses R to show how to create a dot plot as an alternative to stacked bar plots like the one posted by the Pew Research Center describing people’s sense of how different groups will fare under the Trump Administration. To pull this off he uses the ggplot2, showtext, and ggtext packages. The functions he uses from these packages include aes, coord_cartesian, element_blank, element_markdown, element_rect, element_text, factor, font_add_google, geom_hline, geom_point, ggplot, ggsave, labs, library, margin, mutate, pivot_longer, rev, scale_color_manual, showtext_auto, showtext_opts, theme, tribble, and unit. The newsletter describing this visualization at a 30,000 ft view can be found here. You can find the original Pew Research Center article here.
Code
library(tidyverse)
library(showtext)
library(ggtext)
font_add_google("Libre Franklin", "franklin")
font_add_google("Gelasio", "gelasio")
showtext_opts(dpi = 300)
showtext_auto()
tribble(
~category,~lose,~gain,~none,
"Business corporations",11,70,18,
"Wealthy people",9,65,26,
"White people",5,60,33,
"Men",8,55,36,
"The military",21,57,21,
"Evangelical Christians",14,48,36,
"<span style='color:white'>dummy1</span>", -10, -10, -10,
"Older people",37,28,33,
"Younger people",39,27,33,
"Children",41,22,36,
"Asian people",46,14,39,
"<span style='color:white'>dummy2</span>", -10, -10, -10,
"Women",51,19,29,
"Poor people",56,23,20,
"Unions",50,16,32,
"Black people",53,16,31,
"Hispanic people",62,14,24,
"Gay and lesbian people",76,4,19,
"Transgender people",84,3,12,
"<span style='color:white'>dummy3</span>", -10, -10, -10,
"People like yourself",41,21,37) %>%
mutate(category = factor(category, levels = rev(category))) %>%
pivot_longer(-category, names_to = "effect", values_to = "percent") %>%
ggplot(aes(x = percent, y = category, color = effect)) +
geom_hline(yintercept = c(1, 3:9, 11:14, 16:21),
linewidth = 0.2,
color = "gray40") +
geom_point(size = 2) +
labs(x = "Percent (%)",
y = NULL,
color = NULL,
title = "Which groups do Americans expect to gain influence<br>— and lose it — in Trump's second term?",
subtitle = "% of U.S. adults who say each group will ____ in Washington with Donald<br>Trump taking office",
caption = "Note: No answer responses are not shown.<br>Source: Survey of U.S. adults conducted Jan. 27-Feb. 2, 2025.<br>**<span style='color:black'>RIFFOMONAS RESEARCH CENTER</span>**") +
coord_cartesian(expand = FALSE, clip = "on", xlim = c(0, 100),
ylim = c(0.75, 21.25)) +
scale_color_manual(
breaks = c("lose", "none", "gain"),
values = c("#af8dc3", "gray", "#7fbf7b"),
labels = c("Lose influence", "Not be affected", "Gain influence")
) +
theme(
text = element_text(family = "franklin"),
axis.text.y = element_markdown(),
axis.ticks = element_blank(),
panel.grid = element_blank(),
panel.background = element_blank(),
legend.background = element_rect(color = "gray40", fill = "white",
linewidth = 0.3),
legend.key.size = unit(10, "pt"),
legend.margin = margin(3, 5, 3, 2),
legend.position = "inside",
legend.position.inside = c(0.8, 0.63),
plot.margin = margin(t = 10, r = 15, b = 10, l = 10),
plot.title.position = "plot",
plot.title = element_markdown(face = "bold", lineheight = 1.3),
plot.subtitle = element_markdown(face = "italic", family = "gelasio",
color = "gray50", size = 10,
lineheight = 1.3,
margin = margin(t = 5, b = 15)),
plot.caption.position = "plot",
plot.caption = element_markdown(hjust = 0, color = "gray50",
lineheight = 1.3, size = 8,
margin = margin(t = 15))
)
ggsave("gainers_losers.png", width = 5, height = 7.4)