1

I am new to R and have to create a plot like the one shown below. As in the figure taken from Ogai et al. (A Comparison of Techniques for Collecting Skin Microbiome Samples: Swabbing Versus Tape-Stripping, Front Microbiol. 2018;9:2362) I'd like to display the cultured bacterial strains found on skin swabs from different samples in a similar fashion.

Can somebody point me toward the function used to create this plot?

Plot

Martin Gal
  • 16,640
  • 5
  • 21
  • 39
MaxW
  • 19
  • 1
  • 2
    Please share some sample data. Take a look at how to make a [great reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for ways of showing data. – Martin Gal Jul 11 '21 at 17:48

1 Answers1

3

This kind of plot is called waffle chart. You can build one using ggplot2:

library(ggplot2)

set.seed(1024)

df <- data.frame(category = rep(c("Swab", "Tape"), each = 7*11),
                 ID = rep(1:7, 11),
                 Name = rep(c("Staphy", "Cory", "Propioni", "Lacto", "Micro",
                                     "Enteroba", "Bifido", "Bacill", "Acine", "Enteroco",
                                     "Staphy aur"), each=7),
                 detection = sample(c(TRUE, FALSE), 7*11, replace = TRUE))
df

ggplot(df, aes(x = ID, y = Name, fill = detection)) + 
  geom_tile(color = "black", size = 0.5) +
  scale_x_continuous() +
  scale_y_discrete() +
  scale_fill_brewer(palette = "Set1") +
  labs(title="Detection in cultures") + 
  facet_wrap(~ category) +
  theme(plot.title = element_text(size = rel(1.2)))

This gives you

enter image description here

Your data has to be in the right format to work with ggplot2. Take a close look at my sample data.

Martin Gal
  • 16,640
  • 5
  • 21
  • 39