Remove a white block space - r

I'm trying to merge this two figures in a way that the rmsf image sits on top of the schematics.
I used the code below to generate the correct figure, but I can't find a way to remove the massive white area at the bottom of the plot (the huge gap that you see between the figure and the code block).
I could crop it with gimp or other software, but I'd prefer to code it correctly.
args <- (commandArgs(T))
library(gdata)
library(ggplot2)
library(plotflow)
library(dplyr)
library(grid)
rmsf.schem <- read.table("./schematics_bindingSites",header=T,sep="",quote="")
rmsf.data <- read.table(paste(args[1]),header=F,sep="",quote="")
names(rmsf.data) <- c("residue","nm")
bb.ggplot <- ggplot(rmsf.data,aes(x=rmsf.data$residue),environment = environment())
gg.rmsf <- bb.ggplot+ geom_line(aes(y=rmsf.data$nm)) +
labs(y = "nm", x = NULL, title = NULL) +
theme(legend.position="none",plot.margin=unit(c(1,1,0,1), "line"))
binding.sites <- ggplot(rmsf.schem, running=order_by(Domain, ~Start), aes(x = Type, ymin = Start, ymax = Stop, colour = Domain)) +
scale_color_manual(values=c("white", "red","blue")) +
geom_linerange(size = 3) + coord_flip() + theme_bw() +
labs(y = NULL, x = NULL, title = NULL) + theme(axis.line = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), panel.border = element_blank(),
panel.background = element_blank(), plot.background= element_blank(), axis.ticks = element_blank(),
legend.position ="none", axis.text = element_blank(), plot.margin=unit(c(-15,1,0,1), "line"))
gA <- ggplotGrob(gg.rmsf)
gB <- ggplotGrob(binding.sites)
maxWidth = grid::unit.pmax(gA$widths[2:5], gB$widths[2:5])
gA$widths[2:5] <- as.list(maxWidth)
gB$widths[2:5] <- as.list(maxWidth)
hello <- grid.arrange(gA, gB, ncol=1)
ggsave("ciao.png",hello, height=7,width=11,dpi=300)
I found several similar questions on stackoverflow, but none that was able to solve this behaviour.
The problem is in the binding.sites object; being a single value I couldn't act on ylim to reduce the area, so I make the background transparent and use a negative margin plot.margin=unit(c(-15,1,0,1) to get the schematics close to the upper plot. I tried to further change plot.margins and change height and width within ggsave, but the white area is always there.
the file used to generate this figure are:
schematics_bindingSites
Domain Type Start Stop
chain bindingSite 0 505
g6p bindingSite 198 206
coEnz bindingSite 38 44
rmsf file
27 0.5757
28 0.4123
29 0.2642
30 0.1848
31 0.1562
32 0.1362
33 0.1258
34 0.1193
35 0.1183
36 0.1207
37 0.1250
38 0.1374
39 0.1459
40 0.1684
41 0.1621
42 0.1766
43 0.1480
44 0.1266
45 0.1566
46 0.1578
47 0.1288
48 0.1132
49 0.1263
50 0.1295
51 0.1112
52 0.1131
53 0.1295
54 0.1240
55 0.1163
56 0.1348
57 0.1487
58 0.1457
59 0.1452
60 0.1245
61 0.1255
62 0.1469
63 0.1644
64 0.1543
65 0.1435
66 0.1426
67 0.1404
68 0.1432
69 0.1483
70 0.1501
71 0.1634
72 0.1825
73 0.1998
74 0.2202
75 0.2133
76 0.2144
77 0.2081
78 0.2197
79 0.2117
80 0.1904
81 0.1967
82 0.2127
83 0.2034
84 0.1857
85 0.2006
86 0.2105
87 0.1816
88 0.1752
89 0.1978
90 0.2062
91 0.2303
92 0.2545
93 0.2425
94 0.2201
95 0.2312
96 0.2364
97 0.2040
98 0.1935
99 0.2060
100 0.2004
101 0.1793
102 0.1858
103 0.1979
104 0.1786
105 0.1702
106 0.1705
107 0.1761
108 0.1775
109 0.1852
110 0.1794
111 0.1706
112 0.1533
113 0.1684
114 0.1748
115 0.1800
116 0.1940
117 0.1825
118 0.1684
119 0.1856
120 0.1904
121 0.1696
122 0.1731
123 0.1936
124 0.1854
125 0.1728
126 0.1926
127 0.2041
128 0.1918
129 0.1952
130 0.1895
131 0.1770
132 0.1864
133 0.1757
134 0.1507
135 0.1304
136 0.1157
137 0.1013
138 0.1025
139 0.0989
140 0.0960
141 0.1128
142 0.1341
143 0.1489
144 0.1483
145 0.1469
146 0.1319
147 0.1210
148 0.1220
149 0.1284
150 0.1191
151 0.1115
152 0.1244
153 0.1380
154 0.1296
155 0.1334
156 0.1531
157 0.1614
158 0.1462
159 0.1391
160 0.1416
161 0.1638
162 0.1651
163 0.1475
164 0.1312
165 0.1105
166 0.0945
167 0.0853
168 0.0769
169 0.0751
170 0.0944
171 0.1149
172 0.1292
173 0.1190
174 0.1285
175 0.1306
176 0.1204
177 0.1250
178 0.1235
179 0.1118
180 0.1000
181 0.1070
182 0.1089
183 0.1027
184 0.1040
185 0.1120
186 0.1089
187 0.1063
188 0.1180
189 0.1280
190 0.1170
191 0.1069
192 0.1007
193 0.0930
194 0.0998
195 0.0862
196 0.0697
197 0.0624
198 0.0701
199 0.0761
200 0.0785
201 0.0801
202 0.0826
203 0.0928
204 0.0815
205 0.0734
206 0.0755
207 0.0792
208 0.0737
209 0.0726
210 0.0797
211 0.0818
212 0.0836
213 0.0818
214 0.0787
215 0.0763
216 0.0795
217 0.0801
218 0.0821
219 0.0891
220 0.0955
221 0.0981
222 0.1000
223 0.0972
224 0.0876
225 0.0793
226 0.0782
227 0.0823
228 0.0908
229 0.0811
230 0.0780
231 0.0774
232 0.0720
233 0.0667
234 0.0673
235 0.0735
236 0.0803
237 0.0826
238 0.0892
239 0.0987
240 0.1103
241 0.1179
242 0.1221
243 0.1300
244 0.1600
245 0.1798
246 0.1589
247 0.1363
248 0.1369
249 0.1321
250 0.1231
251 0.1197
252 0.1221
253 0.1094
254 0.1021
255 0.0951
256 0.0894
257 0.0960
258 0.1065
259 0.0998
260 0.0900
261 0.0926
262 0.1163
263 0.0932
264 0.0762
265 0.0746
266 0.0732
267 0.0747
268 0.0731
269 0.0729
270 0.0727
271 0.0726
272 0.0766
273 0.0777
274 0.0874
275 0.0986
276 0.1040
277 0.1359
278 0.1519
279 0.2056
280 0.1788
281 0.1478
282 0.1409
283 0.1236
284 0.0995
285 0.0888
286 0.0974
287 0.0847
288 0.0815
289 0.0865
290 0.0924
291 0.0888
292 0.0864
293 0.0887
294 0.0866
295 0.0870
296 0.0940
297 0.0953
298 0.0965
299 0.1181
300 0.1239
301 0.1187
302 0.1006
303 0.0944
304 0.0952
305 0.0991
306 0.1035
307 0.1097
308 0.1153
309 0.1274
310 0.1422
311 0.1570
312 0.1954
313 0.1978
314 0.1853
315 0.1935
316 0.1917
317 0.1690
318 0.1522
319 0.1554
320 0.1486
321 0.1348
322 0.1272
323 0.1396
324 0.1479
325 0.1499
326 0.1654
327 0.1547
328 0.1452
329 0.1531
330 0.1722
331 0.1581
332 0.1331
333 0.1210
334 0.1120
335 0.1041
336 0.0999
337 0.0956
338 0.0885
339 0.0853
340 0.0809
341 0.0793
342 0.0784
343 0.0821
344 0.0872
345 0.0892
346 0.0881
347 0.0914
348 0.0845
349 0.0802
350 0.0903
351 0.0942
352 0.0809
353 0.0777
354 0.0735
355 0.0751
356 0.0770
357 0.0792
358 0.0825
359 0.0906
360 0.0990
361 0.1063
362 0.1073
363 0.1136
364 0.1094
365 0.1035
366 0.0867
367 0.0730
368 0.0682
369 0.0673
370 0.0670
371 0.0697
372 0.0727
373 0.0772
374 0.0876
375 0.1109
376 0.1202
377 0.1612
378 0.1777
379 0.2111
380 0.1768
381 0.1701
382 0.2152
383 0.1783
384 0.1313
385 0.1055
386 0.0953
387 0.0810
388 0.0734
389 0.0732
390 0.0736
391 0.0732
392 0.0719
393 0.0774
394 0.0795
395 0.0882
396 0.1033
397 0.1215
398 0.1025
399 0.0810
400 0.0788
401 0.0809
402 0.0845
403 0.0815
404 0.0811
405 0.0818
406 0.0830
407 0.0842
408 0.0964
409 0.1156
410 0.1213
411 0.1307
412 0.1222
413 0.1154
414 0.1069
415 0.0926
416 0.0875
417 0.0897
418 0.0833
419 0.0882
420 0.0889
421 0.0861
422 0.0916
423 0.0952
424 0.1088
425 0.1827
426 0.1998
427 0.1620
428 0.1472
429 0.2168
430 0.2671
431 0.3304
432 0.2871
433 0.2700
434 0.1976
435 0.1573
436 0.1736
437 0.0977
438 0.0827
439 0.0736
440 0.0662
441 0.0703
442 0.0767
443 0.0736
444 0.0754
445 0.0888
446 0.0934
447 0.0888
448 0.0820
449 0.0809
450 0.0947
451 0.0980
452 0.0726
453 0.0703
454 0.0735
455 0.0849
456 0.0945
457 0.0880
458 0.0867
459 0.0918
460 0.0829
461 0.0818
462 0.0856
463 0.0856
464 0.0822
465 0.0866
466 0.1037
467 0.1117
468 0.1061
469 0.1064
470 0.1209
471 0.1349
472 0.1383
473 0.1527
474 0.1589
475 0.1506
476 0.1422
477 0.1388
478 0.1338
479 0.1192
480 0.1221
481 0.1228
482 0.1178
483 0.1238
484 0.1236
485 0.1211
486 0.1188
487 0.1139
488 0.1152
489 0.1170
490 0.1243
491 0.1169
492 0.1038
493 0.1049
494 0.1089
495 0.0985
496 0.0959
497 0.1079
498 0.1084
499 0.1046
500 0.1180
501 0.1084
502 0.1051
503 0.1248
504 0.1750
505 0.2072
SOLUTION:
As drmariod suggested, I replaced grid.arrange with arrangeGrob. In this way I was able to set heights = c(0.9,0.1). I then change the margins in the schematics figure plot.margin=unit(c(-1.5,1,0,1), "line") and everything worked as expected.

Instead of going grobs route, how about this:
library(ggplot2)
#dummy data
set.seed(123)
rmsf <- data.frame(res=1:100,nm=runif(100))
bindingSite <- data.frame(
Domain=c("chain","g6p","coEnz"),
Type=rep("bindingSite",3),
Start=c(10,20,40),
Stop=c(15,22,48)
)
#plot
ggplot() +
geom_rect(aes(xmin=Start,xmax=Stop,ymin=-Inf,ymax=0,fill=Domain),
bindingSite,alpha=0.4) +
geom_line(aes(res,nm),rmsf) +
theme_classic()

As drmariod suggested, I replaced grid.arrange with arrangeGrob. In this way I was able to set heights = c(0.9,0.1). I then change the margins in the schematics figure plot.margin=unit(c(-1.5,1,0,1), "line") and everything worked as expected.
Also zx8754 solution works, but in the optics or adding more schematics to the same graph, I think that using arrangeGrob is the best option.

Related

Trying to write a short script that can calculate all the prime numbers less than or equal to 100 in R

I am trying to write a short script that can print out all prime numbers up to 100 and if the number is less than 2 then return "false". I am a bit stuck with the logic that I have.. still new to programming so the codes may not make sense to some of you (don't be tooo harsh) and not sure how to turn ideas into codes..but this is what I have for now!
prime_numbers <- function(x){
pnums <- vector()
for (i in 2:x) {
if (i >= 2) {
pnums = c(pnums,i)
return (pnums)
if (i %% 2==0) {
return(FALSE)
}
}
}
}
prime_numbers(100)
Your logic isn't quite right here. Perhaps something like this?
prime_numbers <- function(x)
{
if(x < 2) return(FALSE)
result <- vector()
for(i in 2:x) if(!any(i %% result == 0)) result <- c(result, i)
return(result)
}
This seems to give the desired behaviour:
prime_numbers(100)
#> [1] 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
prime_numbers(2)
#> [1] 2
prime_numbers(1000)
#> [1] 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61
#> [19] 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151
#> [37] 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251
#> [55] 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359
#> [73] 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463
#> [91] 467 479 487 491 499 503 509 521 523 541 547 557 563 569 571 577 587 593
#> [109] 599 601 607 613 617 619 631 641 643 647 653 659 661 673 677 683 691 701
#> [127] 709 719 727 733 739 743 751 757 761 769 773 787 797 809 811 821 823 827
#> [145] 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 941 947 953
#> [163] 967 971 977 983 991 997
This isn't a very efficient algorithm, since computation time grows exponentially, taking about 150 microseconds to find all the primes under 100, 3 milliseconds for all primes under 1,000, and 200 milliseconds for all primes under 10,000. Still, the logic is hopefully easy to follow.
Created on 2021-10-19 by the reprex package (v2.0.0)

Using R to create a 'change in x' column for multiple x columns

Im currently trying to calculate change in amount of SWE for multiple sites (50) so as to attain snow melt rates (change per day). I think I have the right formula but trying to loop it doesn't seem to be working, my code and error below.
site <- colnames(winter_pr_swe[,55:104])
for (i in site){
sitex <- paste0(i)
ratex <- paste0('rate',i)
winter_pr_swe1 <- winter_pr_swe %>% mutate(ratex = (sitex - lag(sitex))
}
Error: unexpected '}' in: "winter_pr_swe1 <- winter_pr_swe %>% %>% mutate(ratex = (sitex - lag(sitex))
}"
Here is the head of the data set for reference, each column being the SWE levels for a single site, each row being a days measurement:
head(winter_pr_swe[,55:104])
X303 X308 X310 X316 X327 X337 X340 X378 X386 X409 X416 X426
1 157 71 234 287 361 163 86 292 241 348 109 183
2 157 150 272 333 384 165 102 300 302 409 130 185
3 157 150 274 340 396 168 107 310 312 419 130 190
4 157 150 274 343 406 168 157 315 312 419 122 190
5 157 147 274 351 409 168 193 318 318 434 107 190
6 165 140 274 351 427 168 193 335 318 434 107 234
X431 X465 X486 X488 X491 X511 X519 X527 X532 X538 X580 X586
1 592 178 69 53 292 427 64 137 450 175 312 246
2 658 208 97 122 340 490 155 137 508 190 340 282
3 668 218 97 124 351 490 157 137 513 203 343 300
4 676 218 94 124 351 490 157 152 521 203 343 302
5 696 224 86 119 351 490 152 160 531 213 353 323
6 726 226 79 109 351 485 145 168 549 218 368 333
X589 X595 X617 X622 X624 X629 X632 X640 X665 X705 X715 X737
1 310 41 351 300 315 206 358 66 46 358 157 478
2 338 43 406 320 330 231 391 155 48 483 170 511
3 353 33 406 328 338 241 396 155 48 485 180 521
4 356 30 406 330 338 241 399 155 48 485 183 523
5 363 25 401 333 353 267 427 155 56 485 183 556
6 368 15 401 345 376 267 460 145 56 485 198 599
X739 X755 X757 X762 X780 X827 X839 X840 X843 X857 X861 X866
1 168 201 274 254 462 356 74 526 183 180 58 112
2 196 244 310 257 533 363 74 584 224 196 145 160
3 201 249 315 267 549 376 74 589 226 196 145 175
4 206 251 318 267 551 376 74 592 229 196 145 160
5 226 249 320 282 587 376 91 620 239 201 142 155
6 226 249 320 282 599 417 91 648 241 201 119 145
X874 X877
1 615 23
2 671 152
3 678 155
4 681 155
5 709 140
6 752 140

Length of list object returns 1 in R when not 1

I have a list x
X1
1 0.8
2 1.0
3 661.7
4 661.8
5 661.9
6 662.3
7 662.6
8 662.7
9 663.3
10 663.6
11 663.7
12 663.9
13 664.0
14 664.1
15 664.3
16 664.4
17 664.5
18 664.7
19 665.1
20 666.9
21 667.5
22 668.2
23 668.3
24 669.7
25 670.3
26 670.8
27 671.1
28 672.0
29 672.1
30 674.8
31 675.3
32 677.5
33 677.9
34 678.5
35 678.9
36 679.0
37 686.6
38 687.6
39 714.1
40 899.1
41 900.4
42 901.1
43 901.3
44 902.7
45 908.3
46 908.7
47 908.9
48 909.0
49 909.2
50 910.0
51 910.1
52 910.3
53 910.6
54 910.7
55 911.3
56 911.4
57 911.6
58 911.8
59 912.6
60 912.7
61 912.8
62 913.0
63 913.1
64 913.2
65 913.3
66 913.7
67 913.9
68 914.0
69 914.2
70 914.3
71 914.4
72 914.6
73 915.2
74 915.3
75 915.5
76 915.6
77 915.7
78 915.9
79 916.0
80 916.1
81 916.3
82 916.5
83 916.6
84 916.7
85 916.9
86 917.3
87 917.5
88 917.6
89 917.8
90 917.9
91 918.0
92 918.2
93 918.3
94 918.5
95 918.6
96 918.8
97 918.9
98 919.0
99 919.2
100 919.3
101 919.5
102 919.6
103 919.7
104 919.9
105 920.0
106 920.2
107 920.3
108 920.5
109 920.6
110 920.8
111 920.9
112 921.0
113 921.1
114 921.2
115 921.3
116 921.3
117 921.5
118 921.6
119 921.7
120 921.8
121 922.0
122 922.1
123 922.4
124 922.5
125 922.6
126 922.7
127 922.9
128 923.0
129 923.2
130 923.3
131 923.5
132 923.6
133 923.8
134 923.9
135 927.2
136 927.3
137 927.4
138 927.6
139 927.7
140 927.8
141 928.0
142 928.1
143 928.3
144 928.4
145 928.5
146 928.7
147 928.8
148 928.9
149 929.1
150 929.2
151 929.3
152 929.5
153 929.6
154 929.8
155 929.9
156 930.1
157 930.2
158 930.3
159 930.3
160 930.5
161 930.6
162 930.7
163 930.9
164 931.0
165 931.1
166 931.2
167 931.3
168 931.4
169 931.5
170 931.7
171 931.8
172 932.0
173 932.0
174 932.1
175 932.2
176 932.4
177 932.5
178 932.6
179 932.7
180 933.3
181 933.4
182 933.6
183 933.7
184 933.8
185 934.5
186 934.7
187 934.8
188 934.9
189 935.0
190 935.2
191 935.3
192 935.3
193 935.5
194 935.6
195 935.7
196 935.8
197 936.0
198 936.1
199 936.3
200 936.4
201 936.5
202 936.7
203 936.8
204 936.9
205 937.1
206 937.2
207 937.4
208 937.5
209 937.7
210 937.8
211 937.9
212 938.1
213 938.2
214 938.4
215 938.5
216 938.7
217 938.9
218 939.0
219 939.2
220 939.4
221 939.7
222 939.9
223 940.3
224 940.7
225 940.9
226 941.4
227 941.7
228 942.1
229 942.6
230 942.7
231 943.3
232 943.5
233 943.9
234 944.9
235 945.0
236 945.1
237 945.4
238 945.6
239 945.8
240 945.9
241 946.2
242 947.6
243 947.9
244 948.2
245 948.3
246 948.5
247 948.6
248 948.8
249 948.9
250 949.5
251 949.6
252 951.8
253 951.9
254 952.0
255 952.1
256 952.5
257 952.6
258 953.0
259 953.3
260 953.4
261 953.5
262 953.7
263 953.8
264 953.9
265 954.1
266 954.2
267 954.4
268 954.5
269 954.7
270 954.8
271 955.0
272 955.1
273 955.2
274 955.4
275 955.5
276 955.6
277 955.7
278 955.9
279 956.0
280 956.1
281 956.3
282 956.4
283 956.5
284 956.6
285 956.9
286 957.2
287 957.3
288 957.4
289 957.5
290 957.9
291 958.9
292 959.0
293 959.3
294 959.5
295 959.9
296 960.0
297 960.2
298 960.5
299 960.6
300 960.8
301 960.8
302 961.4
303 961.5
304 961.6
305 961.7
306 961.8
307 961.9
308 968.8
309 969.1
310 970.0
311 970.5
312 970.7
313 974.2
314 998.7
315 998.8
316 998.9
317 999.1
318 999.2
319 1000.3
320 1001.2
321 1001.4
322 1001.5
323 1001.6
324 1001.7
325 1003.2
326 1003.4
327 1003.6
328 1004.2
329 1004.3
330 1004.4
331 1004.5
332 1004.6
333 1005.3
334 1005.4
335 1005.5
336 1005.6
337 1005.7
338 1005.9
339 1006.0
340 1006.1
341 1006.8
342 1006.9
343 1007.1
344 1007.2
345 1007.3
346 1007.4
347 1007.6
348 1007.7
349 1007.8
350 1008.0
351 1008.1
352 1008.7
353 1008.8
354 1008.9
355 1009.0
356 1009.2
357 1009.3
358 1009.3
359 1009.5
360 1009.6
361 1009.7
362 1009.8
363 1010.0
364 1010.2
365 1010.4
366 1010.5
367 1010.6
368 1010.7
369 1010.9
370 1011.0
371 1011.1
372 1011.2
373 1011.4
374 1011.5
375 1011.6
376 1011.7
377 1011.9
378 1012.0
379 1012.1
380 1012.2
381 1012.3
382 1012.4
383 1012.6
384 1012.7
385 1012.8
386 1013.0
387 1013.2
388 1013.4
389 1013.5
390 1013.6
391 1013.6
392 1013.8
393 1013.9
394 1014.0
395 1014.0
396 1014.3
397 1014.7
398 1014.8
399 1014.9
400 1015.7
401 1015.8
402 1016.0
403 1016.1
404 1016.2
405 1016.5
406 1016.6
407 1016.9
408 1017.0
409 1017.1
410 1017.3
411 1017.4
412 1017.5
413 1017.7
414 1017.8
415 1017.8
416 1018.3
417 1018.5
418 1026.6
419 1027.0
420 1027.3
421 1027.4
422 1027.7
423 1028.6
424 1029.1
425 1029.9
426 1030.0
427 1030.2
428 1270.0
429 1270.1
430 1270.2
431 1270.3
432 1270.4
433 1270.5
434 1270.7
435 1270.7
436 1270.9
437 1271.0
438 1271.3
439 1271.4
440 1272.3
441 1272.5
442 1273.1
443 1273.2
444 1273.3
445 1273.4
446 1273.5
447 1273.8
448 1274.0
449 1274.1
450 1274.3
451 1274.4
452 1274.6
453 1274.7
454 1274.8
455 1274.9
456 1275.1
457 1275.3
458 1275.5
459 1275.6
460 1275.8
461 1275.9
462 1276.1
463 1276.2
464 1276.3
465 1276.4
466 1276.6
467 1276.7
468 1276.8
469 1277.2
470 1277.3
471 1277.5
472 1277.6
473 1277.7
474 1277.9
475 1278.0
476 1278.1
477 1278.2
478 1278.3
479 1278.4
480 1278.5
481 1278.7
482 1279.0
483 1279.0
484 1279.1
485 1279.3
486 1279.3
487 1279.5
488 1279.6
489 1279.7
490 1279.8
491 1280.3
492 1280.4
493 1280.7
494 1280.8
495 1280.9
496 1281.1
497 1281.3
498 1281.4
499 1281.5
500 1282.3
501 1283.0
502 1283.1
503 1284.0
504 1284.8
505 1284.9
506 1285.0
507 1285.1
508 1285.4
which has a length of 508, although when I use length(x) it returns 1. I have tried to the function
length(as.vector(x))
although this also does not work and returns 1. Is there another form that I should convert this list to so that I can accurately find the length? For reference, I am using the length to duplicate other elements using the rep_len() function.
as.vector on a data.frame returns the data.frame itself as there is no method for as.vector with data.frame
methods('as.vector')
#[1] as.vector,abIndex-method as.vector,ANY-method as.vector,dgCMatrix-method as.vector,dgeMatrix-method
#[5] as.vector,diagonalMatrix-method as.vector,dsCMatrix-method as.vector,ldenseMatrix-method as.vector,Matrix-method
#[9] as.vector,ndenseMatrix-method as.vector,sparseVector-method as.vector.factor as.vector.Matrix*
#[13] as.vector.sparseVector*
We can also check the reverse i.e. on data.frame
grep('^as\\.', methods(class = 'data.frame'), value = TRUE)
#[1] "as.data.frame.data.frame" "as.data.table.data.frame"
#[3] "as.list.data.frame" "as.matrix.data.frame" "as.tbl.data.frame"
and the length is the same as the number of columns of data.frame i.e. here it is 1. Instead, we need nrow(x)
as.vector(mtcars) # nothing changed
length(as.vector(mtcars))
#[1] 11
But, suppose, if we do
nrow(mtcars)
#[1] 32
length can also be applied on the vector by extracting the column with $ or [[
length(mtcars[[1]])

Issues with stmincuts in iGraph for R

I am trying to have R calculate the minimum cuts of a network. I am having trouble using iGraph's stmicuts command. It produces cuts for certain nodes and the ones it does produce cuts for do not make much sense. For example:
st_min_cuts(net_full, '2', '417', capacity = NULL)
Produces
st_min_cuts(net_full, '2', '346', capacity = NULL)
$value
[1] 2
$cuts
$cuts[[1]]
+ 0/960 edges from 19b17c7 (vertex names):
$partition1s
$partition1s[[1]]
+ 512/525 vertices, named, from 19b17c7:
[1] 346 515 345 145 144 143 142 141 140 139 138 137 136 135 134 133 132 131 130 129 128 127 126 125 124
[26] 123 122 121 120 119 118 117 116 115 114 113 112 111 110 109 108 107 106 105 104 103 102 101 100 99
[51] 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74
[76] 73 72 32 31 28 15 8 4 2 524 520 518 517 516 519 513 512 511 510 509 506 504 503 499 497
[101] 491 487 485 480 478 476 474 469 382 380 379 377 370 368 367 366 364 362 361 358 356 354 349 347 338
[126] 336 332 329 319 318 317 316 315 314 313 312 311 310 309 308 307 306 305 304 303 302 301 300 299 298
[151] 297 296 295 30 27 294 293 292 291 290 289 288 287 286 285 284 283 282 281 280 279 278 277 276 275
[176] 274 273 272 24 23 265 264 263 262 261 260 259 258 257 256 255 254 253 252 251 250 249 248 247 246
[201] 245 244 243 242 241 240 239 238 237 21 20 19 18 269 268 267 266 26 17 16 236 235 234 233 232
[226] 231 230 229 228 227 226 225 224 223 222 221 220 219 218 217 216 215 214 213 212 211 210 209 208 207
+ ... omitted several vertices
I am trying to determine the minimum number of nodes that need to be cut for the source and sink to be disconnected. Any recommendations about how I can fix this issue or find the minimum cuts another way?

Adding geom_vline to ggplot2 from separate data frame

I am having trouble adding geom_vline to my ggplot 2 chart. I am plotting on the x axis the date. as.Date format. Then on the Y axis plotting numerical values.
On the same plot. I have matching dates pulled from another data frame. They are in the same Date format as my plot above.
The problem when adding the geom_vline is that it throws a error:
Error: ggplot2 doesn't know how to deal with data of class numeric
If my two dates I want to use are both Date format, im not sure why its throwing a numerical error date. The only numerial format I have is the data on the Y axis.
See code below:
p1 <- ggplot(data=plotdataframe, aes(x=ddate, y=SP500close1, colour=SP500close1)) +
geom_vline(data=matches.df,aes(xintercept=as.numeric(ddatesmatches)))+
geom_line() +
theme_bw() +
labs(title="S&P500 Close",y="S&P500 Close",x="Date")+
theme(plot.title = element_text(hjust = 0.5)) +
theme(plot.subtitle = element_text(hjust = 0.5))
grid.arrange(p1)
Here is the formats of all the data that make this plot:
> str(matches.df)
'data.frame': 48 obs. of 1 variable:
$ ddatesmatches: Date, format: "1929-10-28" "1929-11-11" "1929-11-20" "1931-06-08" ...
> str(ddate)
Date[1:23539], format: "1928-01-03" "1928-01-04" "1928-01-05" "1928-01-06" "1928-01-07" "1928-01-09" "1928-01-10" "1928-01-11" ...
> str(SP500close1)
num [1:23539] 17.8 17.7 17.6 17.7 17.7 ...
As you can see.... both dates are in the Date format and my Y axis (SP500close1) is a numerical value.
Seems a simple thing to add to this chart. Since my formats are where they need to be, can anyone shed some insight into this?
Note - They are however from two different data sources, so i specified in geom_vline(data=matches.df.... in order to read the data to plot the vlines from that specific data frame.
Any insight appreciated.
Thanks
Here is the data for plotdataframe
ddate SP500close1
1 1928-01-03 17.76
2 1928-01-04 17.72
3 1928-01-05 17.55
4 1928-01-06 17.66
5 1928-01-07 17.68
6 1928-01-09 17.50
7 1928-01-10 17.37
8 1928-01-11 17.35
9 1928-01-12 17.47
10 1928-01-13 17.58
11 1928-01-14 17.44
12 1928-01-16 17.29
13 1928-01-17 17.30
14 1928-01-18 17.26
15 1928-01-19 17.38
16 1928-01-20 17.48
17 1928-01-21 17.53
18 1928-01-23 17.64
19 1928-01-24 17.71
20 1928-01-25 17.52
21 1928-01-26 17.63
22 1928-01-27 17.69
23 1928-01-28 17.58
24 1928-01-30 17.49
25 1928-01-31 17.57
26 1928-02-01 17.53
27 1928-02-02 17.63
28 1928-02-03 17.40
29 1928-02-04 17.43
30 1928-02-06 17.45
31 1928-02-07 17.44
32 1928-02-08 17.49
33 1928-02-09 17.55
34 1928-02-10 17.54
35 1928-02-11 17.53
36 1928-02-14 17.44
37 1928-02-15 17.40
38 1928-02-16 17.35
39 1928-02-17 17.04
40 1928-02-18 16.99
41 1928-02-20 16.95
42 1928-02-21 17.11
43 1928-02-23 17.13
44 1928-02-24 17.18
45 1928-02-25 17.19
46 1928-02-27 17.11
47 1928-02-28 17.16
48 1928-02-29 17.26
49 1928-03-01 17.30
50 1928-03-02 17.30
51 1928-03-03 17.42
52 1928-03-05 17.57
53 1928-03-06 17.67
54 1928-03-07 17.58
55 1928-03-08 17.64
56 1928-03-09 17.93
57 1928-03-10 17.87
58 1928-03-12 18.00
59 1928-03-13 17.92
60 1928-03-14 17.93
61 1928-03-15 18.07
62 1928-03-16 18.26
63 1928-03-17 18.32
64 1928-03-19 18.36
65 1928-03-20 18.46
66 1928-03-21 18.65
67 1928-03-22 18.60
68 1928-03-23 18.78
69 1928-03-24 18.79
70 1928-03-26 19.05
71 1928-03-27 18.94
72 1928-03-28 18.89
73 1928-03-29 18.95
74 1928-03-30 19.28
75 1928-03-31 19.13
76 1928-04-02 18.91
77 1928-04-03 19.02
78 1928-04-04 18.99
79 1928-04-05 19.23
80 1928-04-09 19.27
81 1928-04-10 19.10
82 1928-04-11 19.44
83 1928-04-12 19.41
84 1928-04-13 19.73
85 1928-04-14 19.59
86 1928-04-16 19.68
87 1928-04-17 19.52
88 1928-04-18 19.52
89 1928-04-19 19.63
90 1928-04-20 19.34
91 1928-04-23 19.15
92 1928-04-24 19.25
93 1928-04-25 19.33
94 1928-04-26 19.47
95 1928-04-27 19.68
96 1928-04-28 19.75
97 1928-04-30 19.75
98 1928-05-01 19.78
99 1928-05-02 19.83
100 1928-05-03 19.93
101 1928-05-04 20.24
102 1928-05-07 20.41
103 1928-05-08 20.27
104 1928-05-09 20.24
105 1928-05-10 20.21
106 1928-05-11 20.36
107 1928-05-14 20.44
108 1928-05-15 20.39
109 1928-05-16 20.05
110 1928-05-17 20.12
111 1928-05-18 19.92
112 1928-05-21 19.63
113 1928-05-22 19.36
114 1928-05-23 19.75
115 1928-05-24 19.87
116 1928-05-25 19.90
117 1928-05-28 19.57
118 1928-05-29 19.82
119 1928-05-31 20.00
120 1928-06-01 20.07
121 1928-06-02 20.22
122 1928-06-04 19.80
123 1928-06-05 19.85
124 1928-06-06 19.66
125 1928-06-07 19.61
126 1928-06-08 19.37
127 1928-06-09 19.12
128 1928-06-11 18.68
129 1928-06-12 18.34
130 1928-06-13 18.89
131 1928-06-14 19.05
132 1928-06-15 18.74
133 1928-06-16 18.75
134 1928-06-18 18.40
135 1928-06-19 18.34
136 1928-06-20 18.57
137 1928-06-21 18.56
138 1928-06-22 18.54
139 1928-06-23 18.46
140 1928-06-25 18.47
141 1928-06-26 18.68
142 1928-06-27 18.86
143 1928-06-28 19.07
144 1928-06-29 19.14
145 1928-06-30 19.19
146 1928-07-02 18.98
147 1928-07-03 19.29
148 1928-07-05 19.48
149 1928-07-06 19.39
150 1928-07-07 19.44
151 1928-07-09 19.46
152 1928-07-10 19.43
153 1928-07-11 18.95
154 1928-07-12 18.81
155 1928-07-13 18.96
156 1928-07-14 18.93
157 1928-07-16 18.70
158 1928-07-17 18.80
159 1928-07-18 19.03
160 1928-07-19 18.99
161 1928-07-20 19.03
162 1928-07-21 18.96
163 1928-07-23 19.12
164 1928-07-24 19.09
165 1928-07-25 19.20
166 1928-07-26 19.24
167 1928-07-27 19.42
168 1928-07-28 19.47
169 1928-07-30 19.47
170 1928-07-31 19.43
171 1928-08-01 19.42
172 1928-08-02 19.33
173 1928-08-03 19.36
174 1928-08-04 19.41
175 1928-08-06 19.58
176 1928-08-07 19.49
177 1928-08-08 19.25
178 1928-08-09 19.30
179 1928-08-10 19.25
180 1928-08-11 19.30
181 1928-08-13 19.29
182 1928-08-14 19.13
183 1928-08-15 19.47
184 1928-08-16 19.61
185 1928-08-17 19.64
186 1928-08-18 19.71
187 1928-08-20 19.77
188 1928-08-21 20.02
189 1928-08-22 20.00
190 1928-08-23 20.02
191 1928-08-24 20.34
192 1928-08-25 20.45
193 1928-08-27 20.38
194 1928-08-28 20.54
195 1928-08-29 20.58
196 1928-08-30 20.61
197 1928-08-31 20.87
198 1928-09-01 20.87
199 1928-09-04 20.97
200 1928-09-05 21.07
201 1928-09-06 20.94
202 1928-09-07 21.09
203 1928-09-08 21.03
204 1928-09-10 20.92
205 1928-09-11 21.08
206 1928-09-12 21.16
207 1928-09-13 21.14
208 1928-09-14 21.14
209 1928-09-15 21.23
210 1928-09-17 21.33
211 1928-09-18 21.18
212 1928-09-19 21.18
213 1928-09-20 21.22
214 1928-09-21 21.36
215 1928-09-22 21.24
216 1928-09-24 21.43
217 1928-09-25 21.41
218 1928-09-26 21.40
219 1928-09-27 21.13
220 1928-09-28 21.14
221 1928-09-29 21.37
222 1928-10-01 21.36
223 1928-10-02 21.26
224 1928-10-03 21.19
225 1928-10-04 21.26
226 1928-10-05 21.22
227 1928-10-06 21.17
228 1928-10-08 21.11
229 1928-10-09 21.18
230 1928-10-10 21.41
231 1928-10-11 21.51
232 1928-10-13 21.63
233 1928-10-15 21.65
234 1928-10-16 21.72
235 1928-10-17 21.79
236 1928-10-18 21.84
237 1928-10-19 21.96
238 1928-10-20 21.84
239 1928-10-22 21.81
240 1928-10-23 21.95
241 1928-10-24 21.99
242 1928-10-25 21.91
243 1928-10-26 21.65
244 1928-10-27 21.87
245 1928-10-29 22.00
246 1928-10-30 21.76
247 1928-10-31 21.68
248 1928-11-01 21.99
249 1928-11-02 21.95
250 1928-11-03 21.97
251 1928-11-05 22.24
252 1928-11-07 22.50
253 1928-11-08 22.39
254 1928-11-09 22.55
255 1928-11-10 22.73
256 1928-11-12 22.84
257 1928-11-13 22.91
258 1928-11-14 22.91
259 1928-11-15 23.03
260 1928-11-16 23.37
261 1928-11-17 23.37
262 1928-11-19 23.37
263 1928-11-20 23.57
264 1928-11-21 23.22
265 1928-11-22 23.52
266 1928-11-23 23.70
267 1928-11-26 23.80
268 1928-11-27 23.91
269 1928-11-28 24.19
270 1928-11-30 24.28
271 1928-12-01 24.03
272 1928-12-03 23.85
273 1928-12-04 23.91
274 1928-12-05 23.81
275 1928-12-06 22.91
276 1928-12-07 22.36
277 1928-12-08 21.92
278 1928-12-10 22.31
279 1928-12-11 22.74
280 1928-12-12 22.52
281 1928-12-13 22.48
282 1928-12-14 22.53
283 1928-12-15 22.52
284 1928-12-17 22.44
285 1928-12-18 22.70
286 1928-12-19 22.98
287 1928-12-20 23.07
288 1928-12-21 23.33
289 1928-12-22 23.33
290 1928-12-24 23.54
291 1928-12-26 23.44
292 1928-12-27 23.64
293 1928-12-28 23.98
294 1928-12-29 24.05
295 1928-12-31 24.35
296 1929-01-02 24.81
297 1929-01-03 24.86
298 1929-01-04 24.85
299 1929-01-05 24.57
300 1929-01-07 24.25
301 1929-01-08 24.17
302 1929-01-09 24.54
303 1929-01-10 24.58
304 1929-01-11 24.54
305 1929-01-12 24.47
306 1929-01-14 24.59
307 1929-01-15 24.23
308 1929-01-16 24.56
309 1929-01-17 24.69
310 1929-01-18 24.77
311 1929-01-19 24.88
312 1929-01-21 24.86
313 1929-01-22 25.07
314 1929-01-23 25.22
315 1929-01-24 25.06
316 1929-01-25 25.48
317 1929-01-26 25.43
318 1929-01-28 25.35
319 1929-01-29 25.36
320 1929-01-30 25.45
321 1929-01-31 25.74
322 1929-02-01 25.84
323 1929-02-02 25.80
324 1929-02-04 25.64
325 1929-02-05 25.70
326 1929-02-06 25.49
327 1929-02-07 24.71
328 1929-02-08 24.44
329 1929-02-11 24.98
330 1929-02-13 24.95
331 1929-02-14 24.85
332 1929-02-15 24.35
333 1929-02-16 24.05
334 1929-02-18 24.39
335 1929-02-19 24.38
336 1929-02-20 24.59
337 1929-02-21 24.86
338 1929-02-25 24.91
339 1929-02-26 24.95
340 1929-02-27 25.25
341 1929-02-28 25.59
342 1929-03-01 25.83
343 1929-03-02 25.77
344 1929-03-04 25.49
345 1929-03-05 25.32
346 1929-03-06 24.92
347 1929-03-07 25.15
348 1929-03-08 25.31
349 1929-03-09 25.31
350 1929-03-11 24.95
351 1929-03-12 24.97
352 1929-03-13 25.24
353 1929-03-14 25.73
354 1929-03-15 26.08
355 1929-03-16 26.13
356 1929-03-18 26.04
357 1929-03-19 26.03
358 1929-03-20 26.02
359 1929-03-21 25.90
360 1929-03-22 25.55
361 1929-03-23 25.21
362 1929-03-25 24.51
363 1929-03-26 24.35
364 1929-03-27 25.09
365 1929-03-28 25.53
366 1929-04-01 24.88
367 1929-04-02 25.17
368 1929-04-03 24.86
369 1929-04-04 25.30
370 1929-04-05 25.05
371 1929-04-06 25.07
372 1929-04-08 24.83
373 1929-04-09 24.63
374 1929-04-10 24.78
375 1929-04-11 25.06
376 1929-04-12 25.15
377 1929-04-13 25.17
378 1929-04-15 25.00
379 1929-04-16 25.06
380 1929-04-17 25.32
381 1929-04-18 25.43
382 1929-04-19 25.41
383 1929-04-20 25.45
384 1929-04-22 25.70
385 1929-04-23 25.85
386 1929-04-24 25.75
387 1929-04-25 25.63
388 1929-04-26 25.58
389 1929-04-27 25.65
390 1929-04-29 25.53
391 1929-04-30 25.94
392 1929-05-01 25.99
393 1929-05-02 26.11
394 1929-05-03 26.37
395 1929-05-04 26.48
396 1929-05-06 26.32
397 1929-05-07 26.07
398 1929-05-08 26.18
399 1929-05-09 26.02
400 1929-05-10 26.34
401 1929-05-11 26.24
402 1929-05-13 25.64
403 1929-05-14 25.95
404 1929-05-15 25.84
405 1929-05-16 25.97
406 1929-05-17 26.09
407 1929-05-18 26.05
408 1929-05-20 25.39
409 1929-05-21 25.70
410 1929-05-22 24.76
411 1929-05-23 25.31
412 1929-05-24 25.12
413 1929-05-25 25.03
414 1929-05-27 24.20
415 1929-05-28 24.59
416 1929-05-29 24.62
417 1929-05-31 24.83
418 1929-06-01 24.92
419 1929-06-03 25.27
420 1929-06-04 25.64
421 1929-06-05 25.59
422 1929-06-06 25.64
423 1929-06-07 25.53
424 1929-06-08 25.39
425 1929-06-10 25.27
426 1929-06-11 25.43
427 1929-06-12 25.45
428 1929-06-13 25.84
429 1929-06-14 25.93
430 1929-06-15 26.05
431 1929-06-17 26.41
432 1929-06-18 26.42
433 1929-06-19 26.32
434 1929-06-20 26.47
435 1929-06-21 26.62
436 1929-06-22 26.72
437 1929-06-24 26.62
438 1929-06-25 26.90
439 1929-06-26 27.06
440 1929-06-27 27.11
441 1929-06-28 27.41
442 1929-06-29 27.62
443 1929-07-01 27.77
444 1929-07-02 28.06
445 1929-07-03 28.18
446 1929-07-05 28.32
447 1929-07-06 28.36
448 1929-07-08 28.37
449 1929-07-09 28.28
450 1929-07-10 28.20
451 1929-07-11 28.26
452 1929-07-12 28.57
453 1929-07-13 28.64
454 1929-07-15 28.45
455 1929-07-16 28.65
456 1929-07-17 28.72
457 1929-07-18 28.66
458 1929-07-19 28.76
459 1929-07-20 28.81
460 1929-07-22 28.43
461 1929-07-23 28.83
462 1929-07-24 28.64
463 1929-07-25 28.77
464 1929-07-26 28.66
465 1929-07-27 28.55
466 1929-07-29 28.18
467 1929-07-30 28.50
468 1929-07-31 28.88
469 1929-08-01 29.08
470 1929-08-02 29.56
471 1929-08-03 29.81
472 1929-08-05 29.54
473 1929-08-06 29.43
474 1929-08-07 29.13
475 1929-08-08 29.47
476 1929-08-09 28.38
477 1929-08-10 28.83
478 1929-08-12 29.27
479 1929-08-13 29.53
480 1929-08-14 29.62
481 1929-08-15 29.67
482 1929-08-16 30.22
483 1929-08-17 30.24
484 1929-08-19 30.50
485 1929-08-20 30.68
486 1929-08-21 30.55
487 1929-08-22 30.79
488 1929-08-23 31.09
489 1929-08-24 31.15
490 1929-08-26 31.07
491 1929-08-27 31.05
492 1929-08-28 30.99
493 1929-08-29 31.29
494 1929-08-30 31.71
495 1929-09-03 31.82
496 1929-09-04 31.72
497 1929-09-05 31.02
498 1929-09-06 31.83
499 1929-09-07 31.92
500 1929-09-09 31.67
And for the matches.df
ddatesmatches
1 1929-10-28
2 1929-11-11
3 1929-11-20
This issue was solved by adding: scale_x_date()+ to the code
p1 <- ggplot(data=plotdataframe, aes(x=ddate, y=SP500close1, colour=SP500close1)) +
scale_x_date()+
geom_vline(data=matches.df,aes(xintercept=as.numeric(ddatesmatches)))+
geom_line() +
theme_bw() +
labs(title="S&P500 Close",y="S&P500 Close",x="Date")+
theme(plot.title = element_text(hjust = 0.5)) +
theme(plot.subtitle = element_text(hjust = 0.5))
grid.arrange(p1)

Resources