我有一个非常简单的ggplot 2棒棒糖图,来自一些虚拟数据:
mut.df <- data.frame("AA" = c(201, 203, 500, 601),
"Mut" = c("V201L", "R203H", "Q500*", "P601fs"),
"Type" = c("Missense", "Missense", "Nonsense", "Frameshift"),
"Freq" = c(2,3,4,1))
domain.df <- data.frame("Feature" = c("Start", "Dom1", "Dom2", "End"),
"Type" = c("str", "dom", "dom", "str"),
"Start" = c(1, 180, 480, 650),
"End" = c(1, 230, 630, 650))
str.fill <- "#E1E1E1"
str.col <- "#16161D"
dom.fill <- c("Dom2" = "#FF0000", "Dom1" = "#AA4BAB")
dom.col <- c("#16161D")
library(ggplot2)
library(ggrepel)
gp <- ggplot() +
geom_rect(data = subset(domain.df, Type == "str"),
mapping = aes(xmin = min(Start), xmax = max(End), ymin = 0.3, ymax = 0.7),
fill = str.fill,
colour = str.col)
gp <- gp + scale_y_continuous(limits = c(0, 10), breaks = 0:10)
gp <- gp + geom_segment(data = mut.df,
mapping = aes(x = AA, xend = AA, y = 0.7, yend = Freq)) +
geom_point(data = mut.df,
mapping = aes(x = AA, y = Freq, fill = Type),
shape = 21,
size = 2) +
geom_text_repel(data = mut.df,
mapping = aes(x = AA, y = Freq, label = Mut),
bg.colour = "white",
seed = 12345,
nudge_y = 0.25)
gp <- gp + geom_rect(data = subset(domain.df, Type == "dom"),
mapping = aes(xmin = Start, xmax = End, ymin = 0.2, ymax = 0.8, fill = Feature, group = Feature),
fill = dom.fill[subset(domain.df, Type == "dom")$Feature],
colour = dom.col)
gp <- gp +
theme_bw() +
theme(panel.grid.minor = element_blank(),
panel.grid.major.x = element_blank(),
panel.grid.major.y = element_line(linetype = "dotted")) +
labs(x = "AA", y = "Freq", fill = "Mutation")
字符串
我想添加一些智能格式的分支,将前两个棒棒糖分开,而不是将它们堆叠在一起。例如:
有人能建议如何在ggplot 2中实现这一点吗?
欢迎提出任何建议!
2条答案
按热度按时间hfyxw5xn1#
我不知道有任何扩展包允许这种有条件的“分支”类型的回避。我所知道的最接近的事情是将棒棒糖转换为带有手动布局的两元素图。棒棒糖的下端将是固定的,但是如果上端接近AA的另一个值,则它们的位置上会添加一些随机噪声。
geom_edge_elbow
:字符串
请注意,您可能需要运行代码几次,以获得一个您满意的躲避图,因为它是随机发生的。您可以 * 安排它,以便计算躲避,但这将是一个非常复杂的计算,需要选择性随机化(ggrepel使用的),或者某种形式的聚类/圆 Package 算法。除非我要产生很多这样的图,或者有很多冲突的聚类,我可能会坚持上面所示的“随机选择最好的”策略。
rqdpfwrv2#
我试着在没有任何花哨的聚类的情况下做到这一点,只是做了一点间距。我认为这将适用于稀疏的点集,但一旦突变变得非常密集,可能会成为一个问题。
字符串