简体   繁体   English

使用ggplot2绘制多条不同长度的线

[英]Using ggplot2 to plot multiple lines of different lengths

I have a 2228 by 2 data.frame that looks something like this: 我有一个2228 x 2 data.frame,看起来像这样:

> head(diffSums, 10)
                               nTranscripts        Library
D6_NoSort_2250b_ATTATACGCCCC          63963   NoSort_2250b
D6_EcadSort_6000b_CCACACCCAGCC       193050 EcadSort_6000b
D6_EcadSort_2250b_CCGATGATTAGC        77631 EcadSort_2250b
D6_EcadSort_2250b_TGTCTGCTTTAG       106599 EcadSort_2250b
D6_EcadSort_2250b_TTCACAAGTTTC        88775 EcadSort_2250b
D6_EcadSort_6000b_CCATATCCAGCT        97861 EcadSort_6000b
D6_EcadSort_6000b_CAACGACTTAGG        91813 EcadSort_6000b
D6_EcadSort_2250b_AGTGAACAGGCG        80503 NoSort_2250b
D6_EcadSort_2250b_AAGCGGCTGCGC        93326 EcadSort_2250b
D6_EcadSort_2250b_CGTTTCACTTCG        72013 NoSort_2250b

where the number of entries per diffSums$Library vary: 每个diffSums$Library的条目数有所不同:

> table(diffSums$Library)

  EcadSort_2250b EcadSort_2250x8b   EcadSort_6000b EcadSort_6000x3b     NoSort_2250b  NoSort_2250x23b   NoSort_2250x8b     NoSort_6000b   NoSort_6000x3b 
             136              321              131              422              269               72              452              192              233 

I'd like to plot a line for each library on the same plot using ggplot2. 我想使用ggplot2在同一图上为每个库绘制一条线。 I am able to accomplish this by manually grep'n out each library and plotting: 我可以通过手动grep'n每个库并作图来完成此操作:

ggplot() + 
  geom_line(data=diffSums[grep("EcadSort_6000x3b", rownames(diffSums)),], aes(x=seq(as.vector(table(diffSums$Library))[4]), y=sort(nTranscripts, decreasing=TRUE)), color='green') +
  geom_line(data=diffSums[grep("NoSort_2250b", rownames(diffSums)),], aes(x=seq(as.vector(table(diffSums$Library))[5]), y=sort(nTranscripts, decreasing=TRUE)), color='blue')

... but I know there must be an easier way! ...但是我知道一定有更简单的方法! Any help would be greatly appreciated. 任何帮助将不胜感激。

It's still not possible to reproduce the graph that you generated using the data and code provided, but I think this is what you're looking for: 仍然无法复制使用提供的数据和代码生成的图形,但是我认为这是您想要的:

library(dplyr)

diffSums <- diffSums %>%
    group_by(Library) %>%
    arrange(-nTranscripts) %>%
    mutate(numLib = seq_len(n()))

ggplot(diffSums, aes(numLib,nTranscripts,colour = Library)) + geom_line()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM