user2117258
user2117258

Reputation: 515

Using ggplot2 to plot multiple lines of different lengths

I have a 2228 by 2 data.frame that looks something like this:

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

> 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. I am able to accomplish this by manually grep'n out each library and plotting:

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.

Upvotes: 1

Views: 2231

Answers (1)

shrgm
shrgm

Reputation: 1344

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()

enter image description here

Upvotes: 1

Related Questions