Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

(Tidy Tuesday is a project to supply weekly data sets for R users to practice their coding skills on. You can find full details here.)

A little late, but I decided to have a go at Tidy Tuesday with the recent Claremont Run dataset, looking at Chris Claremont’s 16 year run writing for the Uncanny X-men comic.

Looking at the characters dataset provided, that describe some of the events that occur to each character in an issue, it seemed like being an X-Person was a pretty rough gig. I wondered, who got dealt the harshest hand during the entire Claremont run?

Poor Storm.

This also gave me a chance to (very slightly) play with the new across (docs) function in the latest dplyr release. I’ve seen a few blog posts from excited R users around singing it’s praises, so it was good to at least touch it, even if I clearly haven’t made the most of it here.

I also played with adorn_totals from the Janitor package for the first time, as well as adding an image to a plot with Cowplot’s draw_image. Random new functions – always my favourite thing about working with R.

Code:

library(tidyverse)
library(janitor)
library(tidytuesdayR)
library(ggthemr)
library(cowplot)
library(here)

# set theme with ggthemr
ggthemr('fresh')

# import tidy tuesday data
characters <- data\$characters

# summarise total instances the state occurred on each character across run
char_states_totals <- characters %>%
select(-issue) %>%
group_by(character) %>%
summarise(across(is.numeric, sum)) %>%
ungroup() %>%
select(1:4, 6, 8) %>%
janitor::untabyl() %>%
slice_max(total, n = 10) %>%
select(character, total, everything()) %>%
pivot_longer(3:7, names_to="state", values_to = "count") %>%
separate(character, sep = " = ", into = c("codename", "name")) %>%
codename == "Marvel Girl/Phoenix" ~ "Jean Grey",
TRUE ~ codename))

# create stacked column plot
plot <- char_states_totals %>%
mutate(state = str_replace_all(state, "_", " "),
state = str_replace_all(state, "subject", "subjected"),
state = str_to_sentence(state)) %>%
mutate(state = fct_reorder(state, count),
codename = fct_reorder(codename, total)) %>%
ggplot(aes(codename, count, fill = state)) +
geom_col() +
coord_flip() +
labs(title = "Which X-Men character had it worst?",
subtitle = "During Chris Claremont's 1975-1991 run on Uncanny X-Men",
x = "",
y = "No. of Occurrences",
fill = "")

# add storm image to plot
cowplot::ggdraw(plot) +
cowplot::draw_image(here("xmen", "storm.jpg"), scale = .3, x = .37, y = .3)