gpt4 book ai didi

r - 使用 ggplot2 连接嵌套数据组中图形上的点

转载 作者:行者123 更新时间:2023-12-04 09:33:19 27 4
gpt4 key购买 nike

我在解决如何在嵌套结构中的 ggplot 上的点之间画线时遇到问题。

我拥有的是一组由 3 个不同的嵌套组分解的数据。然后绘制,第一组使用 facet 来配对子组(Mutation),第二组然后将数据分成初始实验(HiSeq)和复制实验 (MiSeq),而第三组 (Grouping) 根据它们来自的样本类型对点进行着色和整形。

但我遇到困难的地方是,我想通过一条线将一对(突变)中的 2 个点 (HiSeq/Miseq) 链接起来,以便轻松地锻炼链接的两个点。我做了一个可以看到的模型:

here, which shows roughly what I want to try and do但是,我无法弄清楚如何在两个组 (HiSeq/Miseq) 之间执行此操作,同时保持在顶级组(突变)中。

有没有人有办法解决这个问题?数据片段和我用来构建当前图表的代码如下所示。它可能最终会变得凌乱而无法呈现,但解决起来会很有用。

ggplot(test,aes(y=AR,x=Type,fill=Grouping,colour=Grouping,shape=Grouping)) + 
geom_point(binaxis='y',stackdir='center',position=position_dodge(width = 0.2),size=7) +
facet_wrap(~ Mutation,nrow=1) +
xlab("") +
ylab("Allelic Ratio") +
theme_minimal(base_size=20)

示例数据:

structure(list(Mutation = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("chr1:51910329",
"chr1:72951069"), class = "factor"), Type = structure(c(1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L), .Label = c("HiSeq", "MiSeq"), class = "factor"), Grouping = structure(c(3L,
3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L,
2L, 2L, 1L, 3L, 3L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L,
1L, 1L, 1L), .Label = c("Offspring (M)", "Offspring (P)", "Proband"
), class = "factor"), Name = c(288458773L, 288458773L, 423125012L,
423125012L, 344991226L, 344991226L, 422977809L, 422977809L, 420753074L,
420753074L, 351142406L, 351142406L, 422743921L, 422743921L, 425596544L,
425596544L, 422595517L, 422595517L, 477342393L, 477342393L, 288458773L,
288458773L, 423125012L, 423125012L, 344991226L, 344991226L, 422977809L,
422977809L, 420753074L, 420753074L, 351142406L, 351142406L, 477342393L,
477342393L, 480773638L, 480773638L), AR = c(0.38, 0.3, 0, 0,
0.375, 0.545, 0.41, 0.388, 0.35, 0.42, 0, 0, NA, 0.59, NA, 0,
0, 0.05, 0, 0, 0.1875, 0.078379734, 0.4, 0.505582473, 0, 0.002394493,
0, 0.002023547, 0, 0.001600569, 0.6, 0.510240797, 0.6, 0.490997813,
0, 0.001785424)), .Names = c("Mutation", "Type", "Grouping",
"Name", "AR"), class = "data.frame", row.names = c(NA, -36L))

最佳答案

我认为这可能是您想要的——查看 geom_line 并了解它的 group 美学:

ggplot(df, aes(x = Type, y = AR, fill = Grouping, color = Grouping, shape = Grouping)) +
geom_point(size = 5) +
geom_line(aes(group = Name)) +
facet_wrap(~ Mutation)

Plot

关于r - 使用 ggplot2 连接嵌套数据组中图形上的点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37810473/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com