r - ggjoy 方面与 ggtree

标签 r ggplot2 data-visualization ggtree ggridges

是否可以将 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)

这会产生下面的图: demo ggtree plot

是否可以创建一个 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()

结果是: demo joyplot

我是 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) 

enter image description here

如果您是 ggplot2 新手,the visualization chapter of Data Science with R (ggplot2作者的一本开源书籍)应该对学习基础知识有帮助。

ggjoyggtree 扩展了 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) 

enter image description here

更新:这是对您询问如何在两个方面面板中获得相同的自定义颜色的评论的回应。以下是使用您问题中的示例数据执行此操作的代码:

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) 

enter image description here

关于r - ggjoy 方面与 ggtree,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45384281/

相关文章:

r - 计算自上次事件以来耗时

regex - 如何用空格分隔千位

r - ggplot2交错轴标签

r - flextable:嵌入 ggpplots 使用来自 2 列的数据和列表(用于比较)作为每行的单个图

python - Bokeh :如何添加连接点的垂直线

r - 创建 N 个无间隙的随机整数

r - 如何在 R 中的错​​误消息中使用特殊字符和颜色?

R:将条形图与填充有匹配条形颜色的线条连接起来

c# - 我可以在 MSChart 的饼图标签上同时显示标签和值吗

javascript - 在 React 中使用 select 标签并使用状态在 React Charts 的数据集之间切换