使用ggplot2在嵌套数据组中的图形上连接点 [英] Connecting points on a graph within nested groups of data with ggplot2

查看:162
本文介绍了使用ggplot2在嵌套数据组中的图形上连接点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm having a problem working out how to draw lines between points on a ggplot that are in a nested structure.

我所拥有的是一组由3个不同的嵌套组细分的数据. 然后将其绘制,将第一组与构面一起使用以将子组(Mutation)配对,然后将第二组将数据拆分为初始实验(HiSeq)和复制实验(MiSeq),而第三组(Grouping)通过点的颜色和形状对它们进行采样.

What I have is a set of data that is broken down by 3 different nested groups. Which are then plotted, the first group is used with facet to pair the subgroups (Mutation), the second group then splits the data into the initial experiment (HiSeq) and the replication experiment (MiSeq), while the third group (Grouping) colors and shapes the points by the sample type they are from.

我被卡住的地方是,我想通过一条线将一对(变异)中的2个点(HiSeq/Miseq)连接起来,以便于锻炼两个相连的点.我做了一个模拟,可以看到:

Where I have gotten stuck though is I'd like to link the 2 points (HiSeq/Miseq) within an pair (mutation) via a line to make it easy to workout which two are linked. I've made a mock up which can be seen:

但是,我无法确定如何在两个小组(HiSeq/Miseq)中做到这一点,同时又停留在顶级小组(Mutation)中.

However I'm unable to work out how to do this across the two groups (HiSeq/Miseq) while staying within the top level group (Mutation).

有人对此有解决方案吗?下面显示了我用于构建当前图形的数据片段和代码.表现得很杂乱,但解决起来还是很有用的.

Does any one have a solution to this? A fragment of the data and the code I'm using to build the current graph can be seen below. It may end up being to messy to be presentable but it would be useful to solve.

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美学:

I think this may be what you want -- look into geom_line and understanding its group aesthetic:

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)

这篇关于使用ggplot2在嵌套数据组中的图形上连接点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆