0
$\begingroup$

Newbie R user here. I want to create a plot with boxplots of read length distribution on the Y-axis, and samples on the X-axis -- like this image from doi: 10.1038/srep16498 enter image description here

However, I'm not sure how to plot my data in ggplot2 to make this. My data is currently in this format:

Length Sample1 Sample2
20 10 5
30 15 223
40 16 4923
50 293 239
60 3290 23
70 248 6
80 23 1
90 21 0
100 5 0

Length is the read length bin, with the sample columns containing the number of reads of that length. I've tried using ggplot2, but don't think I can do it with the data in its current format.

Thanks!

Edit: Here are the number of reads for each individual read length (converted to long format):

"Readlength" "name" "value"
"1" 25 "Sample1" 457
"2" 25 "Sample2" 580
"3" 26 "Sample1" 603
"4" 26 "Sample2" 648
"5" 27 "Sample1" 599
"6" 27 "Sample2" 715
"7" 28 "Sample1" 686
"8" 28 "Sample2" 914
"9" 29 "Sample1" 796
"10" 29 "Sample2" 1018
"11" 30 "Sample1" 1009
"12" 30 "Sample2" 1366
"13" 31 "Sample1" 1225
"14" 31 "Sample2" 1653
"15" 32 "Sample1" 1579
"16" 32 "Sample2" 2135
"17" 33 "Sample1" 1817
"18" 33 "Sample2" 2678
"19" 34 "Sample1" 2322
"20" 34 "Sample2" 3424
"21" 35 "Sample1" 2861
"22" 35 "Sample2" 4167
"23" 36 "Sample1" 3460
"24" 36 "Sample2" 5220
"25" 37 "Sample1" 4161
"26" 37 "Sample2" 6088
"27" 38 "Sample1" 4930
"28" 38 "Sample2" 7609
"29" 39 "Sample1" 5910
"30" 39 "Sample2" 9308
"31" 40 "Sample1" 7007
"32" 40 "Sample2" 11062
"33" 41 "Sample1" 8339
"34" 41 "Sample2" 13341
"35" 42 "Sample1" 10105
"36" 42 "Sample2" 16288
"37" 43 "Sample1" 11720
"38" 43 "Sample2" 19023
"39" 44 "Sample1" 13312
"40" 44 "Sample2" 22152
"41" 45 "Sample1" 15430
"42" 45 "Sample2" 25360
"43" 46 "Sample1" 17515
"44" 46 "Sample2" 28789
"45" 47 "Sample1" 20022
"46" 47 "Sample2" 32985
"47" 48 "Sample1" 22825
"48" 48 "Sample2" 38019
"49" 49 "Sample1" 26150
"50" 49 "Sample2" 42972
"51" 50 "Sample1" 28998
"52" 50 "Sample2" 49293
"53" 51 "Sample1" 33236
"54" 51 "Sample2" 56239
"55" 52 "Sample1" 37302
"56" 52 "Sample2" 62791
"57" 53 "Sample1" 41212
"58" 53 "Sample2" 69865
"59" 54 "Sample1" 45683
"60" 54 "Sample2" 76922
"61" 55 "Sample1" 49844
"62" 55 "Sample2" 82906
"63" 56 "Sample1" 53833
"64" 56 "Sample2" 90092
"65" 57 "Sample1" 58486
"66" 57 "Sample2" 98194
"67" 58 "Sample1" 64650
"68" 58 "Sample2" 105602
"69" 59 "Sample1" 69514
"70" 59 "Sample2" 114319
"71" 60 "Sample1" 76202
"72" 60 "Sample2" 123807
"73" 61 "Sample1" 83273
"74" 61 "Sample2" 133289
"75" 62 "Sample1" 90783
"76" 62 "Sample2" 146387
"77" 63 "Sample1" 98805
"78" 63 "Sample2" 159302
"79" 64 "Sample1" 107247
"80" 64 "Sample2" 168856
"81" 65 "Sample1" 115200
"82" 65 "Sample2" 180408
"83" 66 "Sample1" 123359
"84" 66 "Sample2" 191083
"85" 67 "Sample1" 134258
"86" 67 "Sample2" 200708
"87" 68 "Sample1" 142179
"88" 68 "Sample2" 212830
"89" 69 "Sample1" 155549
"90" 69 "Sample2" 226788
"91" 70 "Sample1" 165425
"92" 70 "Sample2" 239063
"93" 71 "Sample1" 178681
"94" 71 "Sample2" 255338
"95" 72 "Sample1" 194293
"96" 72 "Sample2" 272725
"97" 73 "Sample1" 211417
"98" 73 "Sample2" 288512
"99" 74 "Sample1" 225639
"100" 74 "Sample2" 307752
"101" 75 "Sample1" 242618
"102" 75 "Sample2" 324919
"103" 76 "Sample1" 259391
"104" 76 "Sample2" 338429
"105" 77 "Sample1" 274041
"106" 77 "Sample2" 354866
"107" 78 "Sample1" 295365
"108" 78 "Sample2" 373456
"109" 79 "Sample1" 315802
"110" 79 "Sample2" 389945
"111" 80 "Sample1" 337826
"112" 80 "Sample2" 411033
"113" 81 "Sample1" 363906
"114" 81 "Sample2" 437069
"115" 82 "Sample1" 388803
"116" 82 "Sample2" 460419
"117" 83 "Sample1" 416728
"118" 83 "Sample2" 487959
"119" 84 "Sample1" 446797
"120" 84 "Sample2" 518295
"121" 85 "Sample1" 474343
"122" 85 "Sample2" 538191
"123" 86 "Sample1" 496615
"124" 86 "Sample2" 558646
"125" 87 "Sample1" 524080
"126" 87 "Sample2" 583043
"127" 88 "Sample1" 549720
"128" 88 "Sample2" 601107
"129" 89 "Sample1" 573864
"130" 89 "Sample2" 623715
"131" 90 "Sample1" 604559
"132" 90 "Sample2" 648209
"133" 91 "Sample1" 632324
"134" 91 "Sample2" 668883
"135" 92 "Sample1" 657924
"136" 92 "Sample2" 698291
"137" 93 "Sample1" 686875
"138" 93 "Sample2" 727313
"139" 94 "Sample1" 712778
"140" 94 "Sample2" 745380
"141" 95 "Sample1" 732786
"142" 95 "Sample2" 766930
"143" 96 "Sample1" 755081
"144" 96 "Sample2" 784337
"145" 97 "Sample1" 772734
"146" 97 "Sample2" 793358
"147" 98 "Sample1" 787861
"148" 98 "Sample2" 804737
"149" 99 "Sample1" 807004
"150" 99 "Sample2" 820443
"151" 100 "Sample1" 823211
"152" 100 "Sample2" 826114
"153" 101 "Sample1" 836362
"154" 101 "Sample2" 839164
"155" 102 "Sample1" 853124
"156" 102 "Sample2" 853144
"157" 103 "Sample1" 865378
"158" 103 "Sample2" 861670
"159" 104 "Sample1" 874578
"160" 104 "Sample2" 875069
"161" 105 "Sample1" 887322
"162" 105 "Sample2" 883285
"163" 106 "Sample1" 888977
"164" 106 "Sample2" 875065
"165" 107 "Sample1" 889700
"166" 107 "Sample2" 877648
"167" 108 "Sample1" 893138
"168" 108 "Sample2" 875875
"169" 109 "Sample1" 887592
"170" 109 "Sample2" 865221
"171" 110 "Sample1" 888684
"172" 110 "Sample2" 863268
"173" 111 "Sample1" 887037
"174" 111 "Sample2" 863513
"175" 112 "Sample1" 887121
"176" 112 "Sample2" 855519
"177" 113 "Sample1" 883085
"178" 113 "Sample2" 854312
"179" 114 "Sample1" 886536
"180" 114 "Sample2" 855445
"181" 115 "Sample1" 875703
"182" 115 "Sample2" 844795
"183" 116 "Sample1" 870749
"184" 116 "Sample2" 837992
"185" 117 "Sample1" 863969
"186" 117 "Sample2" 830157
"187" 118 "Sample1" 849188
"188" 118 "Sample2" 809476
"189" 119 "Sample1" 840690
"190" 119 "Sample2" 798965
"191" 120 "Sample1" 834472
"192" 120 "Sample2" 791200
"193" 121 "Sample1" 822558
"194" 121 "Sample2" 776191
"195" 122 "Sample1" 811864
"196" 122 "Sample2" 765913
"197" 123 "Sample1" 805300
"198" 123 "Sample2" 761200
"199" 124 "Sample1" 794602
"200" 124 "Sample2" 747483
"201" 125 "Sample1" 784650
"202" 125 "Sample2" 739639
"203" 126 "Sample1" 775107
"204" 126 "Sample2" 730152
"205" 127 "Sample1" 763282
"206" 127 "Sample2" 713285
"207" 128 "Sample1" 748652
"208" 128 "Sample2" 699446
"209" 129 "Sample1" 736764
"210" 129 "Sample2" 686165
"211" 130 "Sample1" 723134
"212" 130 "Sample2" 671281
"213" 131 "Sample1" 710686
"214" 131 "Sample2" 658314
"215" 132 "Sample1" 701834
"216" 132 "Sample2" 650564
"217" 133 "Sample1" 689531
"218" 133 "Sample2" 635331
"219" 134 "Sample1" 678070
"220" 134 "Sample2" 628475
"221" 135 "Sample1" 668757
"222" 135 "Sample2" 617210
"223" 136 "Sample1" 654506
"224" 136 "Sample2" 601336
"225" 137 "Sample1" 644720
"226" 137 "Sample2" 592700
"227" 138 "Sample1" 630305
"228" 138 "Sample2" 579707
"229" 139 "Sample1" 615571
"230" 139 "Sample2" 562022
"231" 140 "Sample1" 603896
"232" 140 "Sample2" 550812
"233" 141 "Sample1" 597816
"234" 141 "Sample2" 540016
"235" 142 "Sample1" 584121
"236" 142 "Sample2" 526066
"237" 143 "Sample1" 571689
"238" 143 "Sample2" 516774
"239" 144 "Sample1" 564142
"240" 144 "Sample2" 509033
"241" 145 "Sample1" 552281
"242" 145 "Sample2" 496389
"243" 146 "Sample1" 543184
"244" 146 "Sample2" 487283
"245" 147 "Sample1" 532683
"246" 147 "Sample2" 477035
"247" 148 "Sample1" 520019
"248" 148 "Sample2" 461698
"249" 149 "Sample1" 510484
"250" 149 "Sample2" 451095
"251" 150 "Sample1" 507832
"252" 150 "Sample2" 446390
"253" 151 "Sample1" 495343
"254" 151 "Sample2" 431015
"255" 152 "Sample1" 484169
"256" 152 "Sample2" 417551
"257" 153 "Sample1" 471422
"258" 153 "Sample2" 404674
"259" 154 "Sample1" 460944
"260" 154 "Sample2" 390280
"261" 155 "Sample1" 448227
"262" 155 "Sample2" 380134
"263" 156 "Sample1" 437738
"264" 156 "Sample2" 369703
"265" 157 "Sample1" 428359
"266" 157 "Sample2" 354902
"267" 158 "Sample1" 417445
"268" 158 "Sample2" 343343
"269" 159 "Sample1" 408065
"270" 159 "Sample2" 331193
"271" 160 "Sample1" 395244
"272" 160 "Sample2" 315775
"273" 161 "Sample1" 386510
"274" 161 "Sample2" 305048
"275" 162 "Sample1" 377028
"276" 162 "Sample2" 292336
"277" 163 "Sample1" 366563
"278" 163 "Sample2" 277876
"279" 164 "Sample1" 357659
"280" 164 "Sample2" 267642
"281" 165 "Sample1" 348242
"282" 165 "Sample2" 255871
"283" 166 "Sample1" 337553
"284" 166 "Sample2" 242636
"285" 167 "Sample1" 327824
"286" 167 "Sample2" 231054
"287" 168 "Sample1" 317584
"288" 168 "Sample2" 219066
"289" 169 "Sample1" 306454
"290" 169 "Sample2" 204829
"291" 170 "Sample1" 295255
"292" 170 "Sample2" 192423
"293" 171 "Sample1" 284770
"294" 171 "Sample2" 180440
"295" 172 "Sample1" 274444
"296" 172 "Sample2" 166529
"297" 173 "Sample1" 264469
"298" 173 "Sample2" 155605
"299" 174 "Sample1" 253923
"300" 174 "Sample2" 145166
"301" 175 "Sample1" 243462
"302" 175 "Sample2" 134932
"303" 176 "Sample1" 233021
"304" 176 "Sample2" 124474
"305" 177 "Sample1" 223360
"306" 177 "Sample2" 114975
"307" 178 "Sample1" 213341
"308" 178 "Sample2" 105267
"309" 179 "Sample1" 202191
"310" 179 "Sample2" 95632
"311" 180 "Sample1" 193325
"312" 180 "Sample2" 87027
"313" 181 "Sample1" 182022
"314" 181 "Sample2" 78802
"315" 182 "Sample1" 173011
"316" 182 "Sample2" 71116
"317" 183 "Sample1" 164207
"318" 183 "Sample2" 64758
"319" 184 "Sample1" 154985
"320" 184 "Sample2" 57082
"321" 185 "Sample1" 147070
"322" 185 "Sample2" 51728
"323" 186 "Sample1" 138927
"324" 186 "Sample2" 46295
"325" 187 "Sample1" 130203
"326" 187 "Sample2" 40908
"327" 188 "Sample1" 122935
"328" 188 "Sample2" 36275
"329" 189 "Sample1" 115565
"330" 189 "Sample2" 31825
"331" 190 "Sample1" 107969
"332" 190 "Sample2" 28028
"333" 191 "Sample1" 100995
"334" 191 "Sample2" 24447
"335" 192 "Sample1" 94510
"336" 192 "Sample2" 21125
"337" 193 "Sample1" 88365
"338" 193 "Sample2" 18489
"339" 194 "Sample1" 82247
"340" 194 "Sample2" 15878
"341" 195 "Sample1" 77315
"342" 195 "Sample2" 13881
"343" 196 "Sample1" 71657
"344" 196 "Sample2" 12183
"345" 197 "Sample1" 66920
"346" 197 "Sample2" 10305
"347" 198 "Sample1" 61298
"348" 198 "Sample2" 9096
"349" 199 "Sample1" 57765
"350" 199 "Sample2" 7759
"351" 200 "Sample1" 53811
"352" 200 "Sample2" 6645
"353" 201 "Sample1" 48906
"354" 201 "Sample2" 5532
"355" 202 "Sample1" 44847
"356" 202 "Sample2" 4642
"357" 203 "Sample1" 41546
"358" 203 "Sample2" 3997
"359" 204 "Sample1" 38125
"360" 204 "Sample2" 3360
"361" 205 "Sample1" 35039
"362" 205 "Sample2" 2858
"363" 206 "Sample1" 31902
"364" 206 "Sample2" 2447
"365" 207 "Sample1" 29076
"366" 207 "Sample2" 2185
"367" 208 "Sample1" 26271
"368" 208 "Sample2" 1811
"369" 209 "Sample1" 24037
"370" 209 "Sample2" 1494
"371" 210 "Sample1" 21487
"372" 210 "Sample2" 1313
"373" 211 "Sample1" 19187
"374" 211 "Sample2" 1028
"375" 212 "Sample1" 17317
"376" 212 "Sample2" 954
"377" 213 "Sample1" 15741
"378" 213 "Sample2" 819
"379" 214 "Sample1" 14000
"380" 214 "Sample2" 693
"381" 215 "Sample1" 12494
"382" 215 "Sample2" 614
"383" 216 "Sample1" 10963
"384" 216 "Sample2" 556
"385" 217 "Sample1" 9928
"386" 217 "Sample2" 549
"387" 218 "Sample1" 8652
"388" 218 "Sample2" 421
"389" 219 "Sample1" 7604
"390" 219 "Sample2" 404
"391" 220 "Sample1" 6515
"392" 220 "Sample2" 394
"393" 221 "Sample1" 5735
"394" 221 "Sample2" 325
"395" 222 "Sample1" 4783
"396" 222 "Sample2" 301
"397" 223 "Sample1" 4108
"398" 223 "Sample2" 308
"399" 224 "Sample1" 3611
"400" 224 "Sample2" 290
"401" 225 "Sample1" 3224
"402" 225 "Sample2" 307
"403" 226 "Sample1" 2676
"404" 226 "Sample2" 304
"405" 227 "Sample1" 2261
"406" 227 "Sample2" 273
"407" 228 "Sample1" 1936
"408" 228 "Sample2" 303
"409" 229 "Sample1" 1664
"410" 229 "Sample2" 302
"411" 230 "Sample1" 1529
"412" 230 "Sample2" 291
"413" 231 "Sample1" 1180
"414" 231 "Sample2" 313
"415" 232 "Sample1" 975
"416" 232 "Sample2" 288
"417" 233 "Sample1" 897
"418" 233 "Sample2" 274
"419" 234 "Sample1" 812
"420" 234 "Sample2" 281
"421" 235 "Sample1" 712
"422" 235 "Sample2" 318
"423" 236 "Sample1" 587
"424" 236 "Sample2" 288
"425" 237 "Sample1" 555
"426" 237 "Sample2" 299
"427" 238 "Sample1" 524
"428" 238 "Sample2" 314
"429" 239 "Sample1" 443
"430" 239 "Sample2" 336
"431" 240 "Sample1" 411
"432" 240 "Sample2" 354
"433" 241 "Sample1" 382
"434" 241 "Sample2" 339
"435" 242 "Sample1" 344
"436" 242 "Sample2" 342
"437" 243 "Sample1" 340
"438" 243 "Sample2" 366
"439" 244 "Sample1" 362
"440" 244 "Sample2" 353
"441" 245 "Sample1" 325
"442" 245 "Sample2" 373
"443" 246 "Sample1" 290
"444" 246 "Sample2" 340
"445" 247 "Sample1" 319
"446" 247 "Sample2" 345
"447" 248 "Sample1" 293
"448" 248 "Sample2" 341
"449" 249 "Sample1" 304
"450" 249 "Sample2" 365
"451" 250 "Sample1" 354
"452" 250 "Sample2" 381
"453" 251 "Sample1" 296
"454" 251 "Sample2" 366
"455" 252 "Sample1" 315
"456" 252 "Sample2" 415
"457" 253 "Sample1" 315
"458" 253 "Sample2" 399
"459" 254 "Sample1" 331
"460" 254 "Sample2" 377
"461" 255 "Sample1" 321
"462" 255 "Sample2" 410
"463" 256 "Sample1" 326
"464" 256 "Sample2" 421
"465" 257 "Sample1" 349
"466" 257 "Sample2" 433
"467" 258 "Sample1" 370
"468" 258 "Sample2" 429
"469" 259 "Sample1" 318
"470" 259 "Sample2" 439
"471" 260 "Sample1" 344
"472" 260 "Sample2" 433
"473" 261 "Sample1" 346
"474" 261 "Sample2" 400
"475" 262 "Sample1" 341
"476" 262 "Sample2" 419
"477" 263 "Sample1" 345
"478" 263 "Sample2" 499
"479" 264 "Sample1" 348
"480" 264 "Sample2" 418
"481" 265 "Sample1" 371
"482" 265 "Sample2" 454
"483" 266 "Sample1" 405
"484" 266 "Sample2" 441
"485" 267 "Sample1" 380
"486" 267 "Sample2" 476
"487" 268 "Sample1" 365
"488" 268 "Sample2" 464
"489" 269 "Sample1" 366
"490" 269 "Sample2" 461
"491" 270 "Sample1" 351
"492" 270 "Sample2" 483
"493" 271 "Sample1" 431
"494" 271 "Sample2" 481
"495" 272 "Sample1" 340
"496" 272 "Sample2" 503
"497" 273 "Sample1" 405
"498" 273 "Sample2" 493
"499" 274 "Sample1" 370
"500" 274 "Sample2" 529
"501" 275 "Sample1" 374
"502" 275 "Sample2" 469
"503" 276 "Sample1" 385
"504" 276 "Sample2" 503
"505" 277 "Sample1" 361
"506" 277 "Sample2" 496
"507" 278 "Sample1" 410
"508" 278 "Sample2" 491
"509" 279 "Sample1" 410
"510" 279 "Sample2" 521
"511" 280 "Sample1" 368
"512" 280 "Sample2" 514
"513" 281 "Sample1" 397
"514" 281 "Sample2" 501
"515" 282 "Sample1" 379
"516" 282 "Sample2" 546
"517" 283 "Sample1" 384
"518" 283 "Sample2" 502
"519" 284 "Sample1" 389
"520" 284 "Sample2" 513
"521" 285 "Sample1" 419
"522" 285 "Sample2" 571
"523" 286 "Sample1" 403
"524" 286 "Sample2" 519
"525" 287 "Sample1" 423
"526" 287 "Sample2" 536
"527" 288 "Sample1" 480
"528" 288 "Sample2" 653
"529" 289 "Sample1" 478
"530" 289 "Sample2" 564

$\endgroup$
0

3 Answers 3

1
$\begingroup$

Here's my attempt.

library(tidyverse)
library(S4Vectors)

# This would probably be read in using `read_tsv()` or something
rawData <- structure(
    list(
        Readlength = c(25, 26, 27, 28, 29, 30, 31, 32, 
                       33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 
                       49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 
                       65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 
                       81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 
                       97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 
                       110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 
                       123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 
                       136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 
                       149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 
                       162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 
                       175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 
                       188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 
                       201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 
                       214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 
                       227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 
                       240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 
                       253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 
                       266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 276, 277, 278, 
                       279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289), 
        Sample1 = c(457, 603, 599, 686, 796, 1009, 1225, 1579, 1817, 2322, 2861, 3460, 
                    4161, 4930, 5910, 7007, 8339, 10105, 11720, 13312, 15430, 17515, 
                    20022, 22825, 26150, 28998, 33236, 37302, 41212, 45683, 49844, 
                    53833, 58486, 64650, 69514, 76202, 83273, 90783, 98805, 107247, 
                    115200, 123359, 134258, 142179, 155549, 165425, 178681, 194293, 
                    211417, 225639, 242618, 259391, 274041, 295365, 315802, 337826, 
                    363906, 388803, 416728, 446797, 474343, 496615, 524080, 549720, 
                    573864, 604559, 632324, 657924, 686875, 712778, 732786, 755081, 
                    772734, 787861, 807004, 823211, 836362, 853124, 865378, 874578, 
                    887322, 888977, 889700, 893138, 887592, 888684, 887037, 887121, 
                    883085, 886536, 875703, 870749, 863969, 849188, 840690, 834472, 
                    822558, 811864, 805300, 794602, 784650, 775107, 763282, 748652, 
                    736764, 723134, 710686, 701834, 689531, 678070, 668757, 654506, 
                    644720, 630305, 615571, 603896, 597816, 584121, 571689, 564142, 
                    552281, 543184, 532683, 520019, 510484, 507832, 495343, 484169, 
                    471422, 460944, 448227, 437738, 428359, 417445, 408065, 395244, 
                    386510, 377028, 366563, 357659, 348242, 337553, 327824, 317584, 
                    306454, 295255, 284770, 274444, 264469, 253923, 243462, 233021, 
                    223360, 213341, 202191, 193325, 182022, 173011, 164207, 154985, 
                    147070, 138927, 130203, 122935, 115565, 107969, 100995, 94510, 
                    88365, 82247, 77315, 71657, 66920, 61298, 57765, 53811, 48906, 
                    44847, 41546, 38125, 35039, 31902, 29076, 26271, 24037, 21487, 
                    19187, 17317, 15741, 14000, 12494, 10963, 9928, 8652, 7604, 6515, 
                    5735, 4783, 4108, 3611, 3224, 2676, 2261, 1936, 1664, 1529, 1180, 
                    975, 897, 812, 712, 587, 555, 524, 443, 411, 382, 344, 340, 362, 
                    325, 290, 319, 293, 304, 354, 296, 315, 315, 331, 321, 326, 349, 
                    370, 318, 344, 346, 341, 345, 348, 371, 405, 380, 365, 366, 351, 
                    431, 340, 405, 370, 374, 385, 361, 410, 410, 368, 397, 379, 384, 
                    389, 419, 403, 423, 480, 478), 
        Sample2 = c(580, 648, 715, 914, 
                    1018, 1366, 1653, 2135, 2678, 3424, 4167, 5220, 6088, 7609, 9308, 
                    11062, 13341, 16288, 19023, 22152, 25360, 28789, 32985, 38019, 
                    42972, 49293, 56239, 62791, 69865, 76922, 82906, 90092, 98194, 
                    105602, 114319, 123807, 133289, 146387, 159302, 168856, 180408, 
                    191083, 200708, 212830, 226788, 239063, 255338, 272725, 288512, 
                    307752, 324919, 338429, 354866, 373456, 389945, 411033, 437069, 
                    460419, 487959, 518295, 538191, 558646, 583043, 601107, 623715, 
                    648209, 668883, 698291, 727313, 745380, 766930, 784337, 793358, 
                    804737, 820443, 826114, 839164, 853144, 861670, 875069, 883285, 
                    875065, 877648, 875875, 865221, 863268, 863513, 855519, 854312, 
                    855445, 844795, 837992, 830157, 809476, 798965, 791200, 776191, 
                    765913, 761200, 747483, 739639, 730152, 713285, 699446, 686165, 
                    671281, 658314, 650564, 635331, 628475, 617210, 601336, 592700, 
                    579707, 562022, 550812, 540016, 526066, 516774, 509033, 496389, 
                    487283, 477035, 461698, 451095, 446390, 431015, 417551, 404674, 
                    390280, 380134, 369703, 354902, 343343, 331193, 315775, 305048, 
                    292336, 277876, 267642, 255871, 242636, 231054, 219066, 204829, 
                    192423, 180440, 166529, 155605, 145166, 134932, 124474, 114975, 
                    105267, 95632, 87027, 78802, 71116, 64758, 57082, 51728, 46295, 
                    40908, 36275, 31825, 28028, 24447, 21125, 18489, 15878, 13881, 
                    12183, 10305, 9096, 7759, 6645, 5532, 4642, 3997, 3360, 2858, 
                    2447, 2185, 1811, 1494, 1313, 1028, 954, 819, 693, 614, 556, 
                    549, 421, 404, 394, 325, 301, 308, 290, 307, 304, 273, 303, 302, 
                    291, 313, 288, 274, 281, 318, 288, 299, 314, 336, 354, 339, 342, 
                    366, 353, 373, 340, 345, 341, 365, 381, 366, 415, 399, 377, 410, 
                    421, 433, 429, 439, 433, 400, 419, 499, 418, 454, 441, 476, 464, 
                    461, 483, 481, 503, 493, 529, 469, 503, 496, 491, 521, 514, 501, 
                    546, 502, 513, 571, 519, 536, 653, 564)),
    class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"),
    row.names = c(NA, -265L), 
    spec = structure(
        list(
            cols = list(
                Readlength = structure(
                    list(), class = c("collector_double", "collector")
                    ), 
                Sample1 = structure(
                    list(), class = c("collector_double", "collector")
                ), 
                Sample2 = structure(
                    list(), class = c("collector_double", "collector"))
                ), 
            default = structure(
                list(), class = c("collector_guess", "collector")
            ), 
            skip = 1), 
        class = "col_spec")
    )

rlData <- rawData %>%
    pivot_longer( # Put all the counts in a single column
        cols = contains("Sample"),
        names_to = "Sample",
        values_to = "Count"
        ) %>%
    split(f = .$Sample) %>% # Separate into each sample
lapply(function(x){
    # Now create a run-length encoded vector
    # This encodes millions of repetitive values into 2 numbers
    # 1) the value & 2) how many
    Rle(values = x$Readlength, lengths = x$Count)
        # They can be a bit difficult to plot though
    }) 

# I would create the quantiles for the boxplot separately now
q <- c(min = 0, q25 = 0.25, median = 0.5, q75 = 0.75, max = 1)
# Collect the stats
bxp <- rlData %>%
    lapply(quantile, probs = q) %>% # for every Rle, get the stats 
    as_tibble() %>%
    mutate(stat = names(q)) %>% # This just collates the output
    pivot_longer(
        # Make it long
        cols = contains("Sample"),
        names_to = "Sample",
        values_to = "value"
    ) %>%
    pivot_wider(
        # Now spread it out for each stat/column
        names_from = stat,
        values_from = value
    ) %>%
    mutate(
        # set a few values for easier plotting
        IQR = q75 - q25,
        Sample = as.factor(Sample),
        x = as.integer(Sample)
    )

w <- 0.4 # Box width
bxp %>%
    mutate(
        # Set the width of the boxes
        xmin = x - w,
        xmax = x + w,
        # Set the values for the whiskers. 
        # These usually extend 1.5*IQR. 
        # Any points beyond are considered outliers
        boxMax = case_when(
            max > q75 + 1.5*IQR ~ q75 + 1.5*IQR,
            max > q75 + 1.5*IQR ~ max
        ),
        boxMin = case_when(
            min < q25 - 1.5*IQR ~ q25 - 1.5*IQR,
            min > q25 - 1.5*IQR ~ min
        )
    ) %>%
    ggplot(aes(x = x, y = median)) +
    geom_rect(
        # Draw the box
        aes(xmin = xmin, xmax = xmax, ymin = q25, ymax = q75),
        fill = "grey80",
        colour = "black"
    ) +
    geom_segment(
        # Add the median
        aes(x = xmin, xend = xmax, yend = median),
        colour = "black"
    ) +
    geom_segment(
        # The top whiskers
        aes(x = x, xend = x, y = q75, yend = boxMax)
    ) +
    geom_segment(
        # The bottom whiskers
        aes(x = x, xend = x, y = q25, yend = boxMin)
    ) +
    geom_point(aes(y = max)) + # Add some symbolic outliers if you want
    geom_point(aes(y = min)) +
    scale_x_continuous(
        # Tidy up the x-axis ticks
        breaks = bxp$x,
    labels = bxp$Sample
    ) +
    labs(
        # Tidy up the labels
        x = "Library",
        y = "Read Length"
    ) +
    theme_bw() # Get rid of the stupid grey background

That gives me this figure

enter image description here

Hope that solves it for you. I probably went the long way around though :)

$\endgroup$
1
  • $\begingroup$ Just what I wanted, thank you! $\endgroup$
    – Lasio
    Commented Jan 29, 2020 at 0:02
1
$\begingroup$

The plot you pasted in your question has two layers of information: i) fragment (or read) length, ii) sample.

To be able to draw the same plot with your data using ggplot2, you will need your data to be in the "long format" (your data as you presented now is in the "wide format"):

library(readr)
library(tidyr)
library(ggplot2)

my_data <- "
Length Sample1 Sample2
20 10 5
30 15 223
40 16 4923
50 293 239
60 3290 23
70 248 6
80 23 1
90 21 0
100 5 0
"

my_data <- read_delim(my_data, delim = " ")

my_data_long <- pivot_longer(my_data, cols = c("Sample1", "Sample2"))

> my_data_long
# A tibble: 18 x 3
   Length name    value
    <dbl> <chr>   <dbl>
 1     20 Sample1    10
 2     20 Sample2     5
 3     30 Sample1    15
 4     30 Sample2   223
 5     40 Sample1    16
 6     40 Sample2  4923
 7     50 Sample1   293
 8     50 Sample2   239
 9     60 Sample1  3290
10     60 Sample2    23
11     70 Sample1   248
12     70 Sample2     6
13     80 Sample1    23
14     80 Sample2     1
15     90 Sample1    21
16     90 Sample2     0
17    100 Sample1     5
18    100 Sample2     0

Moreover, boxplots are meaningful when you would like to visualize distributions, in your case you don't have all of your data points but only a summary of those, counts of read lengths. The best I could think of was using a third aesthetic to reflect the counts (after removing read lengths that were not observed):

# remove read lengths that are not observed
my_data_long <- my_data_long[my_data_long$value != 0,]

ggplot(my_data_long,
       aes(x = name, y = Length)) +
  geom_boxplot() +
  geom_point(aes(size = value))

enter image description here

EDIT: In a boxplot, the quantiles reflect the number of data points but the way I draw the boxplot (using Length and name columns) do not make use of such information. A better way of visualising the data would be the following (and is not that different from what @gringer suggested):

ggplot(my_data_long,
       aes(x = name, y = Length)) +
  geom_point(aes(size = value))

enter image description here

$\endgroup$
1
$\begingroup$

My first recommendation would be to use (i.e. ask for) the indidividual-level data, which is what ggplot2 expects.

Binned Data

If that's not possible, binned data can be shoehorned into a ggplot2 boxplot by forcing it back into individual values. My example here adds new unbinned rows to the table with a count of -1, then removes rows with a positive count:

library(tidyverse)
library(ggplot2)

read_table2("11196_input.txt") %>%
    pivot_longer(cols=c("Sample1", "Sample2"),
                 names_to="Sample", values_to="Count") %>%
    bind_rows(tibble(Length=rep(.$Length, .$Count),
                     Sample=rep(.$Sample, .$Count),
                     Count=rep(.$Length * 0 - 1, .$Count))) %>%
    filter(Count < 0) %>%
    select(-Count) -> data.tbl

data.tbl %>%
    ggplot() +
    aes(x = Sample, y = Length) +
    geom_boxplot()

boxplot of binned data

It would be possible to make the boxplot look more normal by modifying the rep functions to create values based on a distribution (e.g. using runif or rnorm), but that gets quite tricky because (for example) binned normal data would have different probability distributions for each bin.

Individual Values

If, on the other hand, individual values are available in long format, creating a boxplot becomes much easier because the data fits the format expected by ggplot2:

library(tidyverse)
library(ggplot2)

read.table("11196_input2.txt") -> data.tbl

data.tbl %>%
    ggplot() +
    aes(x = name, y = Readlength) +
    geom_boxplot()

boxplot of raw data

$\endgroup$
2
  • $\begingroup$ I don't think that the individual example worked -- the box plots should be different as both of the samples have different underlying data. $\endgroup$
    – Lasio
    Commented Jan 23, 2020 at 21:59
  • $\begingroup$ Ah, yeah. That's because the data isn't individual-level (i.e. one line per observation), and I didn't realise that the heading change from "count" to "value" still represented the same thing. I'm seeing 130M data points... that's quite a lot for ggplot2 to process. It'd probably be better to use stat_boxplot rather than geom_boxplot, and specify the boxplot points rather than the values. $\endgroup$
    – gringer
    Commented Jan 24, 2020 at 1:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.