我在使用ggplot2再现小提琴图时遇到问题

时间:2019-06-27 20:53:33

标签: r ggplot2

我的数据包括每个样本几个基因的突变,计数和筛选细菌的抗生素的MIC(这些是离散的)的组合。

这是玩具数据:

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")

对于每个MIC类别,此操作不断进行(log2等级上有8个)。

我正在尝试创建一个像这样的小提琴图(来源:https://www.thelancet.com/journals/laninf/article/PIIS1473-3099(18)30225-1/fulltext ): enter image description here

我尝试编写一些代码,但距离不远:

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

这是该情节的样子: enter image description here

我可以轻松地创建一个计数表,但我希望具有一些视觉效果,例如下面产生的图像。有人可以指引我正确的方向吗?

enter image description here

p <- ggplot(mydata, aes(x = combo, y=mic)) 
p + geom_violin()  + geom_jitter(height = 0, width = 0.1, aes(color=factor(combo)))

2 个答案:

答案 0 :(得分:2)

我无法复制您的绘图代码,因为它缺少列mic。但是,我认为这是您想要的:

# 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)

当然,您可以使用这种格式(geom_sina调用中点的缩小最明显)。

enter image description here

编辑

# 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))

哪个会产生:

enter image description here

答案 1 :(得分:0)

很长一段时间过去了,现在有一个名为R的{​​{1}}软件包,它使图表类似于《柳叶刀》中链接的那些图表。

https://github.com/const-ae/ggupset

绘制组合矩阵而不是标准x轴,并使用ggplot2创建 UpSet图

ggupset example violin plot from github