#r #ggplot2
#r #ggplot2
Вопрос:
У меня есть матрица с данными, я подгоняю glm к срезам матрицы, строю гистограмму и строю расчетную плотность в соответствии с параметром glm. Затем я хочу сохранить все графики в один PDF-файл.
n_clusters <- length(levels(SNF_W_louvain))
n_features <- dim(filtered_ADT_counts)[1]
all_plots <- vector("list", n_clusters * n_features)
i <- 1
for(current_cluster in 1:n_clusters) {
for(current_marker in 1:n_features) {
current_data <- data.frame(counts = filtered_ADT_counts[current_marker, SNF_W_louvain == current_cluster])
fit_poisson <- glm(counts ~ 1, current_data, family = "poisson")
fit_info <- paste0("AIC: ", round(fit_poisson$aic, 2), " (Pois)")
plot_title <- paste0("Protein: ", rownames(filtered_ADT_counts)[current_marker], " Cluster: ", current_cluster)
pred_density_pois <- dpois(current_data$counts, lambda = exp(fit_poisson$coefficients[1]))
current_plot <- current_data %>%
ggplot()
geom_histogram(aes(x = counts, y=..density..), fill = "white", color='red')
geom_line(aes(x = counts, y = pred_density_pois), color= 'blue')
ggtitle(plot_title)
annotate("text", x = Inf, y = Inf, hjust = 1, vjust = 2, label = fit_info, size = 3)
all_plots[[i]] <- current_plot
i <- i 1
}
}
ggsave(
filename = "all_plots.pdf",
plot = marrangeGrob(all_plots, nrow=4, ncol=2),
units = "mm",
width = 210,
height = 297
)
Я получаю следующую ошибку:
Aesthetics must be either length 1 or the same as the data (30): y
Backtrace:
1. ggplot2::ggsave(...)
3. gridExtra::marrangeGrob(all_plots, nrow = 4, ncol = 2)
6. base::lapply(grobs[toconv], ggplot2::ggplotGrob)
7. ggplot2:::FUN(X[[i]], ...)
10. ggplot2:::ggplot_build.ggplot(x)
11. ggplot2:::by_layer(function(l, d) l$compute_aesthetics(d, plot))
12. ggplot2:::f(l = layers[[i]], d = data[[i]])
13. l$compute_aesthetics(d, plot)
14. ggplot2:::f(..., self = self)
15. ggplot2:::check_aesthetics(evaled, n)
Когда я удалю эту строку:
geom_line(aes(x = counts, y = pred_density_pois), color= 'blue')
это действительно работает.
Любые советы приветств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, 1L, 1L,
0L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 0L,
0L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 0L, 1L, 1L, 0L, 1L, 0L, 1L, 0L,
1L, 0L, 1L, 0L, 1L, 0L, 1L, 1L, 0L, 1L, 0L, 0L, 1L), p = c(0L,
0L, 1L, 1L, 2L, 2L, 4L, 4L, 4L, 4L, 5L, 6L, 8L, 8L, 9L, 10L,
12L, 14L, 16L, 18L, 19L, 20L, 20L, 22L, 23L, 25L, 26L, 27L, 29L,
31L, 32L, 33L, 34L, 35L, 36L, 38L, 39L, 41L, 42L, 43L, 45L, 46L,
48L, 50L, 50L, 51L, 52L, 53L, 54L, 55L, 56L, 57L, 58L, 58L, 59L,
61L, 62L, 63L, 64L, 66L, 66L, 68L, 69L, 70L, 70L, 72L, 74L, 75L,
76L, 78L, 80L, 82L, 84L, 85L, 86L, 86L, 87L, 89L, 90L, 90L, 91L,
92L, 93L, 94L, 96L, 98L, 98L, 100L, 101L, 102L, 104L, 106L, 107L,
109L, 111L, 111L, 112L, 112L, 113L, 114L, 115L, 117L, 118L, 119L,
120L, 122L, 124L, 125L, 127L, 128L, 129L, 131L, 132L, 132L, 133L,
134L, 136L, 137L, 137L, 138L, 138L, 139L, 141L, 143L, 145L, 147L,
148L, 150L, 152L, 153L, 154L, 154L, 156L, 158L, 159L, 161L, 161L,
163L, 165L, 166L, 167L, 168L, 169L, 171L, 173L, 175L, 176L, 178L,
179L, 181L, 183L, 184L, 185L, 186L, 187L, 189L, 190L, 192L, 194L,
195L, 196L, 197L, 199L, 200L, 201L, 203L, 204L, 205L, 205L, 206L,
206L, 207L, 208L, 210L, 211L, 212L, 213L, 214L, 215L, 216L, 216L,
218L, 219L, 220L, 220L, 221L, 223L, 223L, 225L, 226L, 227L, 228L,
230L, 231L, 233L, 234L, 236L, 237L, 238L, 239L, 239L, 241L, 242L,
242L, 244L, 245L, 246L, 248L, 249L, 251L, 252L, 254L, 255L, 256L,
257L, 258L, 259L, 261L, 263L, 265L, 265L, 266L, 267L, 269L, 271L,
272L, 274L, 276L, 278L, 278L, 279L, 281L, 282L, 284L, 286L, 287L,
289L, 290L, 292L, 293L, 294L, 295L, 296L, 297L, 298L, 298L, 299L,
301L, 301L, 302L, 304L, 305L, 307L, 307L, 308L, 310L, 311L, 311L,
312L, 313L, 314L, 315L, 317L, 318L, 320L, 321L, 321L, 322L, 323L,
325L, 327L, 328L, 329L, 331L, 333L, 334L, 336L, 336L, 338L, 339L,
340L, 342L, 344L, 346L, 347L, 347L, 349L, 351L, 353L, 354L, 355L,
355L, 356L, 356L, 357L, 359L, 360L, 362L, 364L, 366L, 367L, 367L,
369L, 371L, 372L, 373L, 375L, 377L, 379L, 380L, 382L, 383L, 385L,
386L, 388L, 389L, 390L, 391L, 392L, 394L, 394L, 395L, 397L, 398L,
400L, 402L, 403L, 404L, 404L, 406L, 407L, 408L, 409L, 411L, 413L,
414L, 415L, 416L, 417L, 419L, 420L, 421L, 423L, 425L, 426L, 427L,
427L, 428L, 429L, 429L, 431L, 433L, 434L, 436L, 437L, 439L, 440L,
442L, 444L, 445L, 446L, 447L, 448L, 450L, 452L, 454L, 456L, 458L,
459L, 460L, 461L, 463L, 464L, 466L, 467L, 469L, 470L, 472L, 473L,
474L, 475L, 476L, 477L, 478L, 479L, 479L, 481L, 482L, 484L, 485L,
486L, 487L, 489L, 491L, 492L, 494L, 495L, 496L, 498L, 499L, 501L,
502L, 503L, 505L, 506L, 507L, 508L, 509L, 510L, 511L, 513L, 515L,
517L, 519L, 521L, 522L, 523L, 523L, 525L, 525L, 527L, 529L, 530L,
532L, 533L, 534L, 535L, 537L, 539L, 541L, 543L, 544L, 545L, 546L,
548L, 548L, 549L, 551L), Dim = c(2L, 437L), Dimnames = list(c("B220 (CD45R)",
"B7-H1 (PD-L1)"), c("GATCGCGGTTATCGGT", "GGCTGGTAGAGGTTAT", "ACAGCTACAGATCGGA",
"CAACTAGCAGCTCGAC", "TCTCATACATCGATGT", "AGCATACTCTCCCTGA", "CAACCTCCAGTACACT",
"TCGGGACAGGATGTAT", "TCTCTAATCAGAGCTT", "CAGAATCTCCGGCACA", "GTTAAGCTCGAATGGG",
"TACTTACAGTTGTCGT", "CTAAGACTCTTCTGGC", "GTAGGCCCACTGTCGG", "CGATGGCCAAACAACA",
"GACGGCTGTGATGTCT", "TGGTTCCTCAACACTG", "CCTAGCTGTTACGGAG", "CAGCCGAAGACTACAA",
"TGACAACTCTCTGCTG", "GTTCATTTCCCACTTG", "GCATGATAGGTGTTAA", "TTCTCAATCTTTCCTC",
"GCACATATCTCTAAGG", "TTGTAGGTCCGTCATC", "TCATTACGTCTCAACA", "AGCTCCTTCACGCGGT",
"ACTGATGAGATACACA", "CAACTAGAGTACGACG", "CAAGTTGGTTTAGGAA", "TGAGCATTCACGAAGG",
"CCATTCGCAGAGTGTG", "CTACACCGTAAGTGGC", "CGAGCACCATTAGCCA", "CGTTCTGCATCCAACA",
"GGCTCGAGTCCTCTTG", "GCCTCTACATGGTCTA", "GCATACAGTAGCGTGA", "GAACGGAGTCAACATC",
"AGTCTTTAGATCCCGC", "AGACGTTAGTCTTGCA", "GGGTCTGTCGTAGGAG", "AAGGTTCTCTTGAGAC",
"GTGAAGGTCACATGCA", "TAAACCGAGAGTCGGT", "TAGTTGGAGGCCCTCA", "TAGTGGTAGTCTCCTC",
"TTAGGCAGTTGCTCCT", "CGGTTAAGTTCCTCCA", "TTATGCTTCTGTGCAA", "ATCCACCCACATTCGA",
"TGCACCTCAGAGTGTG", "GGGTTGCCACTGCCAG", "AGTCTTTCATCGGTTA", "AGTGAGGTCGATGAGG",
"GCGCAGTGTCGAATCT", "CAGCTGGGTAATCACC", "GTGAAGGGTTCCGTCT", "TACCTATGTCACTGGC",
"GCAGCCAGTAGTACCT", "CATGCCTCATGCCCGA", "GATCAGTTCCGTCATC", "ACGTCAAAGTCGCCGT",
"GCTTGAATCATCGGAT", "AAGTCTGCACATGGGA", "GTTCGGGGTTCACGGC", "GTGTTAGCATTCGACA",
"ATAGACCAGGACAGAA", "TAGAGCTGTGAGGGAG", "TCAGATGCAGCGTAAG", "ATTATCCTCATACGGT",
"TGAGCCGTCCCGGATG", "AGATTGCGTCTACCTC", "GCGCAGTGTCAAAGAT", "TGAGGGAAGAACAACT",
"GACTAACTCACAGGCC", "CCTTCGAAGGGAAACA", "CGTGAGCAGTTTGCGT", "CGTCTACTCTTGGGTA",
"GGTGCGTCATACTCTT", "TCAATCTGTCTTGCGG", "TGAGAGGAGGGTCGAT", "TAGAGCTTCCCGACTT",
"TGTGGTAAGAATGTGT", "ACACCCTCAACACCTA", "AGTCTTTCACATTAGC", "TGAAAGAAGCGTAATA",
"TGCACCTCACAACGTT", "AATCGGTAGCTCTCGG", "CGAGCCAGTAGTAGTA", "TCCACACAGAAGGACA",
"AGGGAGTAGATGTAAC", "GAGTCCGAGCTCCTCT", "GCTTCCACAGACAAAT", "GCCTCTAGTTAAGACA",
"CGCTGGAGTCACTTCC", "CGTGTAACATGGGAAC", "CTAACTTAGCGATATA", "TCAGGTAAGGGTTCCC",
"TTAGTTCTCGAGAACG", "CAGCTAACAAACTGCT", "TAAGAGAGTCTTGTCC", "GTGTGCGTCCGTTGTC",
"TCGAGGCGTCTTGTCC", "GACAGAGTCAAAGACA", "TGATTTCGTCACACGC", "CGCGGTACACTTCTGC",
"CTAGCCTTCCGTCAAA", "CTACACCCAAGAAAGG", "GTGTGCGGTCAATACC", "CCATTCGAGCACAGGT",
"CGAACATAGCAGATCG", "CGATGGCAGTACGATA", "AAATGCCAGCAACGGT", "AGGTCCGCACGAAGCA",
"AGTGGGACAAGTTGTC", "TTATGCTAGTACGACG", "CTGCGGATCCATGAAC", "ACTTGTTCAAATTGCC",
"GTGCTTCCAGGAATCG", "GCTTCCAGTGGTAACG", "ACGCCGATCTTTCCTC", "GCTGGGTCAACAACCT",
"CATGGCGGTCCAGTGC", "CGGACACAGCTGTTCA", "ACGCAGCAGCTACCGC", "CAAGGCCTCATAACCG",
"TGAGCATCAGCCTTTC", "ATCATGGTCGCTTAGA", "TGCGGGTGTTGTACAC", "GAATGAAGTCAGTGGA",
"TGTGGTACATAAAGGT", "TTTATGCTCACCATAG", "CGACTTCAGGATCGCA", "GTGCGGTGTAGAGCTG",
"TGCTACCAGAGGACGG", "CGTTCTGTCCTAGGGC", "GAAACTCGTTCCCGAG", "CATCAAGCAATTGCTG",
"GGGTTGCTCCGTAGTA", "CTGCGGATCCGCATCT", "GAAGCAGGTAGCGCAA", "TGTATTCAGATAGGAG",
"TCAACGACAAGACGTG", "GCCAAATTCGAGGTAG", "TTAGTTCCATCCGCGA", "TTGGCAAGTTCCATGA",
"GTTCGGGGTACCGTTA", "CCCAATCAGTTGTCGT", "CGATGTATCGATGAGG", "CTCGGGACATATACCG",
"TCGAGGCTCTTAGCCC", "TAAGCGTTCACGGTTA", "CTTGGCTAGTGCTGCC", "AGTGGGACAGCTGCAC",
"TTGCGTCAGTAGATGT", "CTGCGGACAGCTCCGA", "TGCGTGGCACATTTCT", "CATGGCGAGGCTATCT",
"GGGATGACATCTCCCA", "GACCTGGGTCTCTTAT", "CAGCTGGCAAGAAGAG", "TAGCCGGAGCAGGTCA",
"CAGCTAATCACAGTAC", "TATTACCTCCTTGCCA", "CTCGTCACATTCTCAT", "TCAACGATCTTTAGTC",
"CTGAAGTCACTCAGGC", "TCAGCTCAGTTGTCGT", "CATTATCGTAAGTTCC", "GTGGGTCGTAGCGCAA",
"CATGACAGTCTAACGT", "CAGTCCTAGGACACCA", "CTCAGAAGTCGAATCT", "GGGTCTGTCCACGAAT",
"GCTGCGAAGCCACGTC", "GAATGAAGTGAGTATA", "TGCTGCTCATGCTGGC", "GCTTGAAGTTGGTAAA",
"GATGCTAGTGCTTCTC", "GTACTCCCACTGCCAG", "AGCAGCCCAAGTCTGT", "GAAGCAGGTTAAGGGC",
"ACCTTTACAGGGTACA", "ACTGTCCAGTATTGGA", "ACTTGTTAGAGTCTGG", "CGGAGTCAGCTAGTTC",
"CCGTGGATCGACCAGC", "CAGCTGGCATACGCTA", "CTGCTGTTCATTGCCC", "GGGTTGCTCATCATTC",
"GCATGCGGTTTAGGAA", "ATCACGAGTGGGTCAA", "ACATACGCACGGTAGA", "CCTAAAGTCCGTAGTA",
"CAACCTCGTAGCAAAT", "AAGGTTCGTAAGGGAA", "CGGACTGTCGGTGTTA", "GATCGATCAGCTGGCT",
"GCGCAACAGCTATGCT", "GCAAACTTCCGCAGTG", "TTGAACGAGAGGTAGA", "TAAGTGCTCGCCAGCA",
"GTGCATAAGAAACGAG", "GCAATCAGTACGAAAT", "TACACGAAGTCGAGTG", "AGAGCTTCACTTGGAT",
"GACGGCTCACATCTTT", "TATCTCACACTGCCAG", "GCACATATCTCCAACC", "TGCCCTATCGGATGTT",
"GATGCTAGTAGGGACT", "TGTGTTTGTCGCCATG", "ACCAGTATCAGTTGAC", "TGGTTCCTCCGGGTGT",
"CTAGCCTCATACAGCT", "GCGAGAAGTTCACCTC", "GGAAAGCAGGATGTAT", "CATGCCTGTCGAGTTT",
"ACAGCCGTCCACGTTC", "TTTCCTCGTCTCCATC", "GCTTCCAGTGAAGGCT", "AGGGAGTTCGTTACGA",
"AGATCTGTCTCGTATT", "TGTTCCGCATCCCACT", "TGATTTCGTTACGACT", "CAGCCGAAGTCCGGTC",
"GCATACATCCCAACGG", "TCTGAGATCCGCATAA", "TTGGCAACATGCCACG", "GGTGAAGAGCTGATAA",
"TCGGGACCACTGTTAG", "TCAGATGGTCTTGTCC", "GGACAAGCACCGCTAG", "CAGATCATCCGTAGGC",
"CTCACACGTTCCGGCA", "CATCAAGGTGCAGACA", "TAGCCGGGTTAAGATG", "CCATGTCTCGATCCCT",
"ATTATCCCAGCCAATT", "GCCTCTAGTTGAGGTG", "CAAGAAAAGCTGTCTA", "CGTTGGGTCACCAGGC",
"CGTAGCGTCTTGAGAC", "AACGTTGGTTGCTCCT", "CATCCACGTGAGTATA", "TGAGGGACAATGGACG",
"CCAGCGAAGCTATGCT", "CGTTGGGTCACGGTTA", "ACTATCTGTAGCGTAG", "ATTTCTGCATGGTCAT",
"TACGGATGTCCTGCTT", "TGGTTCCCAGACGTAG", "GCAAACTGTAGCGTGA", "CCACGGAGTAACGACG",
"GGCCGATAGCGTTGCC", "GGTGCGTAGAAACCAT", "ACGATACGTTCTGAAC", "GACACGCTCCAAACTG",
"GCCTCTAGTTCAGTAC", "TCGAGGCGTGAGTATA", "TACTCGCCAGGATTGG", "GGGAATGTCTTCATGT",
"ACTTTCATCTATCCCG", "GTGCATAGTAAACCTC", "AAGTCTGCATTCTCAT", "TCGGTAATCCACGCAG",
"CAGCATAAGCCGTCGT", "GCTGGGTCACTAAGTC", "CTTACCGGTCTCATCC", "CGTCAGGCAAAGTCAA",
"ACGGAGATCCAAACTG", "GCATACAAGTAGCGGT", "GACTGCGCAGAAGCAC", "ACTGAGTAGTGATCGG",
"GAATGAATCGCGGATC", "CAAGTTGGTACTCGCG", "GATGCTAAGACTAGAT", "GATGCTATCAAACCAC",
"AAATGCCTCTTTACAC", "GTAGTCATCTGGTGTA", "TCACGAACAAGTTGTC", "TTGCCGTTCTAACGGT",
"CGGACTGAGTCCGTAT", "TGGACGCAGGAGTAGA", "TACCTTAGTACAGCAG", "TCGCGAGAGGTGCAAC",
"ACTGAACAGGTGGGTT", "GCACATATCCACGTTC", "AGTGAGGTCGTCCAGG", "GGGTTGCTCTACTCAT",
"CTCAGAAAGTGAACAT", "CTGTGCTTCAACGGCC", "ACGGCCACACCTTGTC", "TCTGAGACAAGAAAGG",
"CACAGGCAGTACACCT", "TCGGTAAAGATCCCGC", "TCATTACCATCACGTA", "ACATCAGGTGATGTCT",
"GACAGAGTCAGCTTAG", "CTACCCACAAACCTAC", "TTCTTAGTCTCTAGGA", "AAGCCGCGTTATCCGA",
"GTACGTAAGATGCCAG", "TTCGGTCGTCTCGTTC", "GTACGTATCCAATGGT", "TTATGCTAGACGACGT",
"CAGAATCTCACTCTTA", "ATTCTACTCGTTTAGG", "GCAAACTGTGGGTCAA", "GGGTCTGTCTGATACG",
"TTTGTCATCCTAAGTG", "GCTGCGAGTCGAGTTT", "GAATAAGAGCGTGTCC", "TCTCTAACACGGTAGA",
"GAATAAGCAAGTCTAC", "AACTTTCCAATGTAAG", "TAGACCATCCTAGGGC", "TGACTTTCAAGTACCT",
"AAAGCAAGTGATAAAC", "GGATTACGTCTTCAAG", "AGAGCTTGTAGAGTGC", "GCTGCTTGTGAGGGTT",
"GGCAATTAGCACCGTC", "TCAGGATGTAGCGTAG", "ACGAGCCGTTCCACTC", "GAACGGAAGAGACGAA",
"TACGGATGTCGGGTCT", "AGTTGGTTCCCGACTT", "AGCCTAATCGTTTGCC", "CAGTCCTAGTGTGGCA",
"CTGCGGAAGAGTAAGG", "CGAGCACCACCACGTG", "TTAACTCCATCAGTCA", "TTGCCGTCAAAGTCAA",
"GTACTCCTCGGATGGA", "AATCCAGTCCGGGTGT", "ATCTGCCGTCCGTGAC", "TACGGATCAATCCAAC",
"TGGCTGGGTTGATTGC", "CATCAAGGTCGCATCG", "AGCTCCTCAGCTTCGG", "CACACAATCAACACAC",
"TTCTACAAGCTGAAAT", "GACTGCGGTCAGCTAT", "TACAGTGCACATCCAA", "GACGTGCGTGCAGACA",
"GGACAAGTCTGCAGTA", "TAGGCATGTCAGTGGA", "CTTACCGTCGTAGGTT", "CGAGCACAGACAATAC",
"AGGTCATCAGTATAAG", "TGTTCCGTCTGCGTAA", "TCAGCAACATTCTCAT", "GTACTTTGTTCAGACT",
"TTGAACGCACCCATTC", "CAAGTTGGTAAGTGGC", "CAAGTTGAGTACGCGA", "CCTCTGAGTGTGCGTC",
"CCAGCGATCCAAGTAC", "ACTGATGGTAAAGGAG", "CTTAACTAGCTAGTCT", "ACACCAAAGTGGTCCC",
"ACTGAACGTCTACCTC", "AGTGGGAAGCGGATCA", "GTTACAGCATAGTAAG", "CAAGGCCAGAGGTTAT",
"TTTCCTCTCTACCAGA", "CTGTGCTGTCATCGGC", "CCTATTATCCTTGCCA", "TCAGGTAGTAGCTGCC",
"TTCTACACATTCGACA", "TGTGGTACATCACAAC", "CAGCGACGTACCGCTG", "AGTTGGTTCACTTCAT",
"TACTTGTAGCGTAATA", "TTAGTTCGTGTGCCTG", "TGTTCCGTCACCTCGT", "AACTGGTAGAGGTTGC",
"CACATAGTCAGCGATT", "CTGCTGTGTGACGGTA", "ACATACGCAATAACGA", "TAGTGGTCACGTTGGC",
"CTGAAACTCGCCTGAG", "ACTTACTAGTAGGTGC", "TCTGAGAAGGGTTCCC", "GTTTCTAGTGCACCAC",
"TCAGCTCAGCGATCCC", "AACGTTGCAAGGTGTG", "GAATGAACAAAGTCAA", "AAACGGGTCTTGAGGT",
"CCTTTCTTCAGGCAAG", "GGACAAGAGCTAAGAT", "GGCTGGTTCTTGCATT", "TGCGCAGAGCCAGTAG",
"CCACTACGTGTTGGGA", "TCAGGTAAGACGCTTT", "CCACCTACATGTCTCC", "GAACATCTCAGAGCTT",
"AGTCTTTTCCAAGCCG", "GATCGCGAGCTATGCT", "AACGTTGCAGCGAACA", "AACTGGTTCGTCCGTT",
"TTGCCGTGTTATGCGT", "TCAATCTTCCAACCAA", "GAGTCCGTCGGAAACG", "AACACGTGTCGCTTCT",
"TGGACGCAGAAGATTC", "GCTTCCATCTGTCAAG", "GGGCACTTCGATAGAA", "TGACTTTGTTCAGACT",
"GTGCGGTAGACACTAA", "CAGCATATCTACTATC", "TTGGCAATCGGGAGTA", "CAGAGAGTCTGTCCGT",
"AGAGCTTAGCCCAGCT", "TGGTTCCGTCTAACGT", "GCGGGTTTCTATCCCG", "TCACAAGAGCGTGAGT",
"TCCCGATAGATAGGAG", "TCTCTAATCGTATCAG", "CACCACTAGGGATGGG", "TAGACCAGTTCTGGTA",
"TCAGCTCGTTAGATGA", "TACGGATTCTAGAGTC", "GTCATTTAGTGGTAGC", "GTACTCCTCTGGCGTG",
"AATCCAGCAGGAACGT", "AGCAGCCGTGCAGGTA", "GAAATGATCAGG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, 5L, 3L, 2L, 2L, 4L, 8L, 8L, 5L, 3L, 4L, 2L, 3L, 8L, 7L, 3L,
7L, 6L, 9L, 9L, 6L, 8L, 8L, 4L, 7L, 8L, 8L, 7L, 1L, 5L, 2L, 1L,
8L, 8L, 5L, 2L, 6L, 5L, 5L, 7L, 8L, 5L, 8L, 9L, 6L, 5L, 6L, 5L,
7L, 2L, 8L, 3L, 1L, 5L, 1L, 8L, 6L, 8L, 8L, 1L, 1L, 9L, 7L, 2L,
2L, 2L, 2L, 8L, 7L, 5L, 8L, 4L, 5L, 1L, 1L, 2L, 2L, 8L, 8L, 6L,
7L, 3L, 8L, 8L, 1L, 2L, 4L, 6L, 6L, 7L, 2L, 5L, 6L, 1L, 7L, 5L,
6L, 8L, 3L, 4L, 1L, 5L, 8L, 4L, 6L, 8L, 5L, 8L, 3L, 5L, 7L, 8L,
6L, 5L, 8L, 2L, 8L, 8L, 3L, 5L), .Label = c("1", "2", "3", "4",
"5", "6", "7", "8", "9"), class = "factor")
Комментарии:
1. Работает ли график вне цикла или только тогда, когда вы запускаете цикл? Если это не работает с одной группой вне цикла, вы можете значительно упростить свой пример кода. 🙂
2. @aosmith это работает для одного примера. Я предполагаю, что что-то не так с оценкой переменных?
3. Мне странно, что вы используете
pred_density_pois
в качестве вектора. Возможно, попробуйте добавить его в виде столбцаcurrent_data
?4. @GregorThomas это отличный момент, это поможет, если я сохраню этот вектор
current_data
.
Ответ №1:
Проблема связана с циклом, поскольку он создает некоторые поврежденные графики. Попробуйте этот подход. Также используйте предложение @GregorThomas:
#Values
n_clusters <- length(levels(SNF_W_louvain))
n_features <- dim(filtered_ADT_counts)[1]
Теперь новый код:
List <- list()
for(current_cluster in 1:n_clusters) {
#List
all_plots <- list()
#Loop
for(current_marker in 1:n_features) {
current_data <- data.frame(counts = filtered_ADT_counts[current_marker, SNF_W_louvain == current_cluster])
fit_poisson <- glm(counts ~ 1, current_data, family = "poisson")
fit_info <- paste0("AIC: ", round(fit_poisson$aic, 2), " (Pois)")
plot_title <- paste0("Protein: ", rownames(filtered_ADT_counts)[current_marker], " Cluster: ", current_cluster)
pred_density_pois <- dpois(current_data$counts, lambda = exp(fit_poisson$coefficients[1]))
current_plot <- current_data %>%
bind_cols(data.frame(pred_density_pois=pred_density_pois)) %>%
ggplot()
geom_histogram(aes(x = counts, y=..density..), fill = "white", color='red')
geom_line(aes(x = counts, y = pred_density_pois), color= 'blue')
ggtitle(plot_title)
annotate("text", x = Inf, y = Inf, hjust = 1, vjust = 2, label = fit_info, size = 3)
all_plots[[current_marker]] <- current_plot
}
List <- c(List,all_plots)
}
И экспортировать:
ggsave(
filename = "all_plots.pdf",
plot = marrangeGrob(List, nrow=4, ncol=2),
units = "mm",
width = 210,
height = 297
)
Дайте мне знать, если это сработает, поскольку у меня нет никаких выходных данных для сравнения.