Plotting non-numerical categorical data in ggplot2

Viewed 195

I am looking for ideas on how to plot a data set of 100 samples in ggplot structured as follows:

df <- data.frame(sample = c(1:5), 
                 group = c("class 1", "class 1", "class 3", "class 3", "class 3"),
                 cat_1 = c("D", "Mj", "Mj", "Tr", "Mn"),
                 cat_2 = c("D", "-", "Mn", "Tr", "Mj"),
                 cat_3 = c("Mn", "Tr", "D", "Tr", "Mn"),
                 cat_4 = c("-", "-", "Mj", "-", "Mn"))

The number of samples per group varies. I have 20 different categories (i.e. cat_1 ... cat_20) and 6 groups in my actual data.

D is greater than 50% of a particular category. Mj is between 5% and 50%. Mn is between 1% and 5%, Tr is less than 1%. Not observed in the sample is denoted "-".

Plot non-numeric data provides some ideas. Essentially I want to show the breakdown of categories by group. I am seeking inspired plots for a scientific report. I was thinking of a doughnut plot for each group with the number of samples in each group in the middle of the doughnut. However, this is hard to interpret at one level. I need to honour the data.

This is the best I can do so far:

df %>% 
  pivot_longer(c(3:6), names_to = "category") %>%
  group_by(group, category) %>%
  mutate(count = n(),
         value = ifelse(value == "-", NA, value)) %>%
  ungroup() %>%
  drop_na() %>%
  ggplot(aes(value, fill = category)) +
  geom_bar() +
  xlab("abundance") +
  facet_wrap(~ group)`
1 Answers
df %>% 
  pivot_longer(c(3:6), names_to = "category") %>%
  group_by(group, category) %>%
  mutate(count = n(),
         value = ifelse(value == "-", NA, value)) %>%
  drop_na() %>%
  ggplot(aes(value, fill = category)) +
  geom_bar(colour = "black", lwd = 0.2) +
  xlab("abundance") +
  facet_wrap(~ group, ncol = 1) + 
  theme_bw()

Related