[英]I'm having trouble using ggplot2 to reproduce a violin plot
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.我的数据包括每个样本的几个基因的突变组合、计数和筛选细菌的抗生素的 MIC(这些是离散的)。
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).对于每个 MIC 类别(在 log2 尺度上有 8 个),这种情况一直持续下去。
I am attempting to create a violin plot like this (source: https://www.thelancet.com/journals/laninf/article/PIIS1473-3099(18)30225-1/fulltext ):我正在尝试创建这样的小提琴图(来源: 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)))
I couldn't reproduce your plotting code, as it lacks the column mic
.我无法重现您的绘图代码,因为它缺少列
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).当然你可以玩弄格式(
geom_sina
调用中点的geom_sina
是最明显的)。
# 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:其中产生:
A long time has passed, and now there is an R
package called ggupset
which makes charts similar to those linked in The Lancet.很长一段时间过去了,现在有一个名为
ggupset
的R
包,它可以制作类似于 The Lancet 中链接的图表。
https://github.com/const-ae/ggupset https://github.com/const-ae/ggupset
Plot a combination matrix instead of the standard x-axis and create UpSet plots with ggplot2.
绘制组合矩阵而不是标准 x 轴,并使用ggplot2创建UpSet 图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.