Sum and Difference between numbers [closed] - math
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 11 months ago.
Improve this question
Is it possible to find two numbers a and b such that difference between a and b is equal to sum of the digits in a and b ?
Is it possible to write a general equation for this?
Yes it is possible. A general code solution / algorithm for finding it would look like this. A mathematical solution would be be found by asking on https://math.stackexchange.com/
The below code checks all integer values between 1 and 10 for a and b
let a,b;
const maxIter = 10
let solution = []
for (a = 1;a < maxIter; a++) {
for (b = 1;b < maxIter; b++) {
const digitsInAB = `${a}${b}`.length
if (a-b === digitsInAB || b-a === digitsInAB)
solution.push({a,b})
}
}
console.log(solution)
Is it possible to find two numbers a and b such that the difference between a and b is equal to sum of the digits in a and b ? Is it possible to write a general equation for this?
Ruby:
def digits(n)
ans = 0
while n > 0 do
ans += n % 10
n /= 10
end
return ans
end
Now we just check the possibilities exhaustively, but assuming a <= b, and noting that b can't be much bigger than a. It can be at most one digit bigger than a, so you can check from a + d(a) + 1 up to a + d(a) + 9 * (1+count of digits in a).
Here we have the first few: a + digits(a) + digits(b) = b.
0 + 0 + 0 = 0
0 + 0 + 1 = 1
0 + 0 + 2 = 2
0 + 0 + 3 = 3
0 + 0 + 4 = 4
0 + 0 + 5 = 5
0 + 0 + 6 = 6
0 + 0 + 7 = 7
0 + 0 + 8 = 8
0 + 0 + 9 = 9
9 + 9 + 2 = 20
9 + 9 + 3 = 21
9 + 9 + 4 = 22
9 + 9 + 5 = 23
9 + 9 + 6 = 24
9 + 9 + 7 = 25
9 + 9 + 8 = 26
9 + 9 + 9 = 27
9 + 9 + 10 = 28
9 + 9 + 11 = 29
18 + 9 + 3 = 30
18 + 9 + 4 = 31
18 + 9 + 5 = 32
18 + 9 + 6 = 33
18 + 9 + 7 = 34
18 + 9 + 8 = 35
18 + 9 + 9 = 36
18 + 9 + 10 = 37
18 + 9 + 11 = 38
18 + 9 + 12 = 39
27 + 9 + 4 = 40
27 + 9 + 5 = 41
27 + 9 + 6 = 42
27 + 9 + 7 = 43
27 + 9 + 8 = 44
27 + 9 + 9 = 45
27 + 9 + 10 = 46
27 + 9 + 11 = 47
27 + 9 + 12 = 48
27 + 9 + 13 = 49
36 + 9 + 5 = 50
36 + 9 + 6 = 51
36 + 9 + 7 = 52
36 + 9 + 8 = 53
36 + 9 + 9 = 54
36 + 9 + 10 = 55
36 + 9 + 11 = 56
36 + 9 + 12 = 57
36 + 9 + 13 = 58
36 + 9 + 14 = 59
45 + 9 + 6 = 60
45 + 9 + 7 = 61
45 + 9 + 8 = 62
45 + 9 + 9 = 63
45 + 9 + 10 = 64
45 + 9 + 11 = 65
45 + 9 + 12 = 66
45 + 9 + 13 = 67
45 + 9 + 14 = 68
45 + 9 + 15 = 69
54 + 9 + 7 = 70
54 + 9 + 8 = 71
54 + 9 + 9 = 72
54 + 9 + 10 = 73
54 + 9 + 11 = 74
54 + 9 + 12 = 75
54 + 9 + 13 = 76
54 + 9 + 14 = 77
54 + 9 + 15 = 78
54 + 9 + 16 = 79
63 + 9 + 8 = 80
63 + 9 + 9 = 81
63 + 9 + 10 = 82
63 + 9 + 11 = 83
63 + 9 + 12 = 84
63 + 9 + 13 = 85
63 + 9 + 14 = 86
63 + 9 + 15 = 87
63 + 9 + 16 = 88
63 + 9 + 17 = 89
72 + 9 + 9 = 90
72 + 9 + 10 = 91
72 + 9 + 11 = 92
72 + 9 + 12 = 93
72 + 9 + 13 = 94
72 + 9 + 14 = 95
72 + 9 + 15 = 96
72 + 9 + 16 = 97
72 + 9 + 17 = 98
72 + 9 + 18 = 99
90 + 9 + 1 = 100
90 + 9 + 2 = 101
90 + 9 + 3 = 102
90 + 9 + 4 = 103
90 + 9 + 5 = 104
90 + 9 + 6 = 105
90 + 9 + 7 = 106
90 + 9 + 8 = 107
90 + 9 + 9 = 108
90 + 9 + 10 = 109
99 + 18 + 3 = 120
99 + 18 + 4 = 121
99 + 18 + 5 = 122
99 + 18 + 6 = 123
99 + 18 + 7 = 124
99 + 18 + 8 = 125
99 + 18 + 9 = 126
99 + 18 + 10 = 127
99 + 18 + 11 = 128
99 + 18 + 12 = 129
108 + 9 + 3 = 120
108 + 9 + 4 = 121
108 + 9 + 5 = 122
108 + 9 + 6 = 123
108 + 9 + 7 = 124
108 + 9 + 8 = 125
108 + 9 + 9 = 126
108 + 9 + 10 = 127
108 + 9 + 11 = 128
108 + 9 + 12 = 129
117 + 9 + 4 = 130
117 + 9 + 5 = 131
117 + 9 + 6 = 132
117 + 9 + 7 = 133
117 + 9 + 8 = 134
117 + 9 + 9 = 135
117 + 9 + 10 = 136
117 + 9 + 11 = 137
117 + 9 + 12 = 138
117 + 9 + 13 = 139
126 + 9 + 5 = 140
126 + 9 + 6 = 141
126 + 9 + 7 = 142
126 + 9 + 8 = 143
126 + 9 + 9 = 144
126 + 9 + 10 = 145
126 + 9 + 11 = 146
126 + 9 + 12 = 147
126 + 9 + 13 = 148
126 + 9 + 14 = 149
135 + 9 + 6 = 150
135 + 9 + 7 = 151
135 + 9 + 8 = 152
135 + 9 + 9 = 153
135 + 9 + 10 = 154
135 + 9 + 11 = 155
135 + 9 + 12 = 156
135 + 9 + 13 = 157
135 + 9 + 14 = 158
135 + 9 + 15 = 159
144 + 9 + 7 = 160
144 + 9 + 8 = 161
144 + 9 + 9 = 162
144 + 9 + 10 = 163
144 + 9 + 11 = 164
144 + 9 + 12 = 165
144 + 9 + 13 = 166
144 + 9 + 14 = 167
144 + 9 + 15 = 168
144 + 9 + 16 = 169
153 + 9 + 8 = 170
153 + 9 + 9 = 171
153 + 9 + 10 = 172
153 + 9 + 11 = 173
153 + 9 + 12 = 174
153 + 9 + 13 = 175
153 + 9 + 14 = 176
153 + 9 + 15 = 177
153 + 9 + 16 = 178
153 + 9 + 17 = 179
162 + 9 + 9 = 180
162 + 9 + 10 = 181
162 + 9 + 11 = 182
162 + 9 + 12 = 183
162 + 9 + 13 = 184
162 + 9 + 14 = 185
162 + 9 + 15 = 186
162 + 9 + 16 = 187
162 + 9 + 17 = 188
162 + 9 + 18 = 189
171 + 9 + 10 = 190
171 + 9 + 11 = 191
171 + 9 + 12 = 192
171 + 9 + 13 = 193
171 + 9 + 14 = 194
171 + 9 + 15 = 195
171 + 9 + 16 = 196
171 + 9 + 17 = 197
171 + 9 + 18 = 198
171 + 9 + 19 = 199
189 + 18 + 3 = 210
189 + 18 + 4 = 211
189 + 18 + 5 = 212
189 + 18 + 6 = 213
189 + 18 + 7 = 214
189 + 18 + 8 = 215
189 + 18 + 9 = 216
189 + 18 + 10 = 217
189 + 18 + 11 = 218
189 + 18 + 12 = 219
198 + 18 + 4 = 220
198 + 18 + 5 = 221
198 + 18 + 6 = 222
198 + 18 + 7 = 223
198 + 18 + 8 = 224
198 + 18 + 9 = 225
198 + 18 + 10 = 226
198 + 18 + 11 = 227
198 + 18 + 12 = 228
198 + 18 + 13 = 229
207 + 9 + 4 = 220
207 + 9 + 5 = 221
207 + 9 + 6 = 222
207 + 9 + 7 = 223
207 + 9 + 8 = 224
207 + 9 + 9 = 225
207 + 9 + 10 = 226
207 + 9 + 11 = 227
207 + 9 + 12 = 228
207 + 9 + 13 = 229
216 + 9 + 5 = 230
216 + 9 + 6 = 231
216 + 9 + 7 = 232
216 + 9 + 8 = 233
216 + 9 + 9 = 234
216 + 9 + 10 = 235
216 + 9 + 11 = 236
216 + 9 + 12 = 237
216 + 9 + 13 = 238
216 + 9 + 14 = 239
225 + 9 + 6 = 240
225 + 9 + 7 = 241
225 + 9 + 8 = 242
225 + 9 + 9 = 243
225 + 9 + 10 = 244
225 + 9 + 11 = 245
225 + 9 + 12 = 246
225 + 9 + 13 = 247
225 + 9 + 14 = 248
225 + 9 + 15 = 249
234 + 9 + 7 = 250
234 + 9 + 8 = 251
234 + 9 + 9 = 252
234 + 9 + 10 = 253
234 + 9 + 11 = 254
234 + 9 + 12 = 255
234 + 9 + 13 = 256
234 + 9 + 14 = 257
234 + 9 + 15 = 258
234 + 9 + 16 = 259
243 + 9 + 8 = 260
243 + 9 + 9 = 261
243 + 9 + 10 = 262
243 + 9 + 11 = 263
243 + 9 + 12 = 264
243 + 9 + 13 = 265
243 + 9 + 14 = 266
243 + 9 + 15 = 267
243 + 9 + 16 = 268
243 + 9 + 17 = 269
252 + 9 + 9 = 270
252 + 9 + 10 = 271
252 + 9 + 11 = 272
252 + 9 + 12 = 273
252 + 9 + 13 = 274
252 + 9 + 14 = 275
252 + 9 + 15 = 276
252 + 9 + 16 = 277
252 + 9 + 17 = 278
252 + 9 + 18 = 279
261 + 9 + 10 = 280
261 + 9 + 11 = 281
261 + 9 + 12 = 282
261 + 9 + 13 = 283
261 + 9 + 14 = 284
261 + 9 + 15 = 285
261 + 9 + 16 = 286
261 + 9 + 17 = 287
261 + 9 + 18 = 288
261 + 9 + 19 = 289
270 + 9 + 11 = 290
270 + 9 + 12 = 291
270 + 9 + 13 = 292
270 + 9 + 14 = 293
270 + 9 + 15 = 294
270 + 9 + 16 = 295
270 + 9 + 17 = 296
270 + 9 + 18 = 297
270 + 9 + 19 = 298
270 + 9 + 20 = 299
279 + 18 + 3 = 300
279 + 18 + 4 = 301
279 + 18 + 5 = 302
279 + 18 + 6 = 303
279 + 18 + 7 = 304
279 + 18 + 8 = 305
279 + 18 + 9 = 306
279 + 18 + 10 = 307
279 + 18 + 11 = 308
279 + 18 + 12 = 309
288 + 18 + 4 = 310
288 + 18 + 5 = 311
288 + 18 + 6 = 312
288 + 18 + 7 = 313
288 + 18 + 8 = 314
288 + 18 + 9 = 315
288 + 18 + 10 = 316
288 + 18 + 11 = 317
288 + 18 + 12 = 318
288 + 18 + 13 = 319
297 + 18 + 5 = 320
297 + 18 + 6 = 321
297 + 18 + 7 = 322
297 + 18 + 8 = 323
297 + 18 + 9 = 324
297 + 18 + 10 = 325
297 + 18 + 11 = 326
297 + 18 + 12 = 327
297 + 18 + 13 = 328
297 + 18 + 14 = 329
306 + 9 + 5 = 320
306 + 9 + 6 = 321
306 + 9 + 7 = 322
306 + 9 + 8 = 323
306 + 9 + 9 = 324
306 + 9 + 10 = 325
306 + 9 + 11 = 326
306 + 9 + 12 = 327
306 + 9 + 13 = 328
306 + 9 + 14 = 329
315 + 9 + 6 = 330
315 + 9 + 7 = 331
315 + 9 + 8 = 332
315 + 9 + 9 = 333
315 + 9 + 10 = 334
315 + 9 + 11 = 335
315 + 9 + 12 = 336
315 + 9 + 13 = 337
315 + 9 + 14 = 338
315 + 9 + 15 = 339
324 + 9 + 7 = 340
324 + 9 + 8 = 341
324 + 9 + 9 = 342
324 + 9 + 10 = 343
324 + 9 + 11 = 344
324 + 9 + 12 = 345
324 + 9 + 13 = 346
324 + 9 + 14 = 347
324 + 9 + 15 = 348
324 + 9 + 16 = 349
333 + 9 + 8 = 350
333 + 9 + 9 = 351
333 + 9 + 10 = 352
333 + 9 + 11 = 353
333 + 9 + 12 = 354
333 + 9 + 13 = 355
333 + 9 + 14 = 356
333 + 9 + 15 = 357
333 + 9 + 16 = 358
333 + 9 + 17 = 359
342 + 9 + 9 = 360
342 + 9 + 10 = 361
342 + 9 + 11 = 362
342 + 9 + 12 = 363
342 + 9 + 13 = 364
342 + 9 + 14 = 365
342 + 9 + 15 = 366
342 + 9 + 16 = 367
342 + 9 + 17 = 368
342 + 9 + 18 = 369
351 + 9 + 10 = 370
351 + 9 + 11 = 371
351 + 9 + 12 = 372
351 + 9 + 13 = 373
351 + 9 + 14 = 374
351 + 9 + 15 = 375
351 + 9 + 16 = 376
351 + 9 + 17 = 377
351 + 9 + 18 = 378
351 + 9 + 19 = 379
360 + 9 + 11 = 380
360 + 9 + 12 = 381
360 + 9 + 13 = 382
360 + 9 + 14 = 383
360 + 9 + 15 = 384
360 + 9 + 16 = 385
360 + 9 + 17 = 386
360 + 9 + 18 = 387
360 + 9 + 19 = 388
360 + 9 + 20 = 389
378 + 18 + 4 = 400
378 + 18 + 5 = 401
378 + 18 + 6 = 402
378 + 18 + 7 = 403
378 + 18 + 8 = 404
378 + 18 + 9 = 405
378 + 18 + 10 = 406
378 + 18 + 11 = 407
378 + 18 + 12 = 408
378 + 18 + 13 = 409
387 + 18 + 5 = 410
387 + 18 + 6 = 411
387 + 18 + 7 = 412
387 + 18 + 8 = 413
387 + 18 + 9 = 414
387 + 18 + 10 = 415
387 + 18 + 11 = 416
387 + 18 + 12 = 417
387 + 18 + 13 = 418
387 + 18 + 14 = 419
396 + 18 + 6 = 420
396 + 18 + 7 = 421
396 + 18 + 8 = 422
396 + 18 + 9 = 423
396 + 18 + 10 = 424
396 + 18 + 11 = 425
396 + 18 + 12 = 426
396 + 18 + 13 = 427
396 + 18 + 14 = 428
396 + 18 + 15 = 429
405 + 9 + 6 = 420
405 + 9 + 7 = 421
405 + 9 + 8 = 422
405 + 9 + 9 = 423
405 + 9 + 10 = 424
405 + 9 + 11 = 425
405 + 9 + 12 = 426
405 + 9 + 13 = 427
405 + 9 + 14 = 428
405 + 9 + 15 = 429
414 + 9 + 7 = 430
414 + 9 + 8 = 431
414 + 9 + 9 = 432
414 + 9 + 10 = 433
414 + 9 + 11 = 434
414 + 9 + 12 = 435
414 + 9 + 13 = 436
414 + 9 + 14 = 437
414 + 9 + 15 = 438
414 + 9 + 16 = 439
423 + 9 + 8 = 440
423 + 9 + 9 = 441
423 + 9 + 10 = 442
423 + 9 + 11 = 443
423 + 9 + 12 = 444
423 + 9 + 13 = 445
423 + 9 + 14 = 446
423 + 9 + 15 = 447
423 + 9 + 16 = 448
423 + 9 + 17 = 449
432 + 9 + 9 = 450
432 + 9 + 10 = 451
432 + 9 + 11 = 452
432 + 9 + 12 = 453
432 + 9 + 13 = 454
432 + 9 + 14 = 455
432 + 9 + 15 = 456
432 + 9 + 16 = 457
432 + 9 + 17 = 458
432 + 9 + 18 = 459
441 + 9 + 10 = 460
441 + 9 + 11 = 461
441 + 9 + 12 = 462
441 + 9 + 13 = 463
441 + 9 + 14 = 464
441 + 9 + 15 = 465
441 + 9 + 16 = 466
441 + 9 + 17 = 467
441 + 9 + 18 = 468
441 + 9 + 19 = 469
450 + 9 + 11 = 470
450 + 9 + 12 = 471
450 + 9 + 13 = 472
450 + 9 + 14 = 473
450 + 9 + 15 = 474
450 + 9 + 16 = 475
450 + 9 + 17 = 476
450 + 9 + 18 = 477
450 + 9 + 19 = 478
450 + 9 + 20 = 479
459 + 18 + 13 = 490
459 + 18 + 14 = 491
459 + 18 + 15 = 492
459 + 18 + 16 = 493
459 + 18 + 17 = 494
459 + 18 + 18 = 495
459 + 18 + 19 = 496
459 + 18 + 20 = 497
459 + 18 + 21 = 498
459 + 18 + 22 = 499
477 + 18 + 5 = 500
477 + 18 + 6 = 501
477 + 18 + 7 = 502
477 + 18 + 8 = 503
477 + 18 + 9 = 504
477 + 18 + 10 = 505
477 + 18 + 11 = 506
477 + 18 + 12 = 507
477 + 18 + 13 = 508
477 + 18 + 14 = 509
486 + 18 + 6 = 510
486 + 18 + 7 = 511
486 + 18 + 8 = 512
486 + 18 + 9 = 513
486 + 18 + 10 = 514
486 + 18 + 11 = 515
486 + 18 + 12 = 516
486 + 18 + 13 = 517
486 + 18 + 14 = 518
486 + 18 + 15 = 519
495 + 18 + 7 = 520
495 + 18 + 8 = 521
495 + 18 + 9 = 522
495 + 18 + 10 = 523
495 + 18 + 11 = 524
495 + 18 + 12 = 525
495 + 18 + 13 = 526
495 + 18 + 14 = 527
495 + 18 + 15 = 528
495 + 18 + 16 = 529
504 + 9 + 7 = 520
504 + 9 + 8 = 521
504 + 9 + 9 = 522
504 + 9 + 10 = 523
504 + 9 + 11 = 524
504 + 9 + 12 = 525
504 + 9 + 13 = 526
504 + 9 + 14 = 527
504 + 9 + 15 = 528
504 + 9 + 16 = 529
513 + 9 + 8 = 530
513 + 9 + 9 = 531
513 + 9 + 10 = 532
513 + 9 + 11 = 533
513 + 9 + 12 = 534
513 + 9 + 13 = 535
513 + 9 + 14 = 536
513 + 9 + 15 = 537
513 + 9 + 16 = 538
513 + 9 + 17 = 539
522 + 9 + 9 = 540
522 + 9 + 10 = 541
522 + 9 + 11 = 542
522 + 9 + 12 = 543
522 + 9 + 13 = 544
522 + 9 + 14 = 545
522 + 9 + 15 = 546
522 + 9 + 16 = 547
522 + 9 + 17 = 548
522 + 9 + 18 = 549
531 + 9 + 10 = 550
531 + 9 + 11 = 551
531 + 9 + 12 = 552
531 + 9 + 13 = 553
531 + 9 + 14 = 554
531 + 9 + 15 = 555
531 + 9 + 16 = 556
531 + 9 + 17 = 557
531 + 9 + 18 = 558
531 + 9 + 19 = 559
540 + 9 + 11 = 560
540 + 9 + 12 = 561
540 + 9 + 13 = 562
540 + 9 + 14 = 563
540 + 9 + 15 = 564
540 + 9 + 16 = 565
540 + 9 + 17 = 566
540 + 9 + 18 = 567
540 + 9 + 19 = 568
540 + 9 + 20 = 569
549 + 18 + 13 = 580
549 + 18 + 14 = 581
549 + 18 + 15 = 582
549 + 18 + 16 = 583
549 + 18 + 17 = 584
549 + 18 + 18 = 585
549 + 18 + 19 = 586
549 + 18 + 20 = 587
549 + 18 + 21 = 588
549 + 18 + 22 = 589
558 + 18 + 14 = 590
558 + 18 + 15 = 591
558 + 18 + 16 = 592
558 + 18 + 17 = 593
558 + 18 + 18 = 594
558 + 18 + 19 = 595
558 + 18 + 20 = 596
558 + 18 + 21 = 597
558 + 18 + 22 = 598
558 + 18 + 23 = 599
576 + 18 + 6 = 600
576 + 18 + 7 = 601
576 + 18 + 8 = 602
576 + 18 + 9 = 603
576 + 18 + 10 = 604
576 + 18 + 11 = 605
576 + 18 + 12 = 606
576 + 18 + 13 = 607
576 + 18 + 14 = 608
576 + 18 + 15 = 609
585 + 18 + 7 = 610
585 + 18 + 8 = 611
585 + 18 + 9 = 612
585 + 18 + 10 = 613
585 + 18 + 11 = 614
585 + 18 + 12 = 615
585 + 18 + 13 = 616
585 + 18 + 14 = 617
585 + 18 + 15 = 618
585 + 18 + 16 = 619
594 + 18 + 8 = 620
594 + 18 + 9 = 621
594 + 18 + 10 = 622
594 + 18 + 11 = 623
594 + 18 + 12 = 624
594 + 18 + 13 = 625
594 + 18 + 14 = 626
594 + 18 + 15 = 627
594 + 18 + 16 = 628
594 + 18 + 17 = 629
603 + 9 + 8 = 620
603 + 9 + 9 = 621
603 + 9 + 10 = 622
603 + 9 + 11 = 623
603 + 9 + 12 = 624
603 + 9 + 13 = 625
603 + 9 + 14 = 626
603 + 9 + 15 = 627
603 + 9 + 16 = 628
603 + 9 + 17 = 629
612 + 9 + 9 = 630
612 + 9 + 10 = 631
612 + 9 + 11 = 632
612 + 9 + 12 = 633
612 + 9 + 13 = 634
612 + 9 + 14 = 635
612 + 9 + 15 = 636
612 + 9 + 16 = 637
612 + 9 + 17 = 638
612 + 9 + 18 = 639
621 + 9 + 10 = 640
621 + 9 + 11 = 641
621 + 9 + 12 = 642
621 + 9 + 13 = 643
621 + 9 + 14 = 644
621 + 9 + 15 = 645
621 + 9 + 16 = 646
621 + 9 + 17 = 647
621 + 9 + 18 = 648
621 + 9 + 19 = 649
630 + 9 + 11 = 650
630 + 9 + 12 = 651
630 + 9 + 13 = 652
630 + 9 + 14 = 653
630 + 9 + 15 = 654
630 + 9 + 16 = 655
630 + 9 + 17 = 656
630 + 9 + 18 = 657
630 + 9 + 19 = 658
630 + 9 + 20 = 659
639 + 18 + 13 = 670
639 + 18 + 14 = 671
639 + 18 + 15 = 672
639 + 18 + 16 = 673
639 + 18 + 17 = 674
639 + 18 + 18 = 675
639 + 18 + 19 = 676
639 + 18 + 20 = 677
639 + 18 + 21 = 678
639 + 18 + 22 = 679
648 + 18 + 14 = 680
648 + 18 + 15 = 681
648 + 18 + 16 = 682
648 + 18 + 17 = 683
648 + 18 + 18 = 684
648 + 18 + 19 = 685
648 + 18 + 20 = 686
648 + 18 + 21 = 687
648 + 18 + 22 = 688
648 + 18 + 23 = 689
657 + 18 + 15 = 690
657 + 18 + 16 = 691
657 + 18 + 17 = 692
657 + 18 + 18 = 693
657 + 18 + 19 = 694
657 + 18 + 20 = 695
657 + 18 + 21 = 696
657 + 18 + 22 = 697
657 + 18 + 23 = 698
657 + 18 + 24 = 699
675 + 18 + 7 = 700
675 + 18 + 8 = 701
675 + 18 + 9 = 702
675 + 18 + 10 = 703
675 + 18 + 11 = 704
675 + 18 + 12 = 705
675 + 18 + 13 = 706
675 + 18 + 14 = 707
675 + 18 + 15 = 708
675 + 18 + 16 = 709
684 + 18 + 8 = 710
684 + 18 + 9 = 711
684 + 18 + 10 = 712
684 + 18 + 11 = 713
684 + 18 + 12 = 714
684 + 18 + 13 = 715
684 + 18 + 14 = 716
684 + 18 + 15 = 717
684 + 18 + 16 = 718
684 + 18 + 17 = 719
693 + 18 + 9 = 720
693 + 18 + 10 = 721
693 + 18 + 11 = 722
693 + 18 + 12 = 723
693 + 18 + 13 = 724
693 + 18 + 14 = 725
693 + 18 + 15 = 726
693 + 18 + 16 = 727
693 + 18 + 17 = 728
693 + 18 + 18 = 729
702 + 9 + 9 = 720
702 + 9 + 10 = 721
702 + 9 + 11 = 722
702 + 9 + 12 = 723
702 + 9 + 13 = 724
702 + 9 + 14 = 725
702 + 9 + 15 = 726
702 + 9 + 16 = 727
702 + 9 + 17 = 728
702 + 9 + 18 = 729
711 + 9 + 10 = 730
711 + 9 + 11 = 731
711 + 9 + 12 = 732
711 + 9 + 13 = 733
711 + 9 + 14 = 734
711 + 9 + 15 = 735
711 + 9 + 16 = 736
711 + 9 + 17 = 737
711 + 9 + 18 = 738
711 + 9 + 19 = 739
720 + 9 + 11 = 740
720 + 9 + 12 = 741
720 + 9 + 13 = 742
720 + 9 + 14 = 743
720 + 9 + 15 = 744
720 + 9 + 16 = 745
720 + 9 + 17 = 746
720 + 9 + 18 = 747
720 + 9 + 19 = 748
720 + 9 + 20 = 749
729 + 18 + 13 = 760
729 + 18 + 14 = 761
729 + 18 + 15 = 762
729 + 18 + 16 = 763
729 + 18 + 17 = 764
729 + 18 + 18 = 765
729 + 18 + 19 = 766
729 + 18 + 20 = 767
729 + 18 + 21 = 768
729 + 18 + 22 = 769
738 + 18 + 14 = 770
738 + 18 + 15 = 771
738 + 18 + 16 = 772
738 + 18 + 17 = 773
738 + 18 + 18 = 774
738 + 18 + 19 = 775
738 + 18 + 20 = 776
738 + 18 + 21 = 777
738 + 18 + 22 = 778
738 + 18 + 23 = 779
747 + 18 + 15 = 780
747 + 18 + 16 = 781
747 + 18 + 17 = 782
747 + 18 + 18 = 783
747 + 18 + 19 = 784
747 + 18 + 20 = 785
747 + 18 + 21 = 786
747 + 18 + 22 = 787
747 + 18 + 23 = 788
747 + 18 + 24 = 789
756 + 18 + 16 = 790
756 + 18 + 17 = 791
756 + 18 + 18 = 792
756 + 18 + 19 = 793
756 + 18 + 20 = 794
756 + 18 + 21 = 795
756 + 18 + 22 = 796
756 + 18 + 23 = 797
756 + 18 + 24 = 798
756 + 18 + 25 = 799
774 + 18 + 8 = 800
774 + 18 + 9 = 801
774 + 18 + 10 = 802
774 + 18 + 11 = 803
774 + 18 + 12 = 804
774 + 18 + 13 = 805
774 + 18 + 14 = 806
774 + 18 + 15 = 807
774 + 18 + 16 = 808
774 + 18 + 17 = 809
783 + 18 + 9 = 810
783 + 18 + 10 = 811
783 + 18 + 11 = 812
783 + 18 + 12 = 813
783 + 18 + 13 = 814
783 + 18 + 14 = 815
783 + 18 + 15 = 816
783 + 18 + 16 = 817
783 + 18 + 17 = 818
783 + 18 + 18 = 819
792 + 18 + 10 = 820
792 + 18 + 11 = 821
792 + 18 + 12 = 822
792 + 18 + 13 = 823
792 + 18 + 14 = 824
792 + 18 + 15 = 825
792 + 18 + 16 = 826
792 + 18 + 17 = 827
792 + 18 + 18 = 828
792 + 18 + 19 = 829
801 + 9 + 10 = 820
801 + 9 + 11 = 821
801 + 9 + 12 = 822
801 + 9 + 13 = 823
801 + 9 + 14 = 824
801 + 9 + 15 = 825
801 + 9 + 16 = 826
801 + 9 + 17 = 827
801 + 9 + 18 = 828
801 + 9 + 19 = 829
810 + 9 + 11 = 830
810 + 9 + 12 = 831
810 + 9 + 13 = 832
810 + 9 + 14 = 833
810 + 9 + 15 = 834
810 + 9 + 16 = 835
810 + 9 + 17 = 836
810 + 9 + 18 = 837
810 + 9 + 19 = 838
810 + 9 + 20 = 839
819 + 18 + 13 = 850
819 + 18 + 14 = 851
819 + 18 + 15 = 852
819 + 18 + 16 = 853
819 + 18 + 17 = 854
819 + 18 + 18 = 855
819 + 18 + 19 = 856
819 + 18 + 20 = 857
819 + 18 + 21 = 858
819 + 18 + 22 = 859
828 + 18 + 14 = 860
828 + 18 + 15 = 861
828 + 18 + 16 = 862
828 + 18 + 17 = 863
828 + 18 + 18 = 864
828 + 18 + 19 = 865
828 + 18 + 20 = 866
828 + 18 + 21 = 867
828 + 18 + 22 = 868
828 + 18 + 23 = 869
837 + 18 + 15 = 870
837 + 18 + 16 = 871
837 + 18 + 17 = 872
837 + 18 + 18 = 873
837 + 18 + 19 = 874
837 + 18 + 20 = 875
837 + 18 + 21 = 876
837 + 18 + 22 = 877
837 + 18 + 23 = 878
837 + 18 + 24 = 879
846 + 18 + 16 = 880
846 + 18 + 17 = 881
846 + 18 + 18 = 882
846 + 18 + 19 = 883
846 + 18 + 20 = 884
846 + 18 + 21 = 885
846 + 18 + 22 = 886
846 + 18 + 23 = 887
846 + 18 + 24 = 888
846 + 18 + 25 = 889
855 + 18 + 17 = 890
855 + 18 + 18 = 891
855 + 18 + 19 = 892
855 + 18 + 20 = 893
855 + 18 + 21 = 894
855 + 18 + 22 = 895
855 + 18 + 23 = 896
855 + 18 + 24 = 897
855 + 18 + 25 = 898
855 + 18 + 26 = 899
873 + 18 + 9 = 900
873 + 18 + 10 = 901
873 + 18 + 11 = 902
873 + 18 + 12 = 903
873 + 18 + 13 = 904
873 + 18 + 14 = 905
873 + 18 + 15 = 906
873 + 18 + 16 = 907
873 + 18 + 17 = 908
873 + 18 + 18 = 909
882 + 18 + 10 = 910
882 + 18 + 11 = 911
882 + 18 + 12 = 912
882 + 18 + 13 = 913
882 + 18 + 14 = 914
882 + 18 + 15 = 915
882 + 18 + 16 = 916
882 + 18 + 17 = 917
882 + 18 + 18 = 918
882 + 18 + 19 = 919
891 + 18 + 11 = 920
891 + 18 + 12 = 921
891 + 18 + 13 = 922
891 + 18 + 14 = 923
891 + 18 + 15 = 924
891 + 18 + 16 = 925
891 + 18 + 17 = 926
891 + 18 + 18 = 927
891 + 18 + 19 = 928
891 + 18 + 20 = 929
900 + 9 + 11 = 920
900 + 9 + 12 = 921
900 + 9 + 13 = 922
900 + 9 + 14 = 923
900 + 9 + 15 = 924
900 + 9 + 16 = 925
900 + 9 + 17 = 926
900 + 9 + 18 = 927
900 + 9 + 19 = 928
900 + 9 + 20 = 929
909 + 18 + 13 = 940
909 + 18 + 14 = 941
909 + 18 + 15 = 942
909 + 18 + 16 = 943
909 + 18 + 17 = 944
909 + 18 + 18 = 945
909 + 18 + 19 = 946
909 + 18 + 20 = 947
909 + 18 + 21 = 948
909 + 18 + 22 = 949
918 + 18 + 14 = 950
918 + 18 + 15 = 951
918 + 18 + 16 = 952
918 + 18 + 17 = 953
918 + 18 + 18 = 954
918 + 18 + 19 = 955
918 + 18 + 20 = 956
927 + 18 + 16 = 961
927 + 18 + 17 = 962
927 + 18 + 18 = 963
927 + 18 + 19 = 964
927 + 18 + 20 = 965
927 + 18 + 21 = 966
927 + 18 + 22 = 967
927 + 18 + 23 = 968
927 + 18 + 24 = 969
936 + 18 + 16 = 970
936 + 18 + 17 = 971
936 + 18 + 18 = 972
936 + 18 + 19 = 973
936 + 18 + 20 = 974
936 + 18 + 21 = 975
936 + 18 + 22 = 976
936 + 18 + 23 = 977
936 + 18 + 24 = 978
936 + 18 + 25 = 979
945 + 18 + 17 = 980
945 + 18 + 18 = 981
945 + 18 + 19 = 982
945 + 18 + 20 = 983
945 + 18 + 21 = 984
945 + 18 + 22 = 985
945 + 18 + 23 = 986
945 + 18 + 24 = 987
945 + 18 + 25 = 988
945 + 18 + 26 = 989
954 + 18 + 18 = 990
954 + 18 + 19 = 991
954 + 18 + 20 = 992
954 + 18 + 21 = 993
954 + 18 + 22 = 994
954 + 18 + 23 = 995
954 + 18 + 24 = 996
954 + 18 + 25 = 997
954 + 18 + 26 = 998
954 + 18 + 27 = 999
981 + 18 + 1 = 1000
981 + 18 + 2 = 1001
981 + 18 + 3 = 1002
981 + 18 + 4 = 1003
981 + 18 + 5 = 1004
981 + 18 + 6 = 1005
981 + 18 + 7 = 1006
981 + 18 + 8 = 1007
981 + 18 + 9 = 1008
981 + 18 + 10 = 1009
990 + 18 + 2 = 1010
990 + 18 + 3 = 1011
990 + 18 + 4 = 1012
990 + 18 + 5 = 1013
990 + 18 + 6 = 1014
990 + 18 + 7 = 1015
990 + 18 + 8 = 1016
990 + 18 + 9 = 1017
990 + 18 + 10 = 1018
990 + 18 + 11 = 1019
999 + 27 + 4 = 1030
999 + 27 + 5 = 1031
999 + 27 + 6 = 1032
999 + 27 + 7 = 1033
999 + 27 + 8 = 1034
999 + 27 + 9 = 1035
999 + 27 + 10 = 1036
999 + 27 + 11 = 1037
999 + 27 + 12 = 1038
999 + 27 + 13 = 1039
Related
Group by sum specific column in R
df <- data.frame(items=sample(LETTERS,replace= T),quantity=sample(1:100,26,replace=FALSE),price=sample(100:1000,26,replace=FALSE)) I want to group_by sum quantity is about 500(ballpark) , When count close about 500 put the same group,like below Any help would be appreciated. Updated Because the condition need to change, I reset the threshold to 250, I summarize to find the max total value for each group, and then, How could I change the the total of group6 < 200 , into group5. I think about using ifelse but can't work successfully. set.seed(123) df <- data.frame(items=sample(LETTERS,replace= T),quantity=sample(1:100,26,replace=FALSE),price=sample(100:1000,26,replace=FALSE)) df$group=cumsum(c(1,ifelse(diff(cumsum(df$quantity)%% 250) < 0,1,0))) df$total=ave(df$quantity,df$group,FUN=cumsum) df %>% group_by(group) %>% summarise(max = max(total, na.rm=TRUE)) # A tibble: 6 × 2 group max <dbl> <int> 1 1 238 2 2 254 3 3 256 4 4 246 5 5 237 6 6 101 I want get like > df items quantity price group total 1 O 36 393 1 36 2 S 78 376 1 114 3 N 81 562 1 195 4 C 43 140 1 238 5 J 76 530 2 76 6 R 15 189 2 91 7 V 32 415 2 123 8 K 7 322 2 130 9 E 9 627 2 139 10 T 41 215 2 180 11 N 74 705 2 254 12 V 23 873 3 23 13 Y 27 846 3 50 14 Z 60 555 3 110 15 E 53 697 3 163 16 S 93 953 3 256 17 Y 86 138 4 86 18 Y 88 258 4 174 19 I 38 851 4 212 20 C 34 308 4 246 21 H 69 473 5 69 22 Z 72 917 5 141 23 G 96 133 5 237 24 J 63 615 5 300 25 I 13 112 5 376 26 S 25 168 5 477 Thank you for any helping all the time.
Base R set.seed(123) df <- data.frame(items=sample(LETTERS,replace= T),quantity=sample(1:100,26,replace=FALSE),price=sample(100:1000,26,replace=FALSE)) df$group=cumsum(c(1,ifelse(diff(cumsum(df$quantity)%%500)<0,1,0))) df$total=ave(df$quantity,df$group,FUN=cumsum) items quantity price group total 1 O 36 393 1 36 2 S 78 376 1 114 3 N 81 562 1 195 4 C 43 140 1 238 5 J 76 530 1 314 6 R 15 189 1 329 7 V 32 415 1 361 8 K 7 322 1 368 9 E 9 627 1 377 10 T 41 215 1 418 11 N 74 705 1 492 12 V 23 873 2 23 13 Y 27 846 2 50 14 Z 60 555 2 110 15 E 53 697 2 163 16 S 93 953 2 256 17 Y 86 138 2 342 18 Y 88 258 2 430 19 I 38 851 2 468 20 C 34 308 2 502 21 H 69 473 3 69 22 Z 72 917 3 141 23 G 96 133 3 237 24 J 63 615 3 300 25 I 13 112 3 313 26 S 25 168 3 338
You could use Reduce(..., accumulate = TRUE) to find where the first cumulative quantity >= 500. set.seed(123) df <- data.frame(items=sample(LETTERS,replace= T),quantity=sample(1:100,26,replace=FALSE),price=sample(100:1000,26,replace=FALSE)) library(dplyr) df %>% group_by(group = lag(cumsum(Reduce(\(x, y) { z <- x + y if(z < 500) z else 0 }, quantity, accumulate = TRUE) == 0) + 1, default = 1)) %>% mutate(total = sum(quantity)) %>% ungroup() # A tibble: 26 × 5 items quantity price group total <chr> <int> <int> <dbl> <int> 1 O 36 393 1 515 2 S 78 376 1 515 3 N 81 562 1 515 4 C 43 140 1 515 5 J 76 530 1 515 6 R 15 189 1 515 7 V 32 415 1 515 8 K 7 322 1 515 9 E 9 627 1 515 10 T 41 215 1 515 11 N 74 705 1 515 12 V 23 873 1 515 13 Y 27 846 2 548 14 Z 60 555 2 548 15 E 53 697 2 548 16 S 93 953 2 548 17 Y 86 138 2 548 18 Y 88 258 2 548 19 I 38 851 2 548 20 C 34 308 2 548 21 H 69 473 2 548 22 Z 72 917 3 269 23 G 96 133 3 269 24 J 63 615 3 269 25 I 13 112 3 269 26 S 25 168 3 269
Here is a base R solution. The groups break after the cumulative sum passes a threshold. The output of aggregate shows that all cumulative sums are above thres except for the last one. set.seed(2022) df <- data.frame(items=sample(LETTERS,replace= T), quantity=sample(1:100,26,replace=FALSE), price=sample(100:1000,26,replace=FALSE)) f <- function(x, thres) { grp <- integer(length(x)) run <- 0 current_grp <- 0L for(i in seq_along(x)) { run <- run + x[i] grp[i] <- current_grp if(run > thres) { current_grp <- current_grp + 1L run <- 0 } } grp } thres <- 500 group <- f(df$quantity, thres) aggregate(quantity ~ group, df, sum) #> group quantity #> 1 0 552 #> 2 1 513 #> 3 2 214 ave(df$quantity, group, FUN = cumsum) #> [1] 70 133 155 224 235 327 347 409 481 484 552 29 95 129 224 263 294 377 433 #> [20] 434 453 513 50 91 182 214 Created on 2022-09-06 by the reprex package (v2.0.1) Edit To assign groups and total quantities to the data can be done as follows. df$group <- f(df$quantity, thres) df$total_quantity <- ave(df$quantity, df$group, FUN = cumsum) head(df) #> items quantity price group total_quantity #> 1 D 70 731 0 70 #> 2 S 63 516 0 133 #> 3 N 22 710 0 155 #> 4 W 69 829 0 224 #> 5 K 11 887 0 235 #> 6 D 92 317 0 327 Created on 2022-09-06 by the reprex package (v2.0.1) Edit 2 To assign only the total quantity per group use sum instead of cumsum. df$total_quantity <- ave(df$quantity, df$group, FUN = sum)
Plotting Partial Least Squares Regression (plsr) biplot with ggplot2
Using the data.frame below (Source: http://eric.univ-lyon2.fr/~ricco/tanagra/fichiers/en_Tanagra_PLSR_Software_Comparison.pdf) Data df <- read.table(text = c(" diesel twodoors sportsstyle wheelbase length width height curbweight enginesize horsepower horse_per_weight conscity price symboling 0 1 0 97 172 66 56 2209 109 85 0.0385 8.7 7975 2 0 0 0 100 177 66 54 2337 109 102 0.0436 9.8 13950 2 0 0 0 116 203 72 57 3740 234 155 0.0414 14.7 34184 -1 0 1 1 103 184 68 52 3016 171 161 0.0534 12.4 15998 3 0 0 0 101 177 65 54 2765 164 121 0.0438 11.2 21105 0 0 1 0 90 169 65 52 2756 194 207 0.0751 13.8 34028 3 1 0 0 105 175 66 54 2700 134 72 0.0267 7.6 18344 0 0 0 0 108 187 68 57 3020 120 97 0.0321 12.4 11900 0 0 0 1 94 157 64 51 1967 90 68 0.0346 7.6 6229 1 0 1 0 95 169 64 53 2265 98 112 0.0494 9.0 9298 1 1 0 0 96 166 64 53 2275 110 56 0.0246 6.9 7898 0 0 1 0 100 177 66 53 2507 136 110 0.0439 12.4 15250 2 0 1 1 94 157 64 51 1876 90 68 0.0362 6.4 5572 1 0 0 0 95 170 64 54 2024 97 69 0.0341 7.6 7349 1 0 1 1 95 171 66 52 2823 152 154 0.0546 12.4 16500 1 0 0 0 103 175 65 60 2535 122 88 0.0347 9.8 8921 -1 0 0 0 113 200 70 53 4066 258 176 0.0433 15.7 32250 0 0 0 0 95 165 64 55 1938 97 69 0.0356 7.6 6849 1 1 0 0 97 172 66 56 2319 97 68 0.0293 6.4 9495 2 0 0 0 97 172 66 56 2275 109 85 0.0374 8.7 8495 2"), header = T) and this Code library(pls) Y <- as.matrix(df[,14]) X <- as.matrix(df[,1:11]) df.pls <- mvr(Y ~ X, ncomp = 3, method = "oscorespls", scale = T) plot(df.pls, "biplot") I got this Biplot Any help to plot the pls biplot using ggplot2 will be appreciated?
#Read data df <- read.table(text = c(" diesel twodoors sportsstyle wheelbase length width height curbweight enginesize horsepower horse_per_weight conscity price symboling 0 1 0 97 172 66 56 2209 109 85 0.0385 8.7 7975 2 0 0 0 100 177 66 54 2337 109 102 0.0436 9.8 13950 2 0 0 0 116 203 72 57 3740 234 155 0.0414 14.7 34184 -1 0 1 1 103 184 68 52 3016 171 161 0.0534 12.4 15998 3 0 0 0 101 177 65 54 2765 164 121 0.0438 11.2 21105 0 0 1 0 90 169 65 52 2756 194 207 0.0751 13.8 34028 3 1 0 0 105 175 66 54 2700 134 72 0.0267 7.6 18344 0 0 0 0 108 187 68 57 3020 120 97 0.0321 12.4 11900 0 0 0 1 94 157 64 51 1967 90 68 0.0346 7.6 6229 1 0 1 0 95 169 64 53 2265 98 112 0.0494 9.0 9298 1 1 0 0 96 166 64 53 2275 110 56 0.0246 6.9 7898 0 0 1 0 100 177 66 53 2507 136 110 0.0439 12.4 15250 2 0 1 1 94 157 64 51 1876 90 68 0.0362 6.4 5572 1 0 0 0 95 170 64 54 2024 97 69 0.0341 7.6 7349 1 0 1 1 95 171 66 52 2823 152 154 0.0546 12.4 16500 1 0 0 0 103 175 65 60 2535 122 88 0.0347 9.8 8921 -1 0 0 0 113 200 70 53 4066 258 176 0.0433 15.7 32250 0 0 0 0 95 165 64 55 1938 97 69 0.0356 7.6 6849 1 1 0 0 97 172 66 56 2319 97 68 0.0293 6.4 9495 2 0 0 0 97 172 66 56 2275 109 85 0.0374 8.7 8495 2"), header = T) #Run OP's code library(pls) library(ggplot2) Y <- as.matrix(df[,14]) X <- as.matrix(df[,1:11]) df.pls <- mvr(Y ~ X, ncomp = 3, method = "oscorespls", scale = T) #Extract information from mvr object df2<-df.pls$scores comp1a<-df2[,1] comp2a<-df2[,2] df2<-as.data.frame(cbind(comp1a, comp2a)) df1<-df.pls$loadings comp1<-df1[,1] comp2<-df1[,2] names<-df1[,0] df1<-as.data.frame(cbind(names, comp1, comp2)) #Generate two plots and overlay #Plot 1 p1<-ggplot(data=df1, aes(comp1,comp2))+ ylab("")+xlab("")+ggtitle("X scores and X Loadings")+ theme_bw() + theme(panel.border = element_rect(colour = "black", fill=NA, size=1),panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))+ geom_text(aes(label=rownames(df1)), color="red")+ scale_x_continuous(breaks = c(-0.6,-0.4,-0.2,0,0.2,0.4,0.6))+ scale_y_continuous(breaks = c(-0.6,-0.4,-0.2,0,0.2,0.4,0.6))+ coord_fixed(ylim=c(-0.6, 0.6),xlim=c(-0.6, 0.6))+ theme(axis.ticks = element_line(colour = "red")) + theme(axis.text.y=element_text(angle = 90, hjust = 0.65)) + theme(axis.text.y = element_text(margin=margin(10,10,10,5,"pt"))) #Plot 2 p2<-ggplot(data=df2, aes(comp1a,comp2a))+ ylab("Comp 2")+xlab("Comp 1")+ggtitle("X scores and X Loadings")+ theme_bw() + theme(panel.border = element_rect(colour = "black", fill=NA, size=1), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))+ geom_text(aes(label=rownames(df2)))+ xlim(-4,4)+ylim(-4,4)+ scale_y_continuous(breaks = c(-4,-2,0,2))+ coord_cartesian(ylim=c(-4, 4))+ scale_x_continuous(breaks = c(-4,-2,0,2)) + theme(plot.title = element_text(face="bold"))+ theme(axis.text.y=element_text(angle = 90, hjust = 0.65)) #Function to overlay plots in order to get two graphs with different axes on same plot library(grid) library(gtable) ggplot_dual_axis = function(plot1, plot2, which.axis = "x") { # Update plot with transparent panel plot2 = plot2 + theme(panel.background = element_rect(fill = NA)) grid.newpage() # Increase right margin if which.axis == "y" if(which.axis == "y") plot1 = plot1 + theme(plot.margin = unit(c(0.7, 1.5, 0.4, 0.4), "cm")) # Extract gtable g1 = ggplot_gtable(ggplot_build(plot1)) g2 = ggplot_gtable(ggplot_build(plot2)) # Overlap the panel of the second plot on that of the first pp = c(subset(g1$layout, name == "panel", se = t:r)) g = gtable_add_grob(g1, g2$grobs[[which(g2$layout$name=="panel")]], pp$t, pp$l, pp$b, pp$l) # Steal axis from second plot and modify axis.lab = ifelse(which.axis == "x", "axis-b", "axis-l") ia = which(g2$layout$name == axis.lab) ga = g2$grobs[[ia]] ax = ga$children[[2]] # Switch position of ticks and labels if(which.axis == "x") ax$heights = rev(ax$heights) else ax$widths = rev(ax$widths) ax$grobs = rev(ax$grobs) if(which.axis == "x") ax$grobs[[2]]$y = ax$grobs[[2]]$y - unit(1, "npc") + unit(0.15, "cm") else ax$grobs[[1]]$x = ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm") # Modify existing row to be tall enough for axis if(which.axis == "x") g$heights[[2]] = g$heights[g2$layout[ia,]$t] # Add new row or column for axis label if(which.axis == "x") { g = gtable_add_grob(g, ax, 2, 4, 2, 4) g = gtable_add_rows(g, g2$heights[1], 1) g = gtable_add_grob(g, g2$grob[[6]], 2, 4, 2, 4) } else { g = gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1) g = gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b) g = gtable_add_grob(g, g2$grob[[7]], pp$t, length(g$widths), pp$b - 1) } # Draw it grid.draw(g) } #Run function on individual plots ggplot_dual_axis(p2, p1, "y")
Align two legends in one plot
I would like to add legends for two geom_line and a geom_point at the same time, but the legends were not align to each other. So how to align the two legends and adjust legend positions? Thank you in advance! My data: df1: x1 y1 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 0 10 0 11 0 12 0 13 0 14 0 15 0 16 0 17 0 18 0 19 0 20 0 21 0 22 0 23 9.2 24 18.5 25 27.6 26 36.8 27 46.1 28 54.2 29 63.4 30 72.6 31 81.7 32 88.9 33 93 34 99.1 35 105.4 36 110 37 118.3 38 128.2 39 138 40 146.9 41 155.1 42 162.5 43 165.7 44 169.2 45 174.2 46 176.3 47 183.8 48 187.8 49 194.2 50 200.7 51 203.4 52 204.7 53 209.5 54 214.5 55 219.6 56 224.1 57 228.5 58 232.8 59 237 60 239.5 61 242.7 62 243.1 63 244.6 64 245 65 246.6 66 248.6 67 251 68 253 69 255 70 256.7 71 256.7 df2: x2 y2 24 0.006525 32 0.072525 39 0.120025 46 0.1601418 53 0.1972939 60 0.2226233 68 0.2312895 df3: x3 y3 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 0 10 0 11 0 12 0 13 0 14 0 15 0 16 0 17 0 18 0 19 0 20 0 21 0 22 0 23 10.9 24 14.8 25 19.6 26 25.6 27 31.4 28 38.5 29 47.1 30 56.9 31 64.7 32 71 33 77 34 84.7 35 92.5 36 98.8 37 108.2 38 118.8 39 126.9 40 134.3 41 141.1 42 147.2 43 149.9 44 152.8 45 157 46 158.7 47 164.9 48 168.3 49 173.6 50 179 51 181.3 52 182.3 53 186.3 54 190.4 55 194.7 56 198.5 57 202.1 58 205.7 59 209.2 60 211.3 61 213.9 62 214.3 63 215.6 64 215.9 65 217.2 66 218.9 67 220.9 68 222.5 69 224.2 70 225.7 71 225.7 My code: library("ggplot2") library("reshape2") library("gridExtra") p <- ggplot() + geom_line(data=df1, aes(x= x1, y= y1, linetype= "aa"))+ geom_point(data=df2, aes(x= x2, y= y2, shape="bbbbbbb"))+ geom_line(data=df3, aes(x= x3, y= y3, linetype= "cc"))+ scale_shape_manual(name="", labels=c("bbbbbbb"), values = c(21) )+ scale_linetype_manual(name="", labels=c("aa","cc"), values=c("solid", "dashed")) + ylab("y")+ xlab("x")+ theme_bw()+ theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), legend.justification = c(0, 1), legend.position=c(0, 1)) My plot:
Thank you guys for attention. I have find a solution to the problem, I adopted the idea from this post. library("ggplot2") library("reshape2") library("gridExtra") library("gtable") p <- ggplot() + geom_line(data=df1, aes(x= x1, y= y1, linetype= "aa"))+ geom_point(data=df2, aes(x= x2, y= y2, shape="bbbbbbb"))+ geom_line(data=df3, aes(x= x3, y= y3, linetype= "cc"))+ # discard errorbar here. scale_shape_manual(name=NULL, labels=c("bbbbbbb"), values = c(21) )+ scale_linetype_manual(name=NULL, labels=c("aa","cc"), values=c("solid", "dashed")) + ylab("y")+ xlab("x")+ theme_bw()+ theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), legend.position=c(0, 1), legend.justification=c(0,1), legend.margin=unit(0,"cm"), legend.box="vertical", legend.box.just = "left", legend.key.size=unit(1,"lines"), legend.text.align=0, legend.key = element_blank(), legend.title = element_blank(), legend.background=element_blank()) data <- ggplot_build(p) gtable <- ggplot_gtable(data) lbox <- which(sapply(gtable$grobs, paste) == "gtable[guide-box]") guide <- gtable$grobs[[lbox]] gtable$grobs[[lbox]]$heights <- unit.c(guide$heights[1:2], unit(-.8,"cm"), guide$heights[2:3]) # Plotting g<-grid.draw(gtable)
how to make stacked ggplot
want to make stacked ggplot for timeseries >air2 dayofmonth total dept 1 1 1107 380 2 2 918 92 3 3 1089 113 4 4 1086 235 5 5 1063 218 6 6 1084 325 7 7 1129 180 8 8 1133 166 9 9 918 72 10 10 1088 214 11 11 1114 180 12 12 1047 195 13 13 1110 421 14 14 1165 216 15 15 1174 228 16 16 1010 115 I tried this but didnt get the expected graph: mdata <- melt(air2,id=c("dayofmonth")) ggplot(mdata, aes(x=Time,y=value,group=variable,fill=variable)) + geom_area(position="fill")
ggplot: Showing x-axis line for each facet plot
I have created a facet plot using ggplot which has 4 rows and 1 column with this code: ggplot(data=c, aes(x=Time, y=X.mean, fill = Site, width=.1)) + geom_bar(stat="identity", position=position_dodge(), width=0.5,colour="black", show_guide=FALSE) + ylab(NULL) + xlab(NULL) + geom_errorbar(aes(ymax= X.Passes.sd, ymin= 0), size = 0.7, width = 0.3,position = position_dodge(0.9))+ scale_fill_manual(values=c("cadetblue2", "royalblue1", "mediumseagreen", "green4")) + scale_y_continuous(breaks=number_ticks(12), expand = c(0, 0), limits=c(0,20)) + scale_x_discrete(expand = c(0, 0), limits=c("17:00","18:00","19:00","20:00","21:00","22:00","23:00","00:00","01:00","02:00","03:00","04:00", "05:00", "06:00")) + facet_wrap(~ Site, ncol=1,nrow=4)+ theme_bw() + theme(strip.text.x = element_text(size=18))+ theme(axis.title.y=element_text(size = 18)) + theme(panel.grid.minor=element_blank(), panel.grid.major=element_blank())+ theme(axis.text.x=element_text(angle = 45, hjust=1, vjust=1, size = 18))+ theme(axis.text.y=element_text(size = 18))+ theme(panel.border = element_blank())+ theme(axis.line = element_line(color = 'black'))+ theme(axis.title.y=element_text(vjust=0.3, size=20))+ theme(strip.background = element_rect(colour="white", fill="white"))+ theme(legend.position = "none") The issue I have is that only the bottom plot has a solid black line on the x-axis. The 3 plots above do not have this line and only have a dashed line for each data point. Does anyone know how I can put a solid line on the x-axis for all of these plots? Thanks Jon Edit 1: Data Date Site Passes 02/11/2013 RM1 85 03/11/2013 RM1 254 04/11/2013 RM1 636 05/11/2013 RM1 610 06/11/2013 RM1 408 07/11/2013 RM1 293 08/11/2013 RM1 388 09/11/2013 RM1 513 10/11/2013 RM1 190 11/11/2013 RM1 333 12/11/2013 RM1 264 13/11/2013 RM1 261 14/11/2013 RM1 364 15/11/2013 RM1 1 16/11/2013 RM1 238 17/11/2013 RM1 149 18/11/2013 RM1 242 19/11/2013 RM1 225 20/11/2013 RM1 196 21/11/2013 RM1 68 22/11/2013 RM1 292 23/11/2013 RM1 159 24/11/2013 RM1 65 25/11/2013 RM1 166 26/11/2013 RM1 44 27/11/2013 RM1 0 28/11/2013 RM1 56 29/11/2013 RM1 378 30/11/2013 RM1 34 01/12/2013 RM1 43 02/12/2013 RM1 518 03/12/2013 RM1 286 04/12/2013 RM1 175 05/12/2013 RM1 169 06/12/2013 RM1 138 07/12/2013 RM1 445 08/12/2013 RM1 1153 09/12/2013 RM1 616 10/12/2013 RM1 1 02/11/2013 RM2 1 03/11/2013 RM2 30 04/11/2013 RM2 210 05/11/2013 RM2 47 06/11/2013 RM2 8 07/11/2013 RM2 66 08/11/2013 RM2 3 09/11/2013 RM2 7 10/11/2013 RM2 4 11/11/2013 RM2 13 12/11/2013 RM2 16 13/11/2013 RM2 31 14/11/2013 RM2 4 15/11/2013 RM2 0 16/11/2013 RM2 9 17/11/2013 RM2 24 18/11/2013 RM2 5 19/11/2013 RM2 47 20/11/2013 RM2 12 21/11/2013 RM2 3 22/11/2013 RM2 43 23/11/2013 RM2 8 24/11/2013 RM2 15 25/11/2013 RM2 26 26/11/2013 RM2 2 27/11/2013 RM2 0 28/11/2013 RM2 0 29/11/2013 RM2 9 30/11/2013 RM2 2 01/12/2013 RM2 1 02/12/2013 RM2 45 03/12/2013 RM2 26 04/12/2013 RM2 6 05/12/2013 RM2 8 06/12/2013 RM2 0 07/12/2013 RM2 0 08/12/2013 RM2 0 09/12/2013 RM2 0 10/12/2013 RM2 0 03/11/2013 RM3 14 04/11/2013 RM3 100 05/11/2013 RM3 22 06/11/2013 RM3 6 07/11/2013 RM3 35 08/11/2013 RM3 12 09/11/2013 RM3 30 10/11/2013 RM3 33 11/11/2013 RM3 3 12/11/2013 RM3 40 13/11/2013 RM3 88 14/11/2013 RM3 5 15/11/2013 RM3 10 16/11/2013 RM3 10 17/11/2013 RM3 13 18/11/2013 RM3 13 19/11/2013 RM3 20 20/11/2013 RM3 12 21/11/2013 RM3 3 22/11/2013 RM3 31 23/11/2013 RM3 1 24/11/2013 RM3 23 25/11/2013 RM3 11 26/11/2013 RM3 2 27/11/2013 RM3 0 28/11/2013 RM3 1 29/11/2013 RM3 23 30/11/2013 RM3 0 01/12/2013 RM3 0 02/12/2013 RM3 9 03/12/2013 RM3 19 04/12/2013 RM3 6 05/12/2013 RM3 8 06/12/2013 RM3 1 07/12/2013 RM3 1 08/12/2013 RM3 35 09/12/2013 RM3 7 10/12/2013 RM3 0 04/11/2013 RM4 371 05/11/2013 RM4 110 06/11/2013 RM4 36 07/11/2013 RM4 55 08/11/2013 RM4 45 09/11/2013 RM4 44 10/11/2013 RM4 10 11/11/2013 RM4 27 12/11/2013 RM4 86 13/11/2013 RM4 116 14/11/2013 RM4 55 15/11/2013 RM4 0 16/11/2013 RM4 95 17/11/2013 RM4 28 18/11/2013 RM4 50 19/11/2013 RM4 69 20/11/2013 RM4 51
You might have found a solution already, but I've always just included the line geom_hline(yintercept=0) to add the x-axis origin line to my plots. Its a serious hack, but it also provides you some aesthetic control, and I haven't found a better work around.
Hard to tell without being able to produce your plot but does using scales = "free_x" in your facet wrap call solve the problem? For example: facet_wrap(~ Site, ncol=1,nrow=4, scales = "free_x")
Might be late to the party, but my solution for this is replacing ggplot2::facet_wrap by lemon::facet_rep_wrap.