-
Notifications
You must be signed in to change notification settings - Fork 0
/
spiralize.Rmd
1438 lines (1154 loc) · 44.7 KB
/
spiralize.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "Visualize Data on Spirals"
author: "Zuguang Gu (z.gu@dkfz.de)"
date: '`r Sys.Date()`'
output:
rmarkdown::html_vignette:
fig_caption: true
css: main.css
toc: true
vignette: >
%\VignetteIndexEntry{Visualize Data on Spirals}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r, echo = FALSE}
library(knitr)
knitr::opts_chunk$set(
error = FALSE,
tidy = FALSE,
message = FALSE,
warning = FALSE,
fig.width = 5,
fig.height = 5,
fig.align = "center",
fig.retina = 2
)
knitr::knit_hooks$set(pngquant = knitr::hook_pngquant)
knitr::opts_chunk$set(
message = FALSE,
dev = "ragg_png",
fig.align = "center",
pngquant = "--speed=10 --quality=30"
)
options(width = 100)
```
```{r, echo = FALSE}
library(spiralize)
library(cowplot)
library(GetoptLong)
```
_This vignette is built with spiralize `r packageDescription('spiralize', fields = "Version")`._
In this vignette, I describe the package **spiralize** which visualizes data along an Archimedean spiral.
It has two major advantages for visualization:
1. It is able to visualize data with very long axis with high resolution.
2. It is efficient for time series data to reveal periodic patterns.
## The Archimedean spiral
In polar coordinates ($r$, $\theta$), the [Archimedean
spiral](https://en.wikipedia.org/wiki/Archimedean_spiral) has the following form:
$$ r = b \cdot \theta $$
where $b$ controls the distance between two loops. The radial distance between two neighbouring loops for a given $\theta$ is:
$$ d(\theta) = r(\theta + 2\pi) - r(\theta) = b \cdot (\theta + 2\pi) - b \cdot \theta = b \cdot 2\pi $$
This shows the radial distance between two neighbouring loops is independent to the value of $\theta$ and is a constant value. The following
figure demonstrates an Archimedean spiral with 4 loops ($\theta \in [0, 8\pi]$).
```{r, echo = FALSE}
theta = seq(0, 360*4, by = 1)/180*pi
b = 1/2/pi
r = theta*b
df = spiralize:::polar_to_cartesian(theta, r)
abs_max_x = max(abs(df$x))
abs_max_y = max(abs(df$y))
grid.newpage()
padding = unit(c(10, 10), "mm")
pushViewport(viewport(
width = unit(1, "snpc") - padding[1],
height = unit(1, "snpc") - padding[2],
xscale = c(-abs_max_x, abs_max_x),
yscale = c(-abs_max_y, abs_max_y)))
d = seq(0, 360, by = 30)
if(d[length(d)] == 360) d = d[-length(d)]
dm = matrix(nrow = length(d), ncol = 4)
for(i in seq_along(d)) {
r0 = max(r + b*2*pi)*1.1
dm[i, ] = c(0, 0, cos(d[i]/180*pi)*r0, sin(d[i]/180*pi)*r0)
}
grid.segments(dm[, 1], dm[, 2], dm[, 3], dm[, 4], default.units = "native",
gp = gpar(col = "grey", lty = 3))
grid.lines(df$x, df$y, default.units = "native")
grid.segments(2, 0, 3, 0, arrow = arrow(angle = 20, length = unit(2, "mm"), ends = "both"), default.units = "native")
grid.text("d", unit(2.5, "native"), unit(0, "native") + unit(1, "mm"), just = "bottom")
popViewport()
```
Note $\theta$ can also be negative values where the spiral is mirrored by _y_-axis
(in Cartesian coordinates). In **spiralize**, we only consider $\theta$ as
positive values. The mirrored spiral can be set by the `flip` argument which is introduced later
in this vignette.
Since the distance between any two neighbouring loops for any given $\theta$ is
constant, it is a ideal place to put tracks along the spiral where the tracks
have identical radial heights everywhere. Later the tracks can be served as
virtual coordinate systems to map to data. This is why the package is called
"_spiralize_" (to transform a normal Cartesian coordinate system to a curved
spiral shape). The following two figures demonstrate a spiral with one track
and with two tracks. The red line is the spiral itself. The spiral ranges
between $\pi/2$ and $6\pi$. It is easy to see the upper border of each track
is also a spiral but with an offset $a$:
$$ r = a + b \cdot \theta $$
where $a$ is the offset to the "Base spiral" (the red spiral in the following plots).
```{r, fig.width = 10, fig.height = 5, echo = FALSE}
grid.newpage()
pushViewport(viewport(x = 0.25, width = 0.5))
spiral_initialize(start = 90, end = 360*3, newpage = FALSE)
spiral_track(height = 0.45)
spiral_lines(TRACK_META$xlim, rep(get_track_data("ymin"), 2), gp = gpar(col = "red"))
spiral_text(2/11, 0.5, "track 1")
spiral_clear()
popViewport()
pushViewport(viewport(x = 0.75, width = 0.5))
spiral_initialize(start = 90, end = 360*3, newpage = FALSE)
spiral_track(height = 0.45)
spiral_lines(TRACK_META$xlim, rep(get_track_data("ymin"), 2), gp = gpar(col = "red"))
spiral_text(2/11, 0.5, "track 1")
spiral_track(height = 0.45, background_gp = gpar(fill = "#CCCCCC"))
spiral_text(2/11, 0.5, "track 2")
spiral_clear()
popViewport()
```
Denote the maximal radius of the spiral as $d_{max} = b \cdot \theta_{max}$,
and denote the length of the spiral as $l$ (which has a complex form, see https://downloads.imagej.net/fiji/snapshots/arc_length.pdf), we can
treat $2d_{max}$ as the resolution of the visualization applied in the normal
Cartesian coordinate system and $l$ as the resolution of the visualization
applied on the spiral. Then the ratio of the two resolutions is:
$$ ratio = \frac{l}{2d_{max}} $$
E.g., for a spiral with 5 loops ($\theta_{max} = 10\pi$), the ratio is 7.89,
which means the spiral improves the resolution of visualization almost to 8
folds. Generally, the ratio increases almost linearly to the number of loops.
```{r, echo = FALSE, fig.keep = "none"}
spiral_initialize()
s = current_spiral()
```
```{r, echo = FALSE}
ratio = function(k) {
theta = pi*2*k
b = 1/2/pi
r = b*theta
len = s$spiral_length(theta)
len/2/r
}
k = 1:20
plot(k, ratio(k), xlab = "number of loops in the spiral", ylab = "ratio",
main = "ratio of the resolution between\nspiral and normal Cartesian system")
```
The relationship between ratio and $\theta$ has the following form:
$$ ratio = \frac{\mathrm{ln}(\theta + \sqrt{1 + \theta^2})}{4\theta} + \frac{\sqrt{1 + \theta^2}}{4} $$
When $\theta$ gets large,
$$ ratio \approx \frac{\mathrm{ln}(\theta + \theta)}{4\theta} + \frac{\theta}{4} \approx \frac{\theta}{4} $$
Denote $k$ as the number of loops, i.e. $\theta = 2\pi \cdot k$, then,
$$ ratio \approx \frac{\theta}{4} = \frac{\pi}{2} \cdot k $$
## The layout of the spiral
The function `spiral_initialize()` is used to intialize the spiral. Arguments `start` and `end` control
the angular range of the spiral. Here the values should be in degrees and they are converted to radians internally.
In **spiralize**, the parameter $b$ in the spiral equation $r = b \cdot \theta$ is set to $b = 1/2\pi$, so that the distance
between two neighbouring loops is $d = 1$. Denote $\theta_e$ as the
end angle (in radians) of the spiral, the ranges of the viewport (under **grid** graphics system) on both _x_-axis and _y_-axis that draw the spiral are $[-x, x]$ where
$$x = b \cdot \theta_e + d = 1/2\pi \cdot \theta_e + 1$$
The following two plots demonstrate different values of `start` and `end`. Also as shown in the following example code,
I suggest to set the values of `start` and `end` in a form of `360*a + b`, e.g. `360*4 + 180`, so that it is straighforward
to know the positions in the polar coordinates and how many loops there are in the spiral (I think people should feel more natural with degrees than radians).
```{r, eval = FALSE}
# the left plot
spiral_initialize(start = 90, end = 360)
spiral_track()
# the right plot
spiral_initialize(start = 180, end = 360*4 + 180)
spiral_track()
```
```{r, fig.width = 10, fig.height = 5, echo = FALSE}
grid.newpage()
pushViewport(viewport(x = 0.25, width = 0.5))
spiral_initialize(start = 90, end = 360, newpage = FALSE)
spiral_track()
spiral_clear()
popViewport()
pushViewport(viewport(x = 0.75, width = 0.5))
spiral_initialize(start = 180, end = 360*4 + 180, newpage = FALSE)
spiral_track()
spiral_clear()
popViewport()
```
Argument `flip` controls how to flip the spiral. It accpets one of the four values: `"none"`/`"horizontal"`/`"vertical"`/`"both"`.
Examples are as follows. In the examples, I additionally add the axes in the tracks to show in which direction the data extends along
the spiral. I also manually adjust the height of the track to give enough space for axes.
```{r, eval = FALSE}
# the top left plot
spiral_initialize(flip = "none") # default
spiral_track(height = 0.6)
spiral_axis()
# the top right plot
spiral_initialize(flip = "horizontal")
spiral_track(height = 0.6)
spiral_axis()
# the bottom left plot
spiral_initialize(flip = "vertical")
spiral_track(height = 0.6)
spiral_axis()
# the bottom right plot
spiral_initialize(flip = "both")
spiral_track(height = 0.6)
spiral_axis()
```
```{r, fig.width = 10, fig.height = 10, echo = FALSE}
p1 = grid.grabExpr({
spiral_initialize()
spiral_track(height = 0.6)
spiral_axis()
grid.text("flip = 'none'", 0, 1, just = c("left", "top"), gp = gpar(fontsize = 14))
})
p2 = grid.grabExpr({
spiral_initialize(flip = "horizontal")
spiral_track(height = 0.6)
spiral_axis()
grid.text("flip = 'horizontal'", 0, 1, just = c("left", "top"), gp = gpar(fontsize = 14))
})
p3 = grid.grabExpr({
spiral_initialize(flip = "vertical")
spiral_track(height = 0.6)
spiral_axis()
grid.text("flip = 'vertical'", 0, 1, just = c("left", "top"), gp = gpar(fontsize = 14))
})
p4 = grid.grabExpr({
spiral_initialize(flip = "both")
spiral_track(height = 0.6)
spiral_axis()
grid.text("flip = 'both'", 0, 1, just = c("left", "top"), gp = gpar(fontsize = 14))
})
plot_grid(p1, p2, p3, p4, nrow = 2)
```
More easily, you can directly set `clockwise = TRUE` to change the orientation of the
spiral. Compare the following plots:
```{r, eval = FALSE}
# the top left plot
spiral_initialize(start = 45 + 360) # default
spiral_track(height = 0.6)
spiral_axis()
# the top right plot
spiral_initialize(start = 45 + 360, clockwise = TRUE)
spiral_track(height = 0.6)
spiral_axis()
# the bottom left plot
spiral_initialize(start = 135 + 360)
spiral_track(height = 0.6)
spiral_axis()
# the bottom right plot
spiral_initialize(start = 135 + 360, clockwise = TRUE)
spiral_track(height = 0.6)
spiral_axis()
```
```{r, fig.width = 10, fig.height = 10, echo = FALSE}
p1 = grid.grabExpr({
spiral_initialize(start = 45 + 360)
spiral_track(height = 0.6)
spiral_axis()
grid.text("default", 0, 1, just = c("left", "top"), gp = gpar(fontsize = 14))
})
p2 = grid.grabExpr({
spiral_initialize(start = 45 + 360, clockwise = TRUE)
spiral_track(height = 0.6)
spiral_axis()
grid.text("clockwise = TRUE", 0, 1, just = c("left", "top"), gp = gpar(fontsize = 14))
})
p3 = grid.grabExpr({
spiral_initialize(start = 135 + 360)
spiral_track(height = 0.6)
spiral_axis()
grid.text("default", 0, 1, just = c("left", "top"), gp = gpar(fontsize = 14))
})
p4 = grid.grabExpr({
spiral_initialize(start = 135 + 360, clockwise = TRUE)
spiral_track(height = 0.6)
spiral_axis()
grid.text("clockwise = TRUE", 0, 1, just = c("left", "top"), gp = gpar(fontsize = 14))
})
plot_grid(p1, p2, p3, p4, nrow = 2)
```
Argument `scale_by` controls how to linearly scale the data on the
spiral. It allows value of `"angle"` or `"curve_length"` (or for short,
`"curve"`). `"angle"` means equal difference on data corresponds to equal
difference of angles in the polar coordinates. `"curve_length"` means equal
difference on data corresponds to equal difference of the length of the
spiral. Observe how the axis ticks distribute in the following two plots. Also
the polar lines are removed for `scale_by = "curve_length"`.
```{r, eval = FALSE}
# the left plot
spiral_initialize(scale_by = "angle") # default
spiral_track(height = 0.6)
spiral_axis()
# the right plot
spiral_initialize(scale_by = "curve_length")
spiral_track(height = 0.6)
spiral_axis()
```
```{r, fig.width = 10, fig.height = 5, echo = FALSE}
p1 = grid.grabExpr({
spiral_initialize(scale_by = "angle")
spiral_track(height = 0.6)
spiral_axis()
grid.text("scale_by = 'angle'", 0, 1, just = c("left", "top"), gp = gpar(fontsize = 14))
})
p2 = grid.grabExpr({
spiral_initialize(scale_by = "curve_length")
spiral_track(height = 0.6)
spiral_axis()
grid.text("scale_by = 'curve_length'", 0, 1, just = c("left", "top"), gp = gpar(fontsize = 14))
})
plot_grid(p1, p2)
```
The following heatmaps might be clearer to demonstrate the difference between `"angle"` and `"curve_length"`. In this
example, each grid has the equal bin size of the data.
```{r, fig.width = 10, fig.height = 5, echo = FALSE}
make_plot = function(scale_by) {
n = 100
col = circlize::colorRamp2(c(0, 0.5, 1), c("blue", "white", "red"))
spiral_initialize(xlim = c(0, n), scale_by = scale_by)
spiral_track(height = 0.9)
x = runif(n)
spiral_rect(1:n - 1, 0, 1:n, 1, gp = gpar(fill = col(x), col = NA))
grid.text(qq("scale_by = '@{scale_by}'"), 0, 1, just = c("left", "top"), gp = gpar(fontsize = 14))
}
p1 = grid.grabExpr(make_plot("angle"))
p2 = grid.grabExpr(make_plot("curve_length"))
plot_grid(p1, p2)
```
As you can see, when `scale_by` is set to `"angle"`, in outer loops, even when the actually difference
on data is the same, the physical widths are larger than these in inner loops. Nevertheless, when the data is
time series or periodic, `"angle"` is the proper choice because it is easy to directly compare between loops
which are the same time points over different periods. As a comparison, `"curve_length"` won't provide any
periodic information.
The spiral grows from inner loops to outer loops, thus, by default, data increases from
the inner loops as well. This can be reversed by setting argument `reverse = TRUE`. See
the following example and also observe the axes. The red arrows indicate the direction of axes.
```{r, eval = FALSE}
# the left plot
spiral_initialize(reverse = FALSE) # default
spiral_track()
spiral_arrow(0.2, 0.8, gp = gpar(fill = "red"))
spiral_axis()
# the right plot
spiral_initialize(reverse = TRUE)
spiral_track()
spiral_arrow(0.2, 0.8, gp = gpar(fill = "red"))
spiral_axis()
```
```{r, fig.width = 10, fig.height = 5, echo = FALSE}
p1 = grid.grabExpr({
spiral_initialize(reverse = FALSE)
spiral_track()
spiral_arrow(0.2, 0.8, gp = gpar(fill = "red"))
spiral_axis()
grid.text("reverse = FALSE", 0, 1, just = c("left", "top"), gp = gpar(fontsize = 14))
})
p2 = grid.grabExpr({
spiral_initialize(reverse = TRUE)
spiral_track()
spiral_arrow(0.2, 0.8, gp = gpar(fill = "red"))
spiral_axis()
grid.text("reverse = TRUE", 0, 1, just = c("left", "top"), gp = gpar(fontsize = 14))
})
plot_grid(p1, p2)
```
To map data to spiral, argument `xlim` should be set which corresponds to data range on _x_-axis.
Observe the axes in the following plots.
```{r, eval = FALSE}
# the left plot
spiral_initialize(xlim = c(0, 1000))
spiral_track(height = 0.6)
spiral_axis()
# the right plot
spiral_initialize(xlim = c(-1000, 1000))
spiral_track(height = 0.6)
spiral_axis()
```
```{r, echo = FALSE, fig.width = 10, fig.height = 5}
p1 = grid.grabExpr({
spiral_initialize(xlim = c(0, 1000))
spiral_track(height = 0.6)
spiral_axis()
})
p2 = grid.grabExpr({
spiral_initialize(xlim = c(-1000, 1000))
spiral_track(height = 0.6)
spiral_axis()
})
plot_grid(p1, p2)
```
Under "angle" mode, the number of loops can also be controlled by argument `period` which controls the length
of data a spiral loop corresponds to. Note in this case, argument `end` is ignored and the value for `end` is
internally recalculated. See the following example:
```{r, eval = FALSE}
# the left plot
spiral_initialize(xlim = c(0, 1), period = 1/3)
spiral_track(height = 0.6)
# the right plot
spiral_initialize(xlim = c(0, 1), period = 2)
spiral_track(height = 0.6)
```
```{r, echo = FALSE, fig.width = 10, fig.height = 5}
p1 = grid.grabExpr({
spiral_initialize(xlim = c(0, 1), period = 1/3)
spiral_track(height = 0.6)
})
p2 = grid.grabExpr({
spiral_initialize(xlim = c(0, 1), period = 2)
spiral_track(height = 0.6)
})
plot_grid(p1, p2)
```
## Create tracks
After the spiral is intialized, next we can add tracks along it. Argument `height` controls
the height of the track. The value of `height` is a value between 0 and 1 which is the fraction of the distance
between two neighbouring loops in the spiral. In the following left plot, I add black border to the track
by setting the argument `background_gp`.
```{r, eval = FALSE}
# the left plot
spiral_initialize()
spiral_track(height = 1, background_gp = gpar(col = "black"))
# the right plot
spiral_initialize()
spiral_track(height = 0.5)
```
```{r, echo = FALSE, fig.width = 10, fig.height = 5}
p1 = grid.grabExpr({
spiral_initialize()
spiral_track(height = 1, background_gp = gpar(col = "black"))
grid.text("height = 1", 0, 1, just = c("left", "top"), gp = gpar(fontsize = 14))
})
p2 = grid.grabExpr({
spiral_initialize()
spiral_track(height = 0.5)
grid.text("height = 0.5", 0, 1, just = c("left", "top"), gp = gpar(fontsize = 14))
})
plot_grid(p1, p2)
```
Multiple tracks can be added sequentially. Just note the sum of heights of all tracks
should not exceed 1.
```{r}
spiral_initialize()
spiral_track(height = 0.4, background_gp = gpar(fill = 2))
spiral_track(height = 0.2, background_gp = gpar(fill = 3))
spiral_track(height = 0.1, background_gp = gpar(fill = 4))
```
The value for `height` can also be `unit` object.
```{r, eval = FALSE}
spiral_initialize()
spiral_track(height = unit(1, "cm"))
```
Data range on _y_-axis is specified by the argument `ylim`. In the following example, I also add a _y_-axis
by `spiral_yaxis()`.
```{r}
spiral_initialize()
spiral_track(ylim = c(0, 100))
spiral_yaxis()
```
Direction of _y_-axis is by default pointing to the outside of spirals. This
direction can be reversed by setting argument `reverse_y = TRUE` in
`spiral_track()`, but in applications it is rarely used.
```{r, eval = FALSE}
spiral_track(reverse_y = TRUE)
```
## Graphics functions
Tracks are created with data ranges on both _x_-axis and _y_-axis. Now the tracks can be thought as
normal Cartesian coordinates. There are following low-level graphics functions so that complex plots
can be easily constructed by combining these low-level graphics functions.
### Points
Like other graphics functions e.g. `points()` or `grid.points()`, the "spiral graphics functions" also
accept locations on _x_-axis and _y_-axis for data points. `spiral_points()` draws points
in the spiral track.
```{r}
spiral_initialize() # by default xlim = c(0, 1)
spiral_track() # by default ylim = c(0, 1)
spiral_points(x = runif(1000), y = runif(1000))
```
### Lines
Adding lines with `spiral_lines()` is also straightforward:
```{r}
x = sort(runif(1000))
y = runif(1000)
spiral_initialize()
spiral_track()
spiral_lines(x, y)
```
Argument `type` can be set to `"h"` so that vertical lines (or radial lines
if you take polar coordinates as reference) are drawn to the baseline for each
data point.
```{r, eval = FALSE}
# the left plot
spiral_initialize()
spiral_track()
spiral_lines(x, y, type = "h")
# the right plot
spiral_initialize()
spiral_track()
spiral_lines(x, y, type = "h", baseline = 0.5, gp = gpar(col = ifelse(y > 0.5, "red", "blue")))
```
```{r, echo = FALSE, fig.width = 10, fig.height = 5}
p1 = grid.grabExpr({
spiral_initialize()
spiral_track()
spiral_lines(x, y, type = "h")
})
p2 = grid.grabExpr({
spiral_initialize()
spiral_track()
spiral_lines(x, y, type = "h", baseline = 0.5, gp = gpar(col = ifelse(y > 0.5, "red", "blue")))
})
plot_grid(p1, p2)
```
Argument `area` can be set to `TRUE` so that area under the lines can be filled with a certain color.
```{r}
spiral_initialize()
spiral_track()
spiral_lines(x, y, area = TRUE, gp = gpar(fill = 2, col = NA))
```
Note you can also set `baseline` with `area = TRUE`, however, you cannot set different colors for the area
above the baseline and below the baseline. Consider to use `spiral_bars()` or `spiral_horizon()` for this scenario.
### Segments
`spiral_segments()` draws a list of segments.
```{r}
n = 1000
x0 = runif(n)
y0 = runif(n)
x1 = x0 + runif(n, min = -0.01, max = 0.01)
y1 = 1 - y0
spiral_initialize(xlim = range(c(x0, x1)))
spiral_track()
spiral_segments(x0, y0, x1, y1,
gp = gpar(col = circlize::rand_color(n, luminosity = "bright"), lwd = runif(n, 0.5, 3)))
```
The same as `grid.segments()`, you can also set the argument `arrow` to add arrows on the segments.
```{r}
n = 100
x0 = runif(n)
y0 = runif(n)
x1 = x0 + runif(n, min = -0.01, max = 0.01)
y1 = 1 - y0
spiral_initialize(xlim = range(c(x0, x1)))
spiral_track()
spiral_segments(x0, y0, x1, y1, arrow = arrow(length = unit(2, "mm")),
gp = gpar(col = circlize::rand_color(n, luminosity = "bright"), lwd = runif(n, 0.5, 3)))
```
### Rectangles
`spiral_rect()` draws rectangles, which is the base function for drawing heatmaps and barplots. The first four
arguments are the coordinates of the bottom left and top right of the rectangles.
```{r}
n = 1000
require(circlize)
spiral_initialize(xlim = c(0, n))
spiral_track(height = 0.9)
x1 = runif(n)
col1 = circlize::colorRamp2(c(0, 0.5, 1), c("blue", "white", "red"))
spiral_rect(1:n - 1, 0, 1:n, 0.5, gp = gpar(fill = col1(x1), col = NA))
x2 = runif(n)
col2 = circlize::colorRamp2(c(0, 0.5, 1), c("green", "white", "red"))
spiral_rect(1:n - 1, 0.5, 1:n, 1, gp = gpar(fill = col2(x2), col = NA))
```
### Bars
`spiral_bars()` can draw bars simply from a numeric vector. Bars can also be drawn to a baseline.
```{r, eval = FALSE}
x = seq(1, 1000, by = 1) - 0.5 # middle points of bars
y = runif(1000)
# the left plot
spiral_initialize(xlim = c(0, 1000))
spiral_track(height = 0.8)
spiral_bars(x, y)
# the right plot
spiral_initialize(xlim = c(0, 1000))
spiral_track(height = 0.8)
spiral_bars(x, y, baseline = 0.5, gp = gpar(fill = ifelse(y > 0.5, 2, 3), col = NA))
```
```{r, echo = FALSE, fig.width = 10, fig.height = 5}
x = seq(1, 1000, by = 1) - 0.5 # middle points of bars
y = runif(1000)
p1 = grid.grabExpr({
spiral_initialize(xlim = c(0, 1000))
spiral_track(height = 0.8)
spiral_bars(x, y)
})
p2 = grid.grabExpr({
spiral_initialize(xlim = c(0, 1000))
spiral_track(height = 0.8)
spiral_bars(x, y, baseline = 0.5, gp = gpar(fill = ifelse(y > 0.5, 2, 3), col = NA))
})
plot_grid(p1, p2)
```
`spiral_bars()` can also draw bars from a matrix, then each column in the matrix correspond to one stack of the bars.
```{r}
y = matrix(runif(3*1000), ncol = 3)
y = y/rowSums(y)
spiral_initialize(xlim = c(0, 1000))
spiral_track(height = 0.8)
spiral_bars(x, y, gp = gpar(fill = 2:4, col = NA))
```
Width of bars can be different. You can set a vector to the argument of `bar_width`. Note `x` always corresponds
to the middle of each bar.
```{r}
w = runif(100)
w = w/sum(w) # width of bars, sum of all width is 1
b = c(0, cumsum(w))
x = (b[1:100] + b[2:101])/2 # middle of each bar
y = runif(100)
spiral_initialize()
spiral_track()
spiral_bars(x, y, bar_width = w)
```
### Polygons
`spiral_polygon()` draws polygons. Note the polygon must be closed, which means, the last data point should overlap to the first one.
```{r}
x0 = sort(runif(200))
x0 = matrix(x0, ncol = 2, byrow = TRUE)
x1 = sort(runif(200))
x1 = matrix(x1, ncol = 2, byrow = TRUE)
spiral_initialize()
spiral_track()
for(i in 1:100) {
pt1 = circlize:::get_bezier_points(x0[i, 1], 0, x1[i, 1], 1, xlim = c(0, 1), ylim = c(0, 1))
pt2 = circlize:::get_bezier_points(x0[i, 2], 0, x1[i, 2], 1, xlim = c(0, 1), ylim = c(0, 1))
spiral_polygon(
c(x0[i, 1], x0[i, 2], pt2[, 1], rev(pt1[, 1]), x0[i, 1]),
c(0, 0, pt2[, 2], rev(pt1[, 2]), 0),
gp = gpar(fill = rand_color(1, luminosity = "bright"), col = NA)
)
}
```
### Text
`spiral_text()` draws texts. Argument `facing` controls the rotation of texts.
```{r, eval = FALSE}
x = seq(0.1, 0.9, length = 26)
text = strrep(letters, 6)
# the top left plot
spiral_initialize()
spiral_track()
spiral_text(x, 0.5, text, facing = "downward") # default
# the bottom left plot
spiral_initialize()
spiral_track()
spiral_text(x, 0.5, text, facing = "inside")
# the bottom right plot
spiral_initialize()
spiral_track()
spiral_text(x, 0.5, text, facing = "outside")
```
```{r, echo = FALSE, fig.width = 10, fig.height = 10}
x = seq(0.1, 0.9, length = 26)
text = strrep(letters, 6)
p1 = grid.grabExpr({
spiral_initialize()
spiral_track()
spiral_text(x, 0.5, text, facing = "downward")
grid.text("facing = 'downward'", 0, 1, just = c("left", "top"), gp = gpar(fontsize = 14))
})
p2 = grid.grabExpr({
grid.newpage()
})
p3 = grid.grabExpr({
spiral_initialize()
spiral_track()
spiral_text(x, 0.5, text, facing = "inside")
grid.text("facing = 'inside'", 0, 1, just = c("left", "top"), gp = gpar(fontsize = 14))
})
p4 = grid.grabExpr({
spiral_initialize()
spiral_track()
spiral_text(x, 0.5, text, facing = "outside")
grid.text("facing = 'outside'", 0, 1, just = c("left", "top"), gp = gpar(fontsize = 14))
})
plot_grid(plot_grid(p1, p2), plot_grid(p3, p4), nrow = 2)
```
Text can also be set to `"clockwise"` or `"reverse_clockwise"`:
```{r, eval = FALSE}
x = seq(0.1, 0.9, length = 26)
# the left plot
spiral_initialize()
spiral_track()
spiral_text(x, 0.5, "aaaa", facing = "clockwise")
# the right plot
spiral_initialize()
spiral_track()
spiral_text(x, 0.5, "aaaa", facing = "reverse_clockwise")
```
```{r, echo = FALSE, fig.width = 10, fig.height = 5}
x = seq(0.1, 0.9, length = 26)
p1 = grid.grabExpr({
spiral_initialize()
spiral_track()
spiral_text(x, 0.5, "aaaa", facing = "clockwise")
grid.text("facing = 'clockwise'", 0, 1, just = c("left", "top"), gp = gpar(fontsize = 14))
})
p2 = grid.grabExpr({
spiral_initialize()
spiral_track()
spiral_text(x, 0.5, "aaaa", facing = "reverse_clockwise")
grid.text("facing = 'reverse_clockwise'", 0, 1, just = c("left", "top"), gp = gpar(fontsize = 14))
})
plot_grid(p1, p2)
```
For long texts, `facing` can be set to `"curved_inside"` or `"curved_outside"` so that curved
texts are draw along the spiral.
```{r, eval = FALSE}
x = seq(0.1, 0.9, length = 10)
text = rep(paste(letters, collapse = ""), 10)
# the left plot
spiral_initialize()
spiral_track()
spiral_text(x, 0.5, text, facing = "curved_inside")
# the right plot
spiral_initialize()
spiral_track()
spiral_text(x, 0.5, text, facing = "curved_outside")
```
```{r, echo = FALSE, fig.width = 10, fig.height = 5}
x = seq(0.1, 0.9, length = 10)
text = rep(paste(letters, collapse = ""), 10)
p1 = grid.grabExpr({
spiral_initialize()
spiral_track()
spiral_text(x, 0.5, text, facing = "curved_inside")
grid.text("facing = 'curved_inside'", 0, 1, just = c("left", "top"), gp = gpar(fontsize = 14))
}, width = 5, height = 5)
p2 = grid.grabExpr({
spiral_initialize()
spiral_track()
spiral_text(x, 0.5, text, facing = "curved_outside")
grid.text("facing = 'curved_outside'", 0, 1, just = c("left", "top"), gp = gpar(fontsize = 14))
}, width = 5, height = 5)
plot_grid(p1, p2)
```
Calculation of positions of letters of the curved text depends on the size of current graphics device. When the device
changes its size, the positions of letters will not be correct and you need to regenerate the plot. Also
users need to be careful when using `grid.grabExpr()` to capture the plot. By default `grid.grabExpr()` captures
graphics output in a device with 7inch x 7inch. Users might need to manually set the device size to make sure
the curved texts are not affected.
In the next example, I use `grid.grabExpr()` to capture two spiral plots with curved texts. Later the two plots are merged
with using the **cowplot** package and the final merged plot is saved in a PDF with 10 inches width and 5 inches height.
I manually set the device size in the two `grid.grabExpr()` calls so that the size of the place where the graphics are captured
is the same as the size of the place where they are finally drawn.
```{r, eval = FALSE}
p1 = grid.grabExpr({
spiral_initialize()
spiral_track()
spiral_text(x, 0.5, text, facing = "curved_inside")
}, width = 5, height = 5)
p2 = grid.grabExpr({
spiral_initialize()
spiral_track()
spiral_text(x, 0.5, text, facing = "curved_outside")
}, width = 5, height = 5)
pdf(..., width = 10, height = 5)
plot_grid(p1, p2)
dev.off()
```
one last thing for drawing text is that the argument `nice_facing` can be set to `TRUE` so that the rotation
of texts are automatically adjusted so that they are easy to read, i.e. all the texts always face the lower part of the polar coordinate system.
### Axis
`spiral_aixs()` draws axis along the spiral. So it is the _x_-axis of the data.
```{r}
spiral_initialize()
spiral_track(height = 0.6)
spiral_axis()
```
Argument `major_at` or simply `at` controls the break points on the axis and argument `labels`
controls the corresponding axis labels.
```{r, eval = FALSE}
# the left plot
spiral_initialize(xlim = c(0, 360*4), start = 360, end = 360*5)
spiral_track(height = 0.6)
spiral_axis(major_at = seq(0, 360*4, by = 30))
# the right plot
spiral_initialize(xlim = c(0, 12*4), start = 360, end = 360*5)
spiral_track(height = 0.6)
spiral_axis(major_at = seq(0, 12*4, by = 1), labels = c("", rep(month.name, 4)))
```
```{r, echo = FALSE, fig.width = 10, fig.height = 5}
p1 = grid.grabExpr({
spiral_initialize(xlim = c(0, 360*4), start = 360, end = 360*5)
spiral_track(height = 0.6)
spiral_axis(major_at = seq(0, 360*4, by = 30))
})
p2 = grid.grabExpr({
spiral_initialize(xlim = c(0, 12*4), start = 360, end = 360*5)
spiral_track(height = 0.6)
spiral_axis(major_at = seq(0, 12*4, by = 1), labels = c("", rep(month.name, 4)))
})
plot_grid(p1, p2)
```
If the axis labels are too long, argument `curved_labels` can be set to `TRUE` so that the labels
are curved along the spiral.
```{r}
spiral_initialize()
spiral_track(height = 0.6)
spiral_axis(at = c(0.1, 0.3, 0.6, 0.9), labels = strrep(letters[1:4], 20), curved_labels = TRUE)
```
`spiral_yaxis()` draws _y_-axis. Argument `side` controls which side of the track to put
the _y_-axis. `side` can be set to `"both"` so that _y_-axis is drawn on the two sides of the track.
```{r, eval = FALSE}
# the left plot
spiral_initialize()
spiral_track(height = 0.8)
spiral_yaxis(side = "start")
spiral_yaxis(side = "end", at = c(0, 0.25, 0.5, 0.75, 1), labels = letters[1:5])
# the right plot
spiral_initialize()
spiral_track(height = 0.8)
spiral_yaxis(side = "both")
```
```{r, echo = FALSE, fig.width = 10, fig.height = 5}
p1 = grid.grabExpr({
spiral_initialize()
spiral_track(height = 0.8)
spiral_yaxis(side = "start")
spiral_yaxis(side = "end", at = c(0, 0.25, 0.5, 0.75, 1), labels = letters[1:5])
})