library(tidyverse)
library(ggplot2)
library(viridis)
library(grid)
library(gridExtra)
library(hrbrthemes)
library(scales)

For the challenge this week, I want to highlight 2 different charts in the same overall visualization. I’ll be working with the Tidy Tuesday Week 8 Census data.

census <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2021/2021-02-16/census.csv')

# for the numeric charts
c <- census %>%
  filter(region == "USA Total") %>%
  select(region,year,white,black,black_free,black_slaves)%>%
  pivot_longer(cols = c(white,black,black_free,black_slaves), 
               names_to = "Group", 
               values_to = "Total") %>%
  filter(Group != 'black')

# for the percentage charts
cp <- c %>%
  group_by(year, Group) %>%
  summarise(n = sum(Total)) %>%
  mutate(Percentage = n / sum(n)) %>%
  select(!n)

Side by side charts:

# numeric chart
p1 <- ggplot(c, aes(x=year,y=Total,fill=Group))+
  geom_area(alpha=0.6 , size=.5, colour="black") +
  scale_fill_viridis(discrete = T, option = 'magma') +
  theme_ipsum() +
  theme(legend.position="bottom") +
  labs(x="Year", y="Total",
       subtitle = "Total Population by Group",
       caption="") +
  scale_y_continuous(labels = comma) 

# percentage chart
p2 <- ggplot(cp, aes(x=year,y=Percentage,fill=Group)) +
  geom_area(alpha=0.6 , size=.5, colour="black") +
  scale_fill_viridis(discrete = T, option = 'magma') +
  theme_ipsum() +
  theme(legend.position="bottom") +
  labs(x="Year", y="Total",
       subtitle = "Percentage Population by Group",
       caption="SeanPJ.com") +
  scale_y_percent()

# arrange them side by side and add a title
grid.arrange(p1,p2, nrow=1,
             top = textGrob("Tidy Tuesday Week 8: W.E.B Du Bois Challenge",
                            gp=gpar(fontsize=20,font=3)))

Bonus animation:

library(gganimate)

ggplot(c, aes(x=year,y=Total,fill=Group))+
  geom_area(alpha=0.6 , size=.5, colour="black") +
  scale_fill_viridis(discrete = T, option = 'magma') +
  theme_ipsum() +
  theme(legend.position="bottom") +
  labs(x="Year", y="Total",
       subtitle = "Total Population by Group",
       caption="SeanPJ.com") +
  scale_y_continuous(labels = comma) +
  transition_reveal(year)