Is there an R function to create a graph that displays multiple variables on the y axis and multiple variables on the x axis?

Viewed 73

I try to create a graph that is similar to the graph created by Kevin Munger (see below).

I can't find anything close to this graphic on R Graph gallery. Therefore, I just wanted to ask you guys if there is a way to do that. It would mean to have multiple variables (like the social media platforms) on the x-axis and variables with their expressions on the y-axis. As far as I know you can only create graphs with ggplt() that display multiple variables on one axis but not on both.

Can you see an opportunity to create a similar plot or do you maybe have an example?

I was thinking to maybe use this one: https://r-graph-gallery.com/web-horizontal-barplot-with-labels-the-economist.html

Best, Gertie

enter image description here

1 Answers

Here's an attempt to reproduce something similar using the titanic dataset:

library(tidyverse)
library(titanic)

#Prepare the data set
titanic_train %>% 
  select(Survived, Pclass, Sex) %>%
  mutate(Survived = ifelse(Survived == 1, "Survived", "Not survived"),
         Sex = ifelse(Sex == "male", "Male", "Female")) %>% 
  pivot_longer(-Pclass) %>% 
  group_by(Pclass, name, value) %>% 
  summarise(count = n()) %>% 
  mutate(perc = count/sum(count)) %>% 
  ungroup() %>% 
  
# The plot
  ggplot(aes(x = value, y = perc * 100, fill = name, label = round(perc*100, 0))) + 
  geom_bar(stat = "identity") +
  geom_text(hjust = -.2, size = 5) +
  labs(title = "Title", subtitle = "Subtitle") +
  coord_flip(clip = "off") +
  facet_grid(~ Pclass, labeller = label_both) +
  theme_minimal() +
  scale_fill_brewer(palette = 7, guide = "none") +
  theme(axis.title.y = element_blank(),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.text.y = element_text(hjust = 0, size = 12),
        panel.grid = element_blank(),
        plot.title = element_text(size = 22),
        plot.subtitle = element_text(size = 15),
        strip.text = element_text(face = "bold", size = 12),
        plot.title.position = "plot")

enter image description here

Related