我试图用ggplot2对齐以将一个绘制在另一个之上。两幅图中的数据显示了x
属性的每个类别。
您可以在此处下载示例数据:https://gist.github.com/itsvenu/97605e07b626d04f290fb39af7ccbc3a
以下是我编写的用于生成图形的代码
all_sb <- unique(mp_mb_sampledata$SB)
cl_min <- seq(from = 0.5, to = max(as.numeric(as.factor(all_sb))), by = 1)[1:length(all_sb)]
cl_max <- seq(from = 1.5, to = max(as.numeric(as.factor(all_sb))) + 0.5, by = 1)[1:length(all_sb)]
shading_cols <- data.frame(min = cl_min,
max = cl_max,
col = rep(c(0, 1), length(cl_min)))
## top panel
p1 <- ggplot()+
ggbeeswarm::geom_quasirandom(data = mp_mb_sampledata, aes(x = SB, y = mut_per_mb))+
geom_rect(data = shading_cols,
aes(xmin = min, xmax = max, ymin = -Inf, ymax = Inf,
fill = factor(col), alpha = 0.01))+
scale_fill_manual(values = c("white", "gray"), guide = FALSE)+
ggbeeswarm::geom_quasirandom(data = mp_mb_sampledata, aes(x = SB, y = mut_per_mb))+
coord_trans(y="log2")+
theme_classic(base_size = 18)+
xlab("")+ylab("")+
theme(legend.position = "none",
axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
axis.line.x = element_blank(),
axis.text = element_text(color = "black"))
## bottom panel
p2 <- ggplot()+
geom_bar(data = req_bc, aes(x = SB, y = Freq, fill = MutationType), stat = "identity")+
geom_rect(data = shading_cols,
aes(xmin = min, xmax = max, ymin = -Inf, ymax = Inf,
fill = factor(col), alpha = 0.01))+
scale_fill_manual(values = color_pal)+
geom_bar(data = req_bc, aes(x = SB, y = Freq, fill = MutationType), stat = "identity")+
theme_classic(base_size = 18)+
theme(axis.text = element_text(color = "black"),
plot.margin = unit(c(0.01, 5.5, 5.5, 5.5), "pt"))+
ylab("Fraction")+
scale_y_continuous(breaks = c(0, 0.50, 1), labels = c(0, 0.50, 1))+
ggpubr::rotate_x_text(angle = 90, hjust = 1)
## both in one
p1 + p2 + patchwork::plot_layout(ncol = 1)
那产生了
我想在同一行上排列顶部和底部阴影。出于某种原因,y轴和第一个蜂群点之间会出现间隔。我尝试过expand
的论点,但没有任何改变。
任何帮助将不胜感激。
谢谢。