是否可以将 Joyplot 作为面板添加到包含 ggtree 的绘图中,如 these examples 所示?欢乐图的示例是 here 。
我意识到我可以按照与树尖标签相同的顺序手动放置 Joyplot 的物种标签,但我正在寻找一种自动解决方案。我想自动将 Joyplot 行与树的尖端关联起来,类似于箱线图数据与尖端标签的关联方式。
我认为上面链接中的Guangchuang Yu的例子提供了合适的数据:
require(ggtree)
require(ggstance)
# generate tree
tr <- rtree(30)
# create simple ggtree object with tip labels
p <- ggtree(tr) + geom_tiplab(offset = 0.02)
# Generate categorical data for each "species"
d1 <- data.frame(id=tr$tip.label, location=sample(c("GZ", "HK", "CZ"), 30, replace=TRUE))
#Plot the categorical data as colored points on the tree tips
p1 <- p %<+% d1 + geom_tippoint(aes(color=location))
# Generate distribution of points for each species
d4 = data.frame(id=rep(tr$tip.label, each=20),
val=as.vector(sapply(1:30, function(i)
rnorm(20, mean=i)))
)
# Create panel with boxplot of the d4 data
p4 <- facet_plot(p1, panel="Boxplot", data=d4, geom_boxploth,
mapping = aes(x=val, group=label, color=location))
plot(p4)
是否可以创建一个 Joyplot 来代替箱线图?
下面是上面演示数据集 d4 的快速 Joyplot 代码:
require(ggjoy)
ggplot(d4, aes(x = val, y = id)) +
geom_joy(scale = 2, rel_min_height=0.03) +
scale_y_discrete(expand = c(0.01, 0)) + theme_joy()
我是 ggplot2、ggtree 和 ggjoy 的新手,所以我完全不知道如何开始这样做。
最佳答案
注意:截至 2017 年 9 月 14 日,ggjoy
package has been deprecated 。相反,请使用 ggridges
package 。要使下面的代码与 ggridges
配合使用,请使用 geom_密度_ridges
而不是 geom_joy
。
看起来你可以在facet_plot
中将geom_boxplot
替换为geom_joy
:
facet_plot(p1, panel="Joy Plot", data=d4, geom_joy,
mapping = aes(x=val, group=label, fill=location), colour="grey50", lwd=0.3)
如果您是 ggplot2 新手,the visualization chapter of Data Science with R (ggplot2作者的一本开源书籍)应该对学习基础知识有帮助。
ggjoy
和 ggtree
扩展了 ggplot2 的功能。当这样的扩展做得好时,“显而易见”的事情(就通常的 ggplot“图形语法”而言)通常会起作用,因为扩展包的编写方式试图忠实于底层的 ggplot2 方法。
在这里,我的第一个想法是用 geom_joy
替换 geom_boxplot
,结果证明可以完成工作。每个geom
只是一种不同的数据可视化方式,在本例中是箱形图与密度图。但绘图的所有其他“结构”保持不变,因此您只需更改几何图形并获得遵循相同轴顺序、颜色映射等的新绘图。一旦您获得了一些经验,这将更有意义ggplot2 图形语法。
这是左侧图的稍微不同的标记方法:
p1 = ggtree(tr) %<+% d1 +
geom_tippoint(aes(color=location), size=6) +
geom_tiplab(offset=-0.01, hjust=0.5, colour="white", size=3.2, fontface="bold")
facet_plot(p1, panel="Joy Plot", data=d4, geom_joy,
mapping = aes(x=val, group=label, fill=location), colour="grey40", lwd=0.3)
更新:这是对您询问如何在两个方面面板中获得相同的自定义颜色的评论的回应。以下是使用您问题中的示例数据执行此操作的代码:
p1 = ggtree(tr) %<+% d1 +
geom_tippoint(aes(color=location), size=5) +
geom_tiplab(offset=-0.01, hjust=0.5, colour="white", size=3, fontface="bold") +
scale_colour_manual(values = c("grey", "red3", "blue")) +
scale_fill_manual(values = c("grey", "red3", "blue"))
facet_plot(p1, panel="Joy Plot", data=d4, geom_joy,
mapping = aes(x=val, group=label, fill=location), colour="grey40", lwd=0.3)
关于r - ggjoy 方面与 ggtree,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45384281/