Reputation: 309
My data includes a combination of mutations for several genes per sample, the counts, and the MIC (these are discrete) for the antibiotic the bacteria was screened on.
Here is toy data:
structure(list(count = c(4L, 26L, 96L, 93L, 2L, 22L), combo = structure(c(22L,
19L, 2L, 2L, 20L, 19L), .Label = c("HFALSEAIVTANGGAL", "HFALSEAIVTANGGAP",
"HFALSEAIVTANGGTP", "HFALSEAIVTANSGAP", "HFALSEAIVTAYGGTP", "HFALSEAIVTTNSGAP",
"HFALSEAIVTVNGGAL", "HFALSEAIVTVNGGAS", "HFALSEAIVTVNSGAP", "HFALSEAMTTAYGSAA",
"HFALSEAMTTAYGSAP", "HFALSEAMTTAYGSTP", "HTRUEAIVSANGGAP", "HTRUEAIVTANGGAP",
"HTRUEAIVTANSGAP", "HTRUEAIVTTNSGAP", "HTRUEAMTTAYGSAP", "YFALSEAIVTANGGAL",
"YFALSEAIVTANGGAP", "YFALSEAIVTANGGAS", "YFALSEAIVTANSGAP", "YFALSEAIVTTNGGAL",
"YFALSEAIVTTNGGAP", "YFALSEAIVTTNSGAP", "YFALSEAIVTVNGGAL", "YFALSEAMTTANGGAP",
"YFALSEAMTTANGGTP", "YFALSEAMTTAYGSAA", "YFALSEAMTTAYGSAP", "YFALSEAMTTAYGSAS",
"YFALSEAMTTAYGSTP", "YFALSENFNFNFNFNFNFNFNFNFNF", "YTRUEAIVTANGGAL",
"YTRUEAIVTANGGAP", "YTRUEAMTTAYGSAP"), class = "factor")), row.names = c(NA,
6L), class = "data.frame")
This goes on and on for each MIC category (there are 8 on a log2 scale).
I am attempting to create a violin plot like this (source: https://www.thelancet.com/journals/laninf/article/PIIS1473-3099(18)30225-1/fulltext
):
I attempted to write some code but didn't get very far:
p <- ggplot(mydata, aes(factor(combo), y=mic)) +
geom_violin() +
geom_boxplot(width=.01, outlier.size=0, fill="grey50") +
stat_summary(fun.y=median, geom="point", fill="white", shape=21, size=4)
p
Here is what that plot looks like:
I can easily create a count table but I was hoping to have some visual like the image produced below. Can someone steer me in the right direction?
p <- ggplot(mydata, aes(x = combo, y=mic))
p + geom_violin() + geom_jitter(height = 0, width = 0.1, aes(color=factor(combo)))
Upvotes: 1
Views: 476
Reputation: 5398
A long time has passed, and now there is an R
package called ggupset
which makes charts similar to those linked in The Lancet.
https://github.com/const-ae/ggupset
Plot a combination matrix instead of the standard x-axis and create UpSet plots with ggplot2.
Upvotes: 1
Reputation: 6496
I couldn't reproduce your plotting code, as it lacks the column mic
. However, I think this is what you're looking for:
# load libraries
library(ggplot2)
library(ggforce)
# make toy data
set.seed(1); a <- data.frame(kind = sample(letters[1:10], 500, TRUE), value = sample(1:20, 500, TRUE))
# plot
ggplot(a, aes(x = kind, y = value, colour = kind))+geom_violin()+geom_sina(size = 2.1)
Of course you can play with the format (the shize of the points in the geom_sina
call is the most evident).
# redefine the first plot, removing the legend:
p1 <- ggplot(a, aes(x = kind, y = value, colour = kind))+
geom_violin()+
geom_sina(size = 1.1)+
theme(legend.position = "none")
# Define toy data for the lower plot:
library(data.table)
set.seed(1)
Genes <- data.table(gene = sample(LETTERS[1:10], 20, TRUE),
n = sample(1:10, 20, TRUE))
# add a coloring variable
Genes[, coloring := cut(n, 3, labels = 1:3)]
# plot the lower plot
p2 <- ggplot(Genes, aes(n, gene, colour = coloring))+
geom_point(size = 2.8)+
theme(axis.title = element_blank(),
axis.text.x = element_blank(),
legend.position = "none",
axis.ticks.x = element_blank(),
panel.background = element_blank())
# put both plots in the canvas:
library(patchwork)
p1+
p2+
plot_layout(ncol = 1, heights = c(.8, .2))
Which produces:
Upvotes: 4