-
-
Notifications
You must be signed in to change notification settings - Fork 81
/
add.R
3787 lines (3351 loc) · 100 KB
/
add.R
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
#' Bar and Line chart
#'
#' Add bar serie.
#'
#' @param e An \code{echarts4r} object as returned by \code{\link{e_charts}} or
#' a proxy as returned by \code{\link{echarts4rProxy}}.
#' @param serie Column name of serie to plot.
#' @param bind Binding between datasets, namely for use of \code{\link{e_brush}}.
#' @param name name of the serie.
#' @param legend Whether to add serie to legend.
#' @param ... Any other option to pass, check See Also section.
#' @param x_index,y_index Indexes of x and y axis.
#' @param coord_system Coordinate system to plot against.
#'
#' @examples
#' library(dplyr)
#'
#' mtcars %>%
#' mutate(
#' model = row.names(.),
#' total = mpg + qsec
#' ) %>%
#' arrange(desc(total)) %>%
#' e_charts(model) %>%
#' e_bar(mpg, stack = "grp") %>%
#' e_bar(qsec, stack = "grp")
#' @seealso \href{https://echarts.apache.org/en/option.html#series-bar}{Additional arguments}
#'
#' @rdname e_bar
#' @export
e_bar <- function(e, serie, bind, name = NULL, legend = TRUE, y_index = 0, x_index = 0, coord_system = "cartesian2d", ...) UseMethod("e_bar")
#' @method e_bar echarts4r
#' @export
e_bar.echarts4r <- function(e, serie, bind, name = NULL, legend = TRUE, y_index = 0, x_index = 0, coord_system = "cartesian2d", ...) {
if (missing(serie)) {
stop("must pass serie", call. = FALSE)
}
if (missing(bind)) {
bd <- NULL
} else {
bd <- deparse(substitute(bind))
}
sr <- deparse(substitute(serie))
e_bar_(e, sr, bd, name, legend, y_index, x_index, coord_system, ...)
}
#' @method e_bar echarts4rProxy
#' @export
e_bar.echarts4rProxy <- function(e, serie, bind, name = NULL, legend = TRUE, y_index = 0, x_index = 0, coord_system = "cartesian2d", ...) {
if (missing(serie)) {
stop("must pass serie", call. = FALSE)
}
if (missing(bind)) {
bd <- NULL
} else {
bd <- deparse(substitute(bind))
}
sr <- deparse(substitute(serie))
e$chart <- e_bar_(e$chart, sr, bd, name, legend, y_index, x_index, coord_system, ...)
return(e)
}
#' Line
#'
#' Add line serie.
#'
#' @inheritParams e_bar
#' @param coord_system Coordinate system to plot against.
#'
#' @examples
#' iris %>%
#' group_by(Species) %>%
#' e_charts(Sepal.Length) %>%
#' e_line(Sepal.Width) %>%
#' e_tooltip(trigger = "axis")
#'
#' # timeline
#' iris %>%
#' group_by(Species) %>%
#' e_charts(Sepal.Length, timeline = TRUE) %>%
#' e_line(Sepal.Width) %>%
#' e_tooltip(trigger = "axis")
#' @seealso \href{https://echarts.apache.org/en/option.html#series-line}{Additional arguments}
#'
#' @rdname e_line
#' @export
e_line <- function(e, serie, bind, name = NULL, legend = TRUE, y_index = 0, x_index = 0,
coord_system = "cartesian2d", ...) {
UseMethod("e_line")
}
#' @export
#' @method e_line echarts4r
e_line.echarts4r <- function(e, serie, bind, name = NULL, legend = TRUE, y_index = 0, x_index = 0,
coord_system = "cartesian2d", ...) {
if (missing(serie)) {
stop("must pass serie", call. = FALSE)
}
serie <- deparse(substitute(serie))
if (missing(bind)) {
bd <- NULL
} else {
bd <- deparse(substitute(bind))
}
e_line_(e, serie, bd, name, legend, y_index, x_index, coord_system, ...)
}
#' @export
#' @method e_line echarts4rProxy
e_line.echarts4rProxy <- function(e, serie, bind, name = NULL, legend = TRUE, y_index = 0, x_index = 0,
coord_system = "cartesian2d", ...) {
if (missing(serie)) {
stop("must pass serie", call. = FALSE)
}
serie <- deparse(substitute(serie))
if (missing(bind)) {
bd <- NULL
} else {
bd <- deparse(substitute(bind))
}
e$chart <- e_line_(e$chart, serie, bd, name, legend, y_index, x_index, coord_system, ...)
return(e)
}
#' Area
#'
#' Add area serie.
#'
#' @inheritParams e_bar
#'
#' @examples
#' CO2 %>%
#' group_by(Plant) %>%
#' e_charts(conc) %>%
#' e_area(uptake) %>%
#' e_tooltip(trigger = "axis")
#'
#' # timeline
#' iris %>%
#' group_by(Species) %>%
#' e_charts(Sepal.Length, timeline = TRUE) %>%
#' e_area(Sepal.Width) %>%
#' e_tooltip(trigger = "axis")
#' @seealso \href{https://echarts.apache.org/en/option.html#series-line}{Additional arguments}
#'
#' @rdname e_area
#' @export
e_area <- function(e, serie, bind, name = NULL, legend = TRUE, y_index = 0, x_index = 0,
coord_system = "cartesian2d", ...) {
UseMethod("e_area")
}
#' @export
#' @method e_area echarts4r
e_area.echarts4r <- function(e, serie, bind, name = NULL, legend = TRUE, y_index = 0, x_index = 0,
coord_system = "cartesian2d", ...) {
if (missing(serie)) {
stop("must pass serie", call. = FALSE)
}
serie <- deparse(substitute(serie))
if (missing(bind)) {
bd <- NULL
} else {
bd <- deparse(substitute(bind))
}
e_area_(e, serie, bd, name, legend, y_index, x_index, coord_system, ...)
}
#' @export
#' @method e_area echarts4rProxy
e_area.echarts4rProxy <- function(e, serie, bind, name = NULL, legend = TRUE, y_index = 0, x_index = 0,
coord_system = "cartesian2d", ...) {
if (missing(serie)) {
stop("must pass serie", call. = FALSE)
}
serie <- deparse(substitute(serie))
if (missing(bind)) {
bd <- NULL
} else {
bd <- deparse(substitute(bind))
}
e$chart <- e_area_(e$chart, serie, bd, name, legend, y_index, x_index, coord_system, ...)
return(e)
}
#' Step
#'
#' Add step serie.
#'
#' @inheritParams e_bar
#' @param step Step type, one of \code{start}, \code{middle} or \code{end}.
#' @param fill Set to fill as area.
#'
#' @examples
#' USArrests %>%
#' dplyr::mutate(State = row.names(.)) %>%
#' e_charts(State) %>%
#' e_step(Murder, name = "Start", step = "start", fill = TRUE) %>%
#' e_step(Rape, name = "Middle", step = "middle") %>%
#' e_step(Assault, name = "End", step = "end") %>%
#' e_tooltip(trigger = "axis")
#'
#' # timeline
#' iris %>%
#' group_by(Species) %>%
#' e_charts(Sepal.Length, timeline = TRUE) %>%
#' e_step(Sepal.Width) %>%
#' e_tooltip(trigger = "axis")
#' @seealso \href{https://echarts.apache.org/en/option.html#series-line}{Additional arguments}
#'
#' @rdname e_step
#' @export
e_step <- function(e, serie, bind, step = c("start", "middle", "end"), fill = FALSE,
name = NULL, legend = TRUE, y_index = 0, x_index = 0, coord_system = "cartesian2d",
...) {
UseMethod("e_step")
}
#' @export
#' @method e_step echarts4r
e_step.echarts4r <- function(e, serie, bind, step = c("start", "middle", "end"), fill = FALSE,
name = NULL, legend = TRUE, y_index = 0, x_index = 0, coord_system = "cartesian2d",
...) {
if (missing(serie)) {
stop("must pass serie", call. = FALSE)
}
serie <- deparse(substitute(serie))
if (missing(bind)) {
bd <- NULL
} else {
bd <- deparse(substitute(bind))
}
e_step_(e, serie, bd, step, fill, name, legend, y_index, x_index, coord_system = "cartesian2d", ...)
}
#' @export
#' @method e_step echarts4rProxy
e_step.echarts4rProxy <- function(e, serie, bind, step = c("start", "middle", "end"), fill = FALSE,
name = NULL, legend = TRUE, y_index = 0, x_index = 0, coord_system = "cartesian2d",
...) {
if (missing(serie)) {
stop("must pass serie", call. = FALSE)
}
serie <- deparse(substitute(serie))
if (missing(bind)) {
bd <- NULL
} else {
bd <- deparse(substitute(bind))
}
e$chart <- e_step_(e$chart, serie, bd, step, fill, name, legend, y_index, x_index, coord_system = "cartesian2d", ...)
return(e)
}
#' Scatter
#'
#' Add scatter serie.
#'
#' @inheritParams e_bar
#' @param size Column name containing size of points.
#' @param symbol The symbol to use, default to \code{NULL}, can also be \code{circle}, \code{rect},
#' \code{roundRect}, \code{triangle}, \code{diamond}, \code{pin}, \code{arrow}, or \code{none}.
#' @param symbol_size Size of points, either an integer or a vector of length 2,
#' if \code{size} is \emph{not} \code{NULL} or missing it is applied as a multiplier to \code{scale}.
#' @param scale A function that takes a vector of \code{numeric} and returns a vector of \code{numeric}
#' of the same length. You can disable the scaling by setting it to \code{NULL}.
#' @param coord_system Coordinate system to plot against, see examples.
#' @param rm_x,rm_y Whether to remove x and y axis, only applies if \code{coord_system} is not
#' set to \code{cartesian2d}.
#' @param x A vector of integers or numeric.
#' @param jitter_factor,jitter_amount Jitter points, passed to \code{jitter}.
#' @param scale_js the JavaScript scaling function.
#'
#' @section Scaling function: defaults to \code{e_scale} which is a basic function that rescales \code{size}
#' between 1 and 20 for that makes for decent sized points on the chart.
#'
#' @examples
#' # scaling
#' e_scale(c(1, 1000))
#'
#' mtcars %>%
#' e_charts(mpg) %>%
#' e_scatter(wt, qsec)
#'
#' # custom function
#' my_scale <- function(x) scales::rescale(x, to = c(2, 50))
#'
#' echart <- mtcars %>%
#' e_charts(mpg) %>%
#' e_scatter(wt, qsec, scale = my_scale)
#'
#' echart
#'
#' # rescale color too
#' echart %>%
#' e_visual_map(wt, scale = my_scale)
#'
#' # or
#' echart %>%
#' e_visual_map(min = 2, max = 50)
#'
#' # disable scaling
#' mtcars %>%
#' e_charts(qsec) %>%
#' e_scatter(wt, mpg, scale = NULL)
#'
#' # jitter point
#' mtcars %>%
#' e_charts(cyl) %>%
#' e_scatter(wt, symbol_size = 5) %>%
#' e_scatter(wt, jitter_factor = 2, legend = FALSE)
#'
#' # examples
#' USArrests %>%
#' e_charts(Assault) %>%
#' e_scatter(Murder, Rape) %>%
#' e_effect_scatter(Rape, Murder, y_index = 1) %>%
#' e_grid(index = c(0, 1)) %>%
#' e_tooltip()
#'
#' iris %>%
#' e_charts_("Sepal.Length") %>%
#' e_scatter_(
#' "Sepal.Width",
#' symbol_size = c(8, 2),
#' symbol = "rect"
#' ) %>%
#' e_x_axis(min = 4)
#'
#' quakes %>%
#' e_charts(long) %>%
#' e_geo(
#' roam = TRUE,
#' boundingCoords = list(
#' c(185, -10),
#' c(165, -40)
#' )
#' ) %>%
#' e_scatter(lat, mag, coord_system = "geo") %>%
#' e_visual_map(min = 4, max = 6.5)
#'
#' # timeline
#' iris %>%
#' group_by(Species) %>%
#' e_charts(Petal.Width, timeline = TRUE) %>%
#' e_scatter(Sepal.Width, Sepal.Length) %>%
#' e_tooltip(trigger = "axis")
#' @seealso \href{https://echarts.apache.org/en/option.html#series-scatter}{Additional arguments scatter},
#' \href{https://echarts.apache.org/en/option.html#series-effectScatter}{Additional arguments for effect scatter}
#'
#' @rdname scatter
#' @export
e_scatter <- function(e, serie, size, bind, symbol = NULL, symbol_size = 1, scale = e_scale,
scale_js = "function(data){ return data[3];}", name = NULL,
coord_system = "cartesian2d", jitter_factor = 0,
jitter_amount = NULL, legend = TRUE, y_index = 0,
x_index = 0, rm_x = TRUE, rm_y = TRUE, ...) {
UseMethod("e_scatter")
}
#' @export
#' @method e_scatter echarts4r
e_scatter.echarts4r <- function(e, serie, size, bind, symbol = NULL, symbol_size = 1, scale = e_scale,
scale_js = "function(data){ return data[3];}", name = NULL,
coord_system = "cartesian2d", jitter_factor = 0,
jitter_amount = NULL, legend = TRUE, y_index = 0,
x_index = 0, rm_x = TRUE, rm_y = TRUE, ...) {
if (missing(serie)) {
stop("must pass serie", call. = FALSE)
}
serie <- deparse(substitute(serie))
if (missing(size)) {
size <- NULL
} else {
size <- deparse(substitute(size))
}
if (missing(bind)) {
bd <- NULL
} else {
bd <- deparse(substitute(bind))
}
e_scatter_(
e = e, serie = serie, size = size, bind = bd, symbol = symbol, symbol_size = symbol_size,
scale = scale, scale_js = scale_js, name = name, coord_system = coord_system,
jitter_factor = jitter_factor, jitter_amount = jitter_amount,
legend = legend, y_index = y_index, x_index = x_index, rm_x = rm_x,
rm_y = rm_y, ...
)
}
#' @export
#' @method e_scatter echarts4rProxy
e_scatter.echarts4rProxy <- function(e, serie, size, bind, symbol = NULL, symbol_size = 1, scale = e_scale,
scale_js = "function(data){ return data[3];}", name = NULL,
coord_system = "cartesian2d", jitter_factor = 0,
jitter_amount = NULL, legend = TRUE, y_index = 0,
x_index = 0, rm_x = TRUE, rm_y = TRUE, ...) {
if (missing(serie)) {
stop("must pass serie", call. = FALSE)
}
serie <- deparse(substitute(serie))
if (missing(size)) {
size <- NULL
} else {
size <- deparse(substitute(size))
}
if (missing(bind)) {
bd <- NULL
} else {
bd <- deparse(substitute(bind))
}
e$chart <- e_scatter_(
e = e$chart, serie = serie, size = size, bind = bd, symbol = symbol, symbol_size = symbol_size,
scale = scale, scale_js = scale_js, name = name, coord_system = coord_system,
jitter_factor = jitter_factor, jitter_amount = jitter_amount,
legend = legend, y_index = y_index, x_index = x_index, rm_x = rm_x,
rm_y = rm_y, ...
)
return(e)
}
#' @rdname scatter
#' @export
e_effect_scatter <- function(e, serie, size, bind, symbol = NULL, symbol_size = 1, scale = e_scale,
scale_js = "function(data){ return data[3];}", name = NULL,
coord_system = "cartesian2d", legend = TRUE,
y_index = 0, x_index = 0, rm_x = TRUE, rm_y = TRUE, ...) {
UseMethod("e_effect_scatter")
}
#' @export
#' @method e_effect_scatter echarts4r
e_effect_scatter.echarts4r <- function(e, serie, size, bind, symbol = NULL, symbol_size = 1, scale = e_scale,
scale_js = "function(data){ return data[3];}", name = NULL,
coord_system = "cartesian2d", legend = TRUE,
y_index = 0, x_index = 0, rm_x = TRUE, rm_y = TRUE, ...) {
if (missing(serie)) {
stop("must pass serie", call. = FALSE)
}
serie <- deparse(substitute(serie))
if (missing(size)) {
size <- NULL
} else {
size <- deparse(substitute(size))
}
if (missing(bind)) {
bd <- NULL
} else {
bd <- deparse(substitute(bind))
}
e_effect_scatter_(e,
serie = serie, size = size, bind = bd, symbol = symbol,
symbol_size = symbol_size, scale = scale, scale_js = scale_js,
name = name, coord_system, legend,
y_index, x_index, rm_x, rm_y, ...
)
}
#' @export
#' @method e_effect_scatter echarts4rProxy
e_effect_scatter.echarts4rProxy <- function(e, serie, size, bind, symbol = NULL, symbol_size = 1, scale = e_scale,
scale_js = "function(data){ return data[3];}", name = NULL,
coord_system = "cartesian2d", legend = TRUE,
y_index = 0, x_index = 0, rm_x = TRUE, rm_y = TRUE, ...) {
if (missing(serie)) {
stop("must pass serie", call. = FALSE)
}
serie <- deparse(substitute(serie))
if (missing(size)) {
size <- NULL
} else {
size <- deparse(substitute(size))
}
if (missing(bind)) {
bd <- NULL
} else {
bd <- deparse(substitute(bind))
}
e$chart <- e_effect_scatter_(e$chart,
serie = serie, size = size, bind = bd, symbol = symbol,
symbol_size = symbol_size, scale = scale, scale_js = scale_js,
name = name, coord_system, legend,
y_index, x_index, rm_x, rm_y, ...
)
return(e)
}
#' Candlestick
#'
#' Add a candlestick chart.
#'
#' @inheritParams e_bar
#' @param opening,closing,low,high Stock prices.
#'
#' @examples
#' date <- c(
#' "2017-01-01", "2017-01-02", "2017-01-03", "2017-01-04", "2017-03-05",
#' "2017-01-06", "2017-01-07"
#' )
#'
#' stock <- data.frame(
#' date = date,
#' opening = c(200.60, 200.22, 198.43, 199.05, 203.54, 203.40, 208.34),
#' closing = c(200.72, 198.85, 199.05, 203.73, 204.08, 208.11, 211.88),
#' low = c(197.82, 198.07, 197.90, 198.10, 202.00, 201.50, 207.60),
#' high = c(203.32, 200.67, 200.00, 203.95, 204.90, 208.44, 213.17)
#' )
#'
#' stock %>%
#' e_charts(date) %>%
#' e_candle(opening, closing, low, high) %>%
#' e_y_axis(min = 190, max = 220)
#' @seealso \href{https://echarts.apache.org/en/option.html#series-candlestick}{Additional arguments}
#'
#' @rdname e_candle
#' @export
e_candle <- function(e, opening, closing, low, high, bind, name = NULL, legend = TRUE, ...) UseMethod("e_candle")
#' @export
#' @method e_candle echarts4r
e_candle.echarts4r <- function(e, opening, closing, low, high, bind, name = NULL, legend = TRUE, ...) {
if (missing(opening) || missing(closing) || missing(low) || missing(high)) {
stop("missing inputs", call. = FALSE)
}
if (!missing(bind)) {
bind <- deparse(substitute(bind))
} else {
bind <- NULL
}
e_candle_(
e, deparse(substitute(opening)),
deparse(substitute(closing)),
deparse(substitute(low)),
deparse(substitute(high)),
bind, name, legend, ...
)
}
#' @export
#' @method e_candle echarts4rProxy
e_candle.echarts4rProxy <- function(e, opening, closing, low, high, bind, name = NULL, legend = TRUE, ...) {
if (missing(opening) || missing(closing) || missing(low) || missing(high)) {
stop("missing inputs", call. = FALSE)
}
if (!missing(bind)) {
bind <- deparse(substitute(bind))
} else {
bind <- NULL
}
e$chart <- e_candle_(
e$chart, deparse(substitute(opening)),
deparse(substitute(closing)),
deparse(substitute(low)),
deparse(substitute(high)),
bind, name, legend, ...
)
return(e)
}
#' Radar
#'
#' Add a radar chart
#'
#' @inheritParams e_bar
#' @param max Maximum value.
#' @param rm_x,rm_y Whether to remove x and y axis, defaults to \code{TRUE}.
#' @param radar A \code{list} of options to pass to the \code{radar}
#' rather than the serie, see \href{https://echarts.apache.org/en/option.html#radar}{official documentation}
#' alternatively, use the \code{\link{e_radar_opts}}.
#'
#' @examples
#' df <- data.frame(
#' x = LETTERS[1:5],
#' y = runif(5, 1, 5),
#' z = runif(5, 3, 7)
#' )
#'
#' df %>%
#' e_charts(x) %>%
#' e_radar(y, max = 7) %>%
#' e_radar(z) %>%
#' e_tooltip(trigger = "item")
#' @rdname e_radar
#' @export
e_radar <- function(e, serie, max = 100, name = NULL, legend = TRUE,
rm_x = TRUE, rm_y = TRUE, ..., radar = list()) {
UseMethod("e_radar")
}
#' @export
#' @method e_radar echarts4r
e_radar.echarts4r <- function(e, serie, max = 100, name = NULL, legend = TRUE,
rm_x = TRUE, rm_y = TRUE, ..., radar = list()) {
if (missing(serie)) {
stop("must pass serie", call. = FALSE)
}
serie <- deparse(substitute(serie))
e_radar_(e, serie, max, name, legend, rm_x, rm_y, ..., radar = radar)
}
#' @export
#' @method e_radar echarts4rProxy
e_radar.echarts4rProxy <- function(e, serie, max = 100, name = NULL, legend = TRUE,
rm_x = TRUE, rm_y = TRUE, ..., radar = list()) {
if (missing(serie)) {
stop("must pass serie", call. = FALSE)
}
serie <- deparse(substitute(serie))
e$chart <- e_radar_(e, serie, max, name, legend, rm_x, rm_y, ..., radar = radar)
return(e)
}
#' Funnel
#'
#' Add a funnel.
#'
#' @param e An \code{echarts4r} object as returned by \code{\link{e_charts}}.
#' @param name name of the serie.
#' @param legend Whether to add serie to legend.
#' @param ... Any other option to pass to \code{bar} or \code{line} char types.
#' @param values,labels Values and labels of funnel.
#' @param rm_x,rm_y Whether to remove x and y axis, defaults to \code{TRUE}.
#'
#' @details No \code{bind} argument here, with a funnel \code{bind} = \code{labels}.
#'
#' @examples
#' funnel <- data.frame(
#' stage = c("View", "Click", "Purchase"),
#' value = c(80, 30, 20)
#' )
#'
#' funnel %>%
#' e_charts() %>%
#' e_funnel(value, stage)
#' @seealso \href{https://echarts.apache.org/en/option.html#series-funnel}{Additional arguments}
#'
#' @rdname e_funnel
#' @export
e_funnel <- function(e, values, labels, name = NULL, legend = TRUE, rm_x = TRUE, rm_y = TRUE, ...) UseMethod("e_funnel")
#' @export
#' @method e_funnel echarts4r
e_funnel.echarts4r <- function(e, values, labels, name = NULL, legend = TRUE, rm_x = TRUE, rm_y = TRUE, ...) {
if (missing(values) || missing(labels)) {
stop("missing values or labels", call. = FALSE)
}
e_funnel_(
e = e,
values = deparse(substitute(values)),
labels = deparse(substitute(labels)),
name = name,
legend = legend,
rm_x = rm_x,
rm_y = rm_y,
...
)
}
#' @export
#' @method e_funnel echarts4rProxy
e_funnel.echarts4rProxy <- function(e, values, labels, name = NULL, legend = TRUE, rm_x = TRUE, rm_y = TRUE, ...) {
if (missing(values) || missing(labels)) {
stop("missing values or labels", call. = FALSE)
}
e$chart <- e_funnel_(
e = e$chart,
values = deparse(substitute(values)),
labels = deparse(substitute(labels)),
name = name,
legend = legend,
rm_x = rm_x,
rm_y = rm_y,
...
)
return(e)
}
#' Sankey
#'
#' Draw a sankey diagram.
#'
#' @inheritParams e_bar
#' @param layout Layout of sankey.
#' @param source,target Source and target columns.
#' @param value Value change from \code{source} to \code{target}.
#' @param rm_x,rm_y Whether to remove the x and y axis, defaults to \code{TRUE}.
#'
#' @examples
#' sankey <- data.frame(
#' source = c("a", "b", "c", "d", "c"),
#' target = c("b", "c", "d", "e", "e"),
#' value = ceiling(rnorm(5, 10, 1)),
#' stringsAsFactors = FALSE
#' )
#'
#' sankey %>%
#' e_charts() %>%
#' e_sankey(source, target, value)
#' @seealso \href{https://echarts.apache.org/en/option.html#series-sankey}{Additional arguments}
#'
#' @rdname e_sankey
#' @export
e_sankey <- function(e, source, target, value, layout = "none", rm_x = TRUE, rm_y = TRUE, ...) UseMethod("e_sankey")
#' @export
#' @method e_sankey echarts4r
e_sankey.echarts4r <- function(e, source, target, value, layout = "none", rm_x = TRUE, rm_y = TRUE, ...) {
if (missing(source) || missing(target) || missing(value)) {
stop("missing source, target or values", call. = FALSE)
}
e <- .rm_axis(e, rm_x, "x")
e <- .rm_axis(e, rm_y, "y")
e_sankey_(
e, deparse(substitute(source)), deparse(substitute(target)),
deparse(substitute(value)), layout, rm_x, rm_y, ...
)
}
#' @export
#' @method e_sankey echarts4rProxy
e_sankey.echarts4rProxy <- function(e, source, target, value, layout = "none", rm_x = TRUE, rm_y = TRUE, ...) {
if (missing(source) || missing(target) || missing(value)) {
stop("missing source, target or values", call. = FALSE)
}
e <- .rm_axis(e, rm_x, "x")
e <- .rm_axis(e, rm_y, "y")
e$chart <- e_sankey_(
e$chart, deparse(substitute(source)), deparse(substitute(target)),
deparse(substitute(value)), layout, rm_x, rm_y, ...
)
return(e)
}
#' Graph
#'
#' Create a graph.
#'
#' @param e An \code{echarts4} object as returned by \code{e_charts}.
#' @param name Name of graph.
#' @param nodes Data.frame of nodes.
#' @param names Names of nodes, unique.
#' @param value values of nodes.
#' @param size Size of nodes.
#' @param symbol Symbols of nodes.
#' @param legend Whether to add serie to legend.
#' @param category Group of nodes (i.e.: group membership).
#' @param edges Data.frame of edges.
#' @param source,target Column names of source and target.
#' @param layout Layout, one of \code{force}, \code{none} or \code{circular}.
#' @param rm_x,rm_y Whether to remove the x and y axis, defaults to \code{TRUE}.
#' @param ... Any other parameter.
#'
#' @examples
#' value <- rnorm(10, 10, 2)
#'
#' nodes <- data.frame(
#' name = sample(LETTERS, 10),
#' value = value,
#' size = value,
#' symbol = sample(c("circle", "rect", "triangle"), 10, replace = TRUE),
#' grp = rep(c("grp1", "grp2"), 5),
#' stringsAsFactors = FALSE
#' )
#'
#' edges <- data.frame(
#' source = sample(nodes$name, 20, replace = TRUE),
#' target = sample(nodes$name, 20, replace = TRUE),
#' stringsAsFactors = FALSE
#' )
#'
#' e_charts() %>%
#' e_graph() %>%
#' e_graph_nodes(nodes, name, value, size, grp, symbol) %>%
#' e_graph_edges(edges, source, target)
#'
#' # Use graphGL for larger networks
#' nodes <- data.frame(
#' name = paste0(LETTERS, 1:1000),
#' value = rnorm(1000, 10, 2),
#' size = rnorm(1000, 10, 2),
#' grp = rep(c("grp1", "grp2"), 500),
#' stringsAsFactors = FALSE
#' )
#'
#' edges <- data.frame(
#' source = sample(nodes$name, 2000, replace = TRUE),
#' target = sample(nodes$name, 2000, replace = TRUE),
#' stringsAsFactors = FALSE
#' )
#'
#' e_charts() %>%
#' e_graph_gl() %>%
#' e_graph_nodes(nodes, name, value, size, grp) %>%
#' e_graph_edges(edges, source, target)
#' @seealso \href{https://echarts.apache.org/en/option.html#series-graph}{Additional arguments},
#' \code{\link{e_modularity}}
#'
#' @rdname graph
#' @export
e_graph <- function(e, layout = "force", name = NULL, rm_x = TRUE, rm_y = TRUE, ...) UseMethod("e_graph")
#' @export
#' @method e_graph echarts4r
e_graph.echarts4r <- function(e, layout = "force", name = NULL, rm_x = TRUE, rm_y = TRUE, ...) {
if (missing(e)) {
stop("must pass e", call. = FALSE)
}
e <- .rm_axis(e, rm_x, "x")
e <- .rm_axis(e, rm_y, "y")
serie <- list(
name = name,
type = "graph",
layout = layout,
...
)
e$x$opts$series <- append(e$x$opts$series, list(serie))
# add dependency
path <- system.file("htmlwidgets/lib/echarts-4.8.0", package = "echarts4r")
dep <- htmltools::htmlDependency(
name = "echarts-gl",
version = "1.1.2",
src = c(file = path),
script = "echarts-gl.min.js"
)
e$dependencies <- append(e$dependencies, list(dep))
e
}
#' @export
#' @method e_graph echarts4rProxy
e_graph.echarts4rProxy <- function(e, layout = "force", name = NULL, rm_x = TRUE, rm_y = TRUE, ...) {
e$chart <- e_graph(e$chart, layout, name, rm_x, rm_y, ...)
return(e)
}
#' @rdname graph
#' @export
e_graph_gl <- function(e, layout = "force", name = NULL, rm_x = TRUE, rm_y = TRUE, ...) UseMethod("e_graph_gl")
#' @export
#' @method e_graph_gl echarts4r
e_graph_gl.echarts4r <- function(e, layout = "force", name = NULL, rm_x = TRUE, rm_y = TRUE, ...) {
e <- .rm_axis(e, rm_x, "x")
e <- .rm_axis(e, rm_y, "y")
serie <- list(
name = name,
type = "graphGL",
layout = layout,
...
)
e$x$opts$series <- append(e$x$opts$series, list(serie))
e
}
#' @export
#' @method e_graph_gl echarts4rProxy
e_graph_gl.echarts4rProxy <- function(e, layout = "force", name = NULL, rm_x = TRUE, rm_y = TRUE, ...) {
e$chart <- e_graph_gl(e$chart, layout, name, rm_x, rm_y, ...)
return(e)
}
#' @rdname graph
#' @export
e_graph_nodes <- function(e, nodes, names, value, size, category, symbol = NULL, legend = TRUE) UseMethod("e_graph_nodes")
#' @export
#' @method e_graph_nodes echarts4r
e_graph_nodes.echarts4r <- function(e, nodes, names, value, size, category, symbol = NULL, legend = TRUE) {
if (missing(nodes) || missing(names) || missing(value)) {
stop("missing arguments", call. = FALSE)
}
value <- dplyr::enquo(value)
symbolSize <- dplyr::enquo(size)
symbol <- dplyr::enquo(symbol)
names <- dplyr::enquo(names)
if (!missing(category) && !missing(size)) {
e$x$opts$series[[length(e$x$opts$series)]]$categories <- .build_graph_category(nodes, dplyr::enquo(category))
if (isTRUE(legend)) {
e$x$opts$legend$data <- append(e$x$opts$legend$data, unique(nodes[[deparse(substitute(category))]]))
}
nodes <- .build_graph_nodes(
nodes,
names,
value,
symbolSize,
dplyr::enquo(category),
symbol
)
} else if (missing(category) && !missing(size)) {
nodes <- .build_graph_nodes_no_cat(
nodes,
names,
value,
symbolSize,
symbol
)
} else if (missing(category) && missing(size)) {
nodes <- .build_graph_nodes_no_size(
nodes,
names,
value,
symbol
)
}
# build JSON data
e$x$opts$series[[length(e$x$opts$series)]]$data <- nodes
e
}
#' @export
#' @method e_graph_nodes echarts4rProxy
e_graph_nodes.echarts4rProxy <- function(e, nodes, names, value, size, category, symbol = NULL, legend = TRUE) {
if (missing(nodes) || missing(names) || missing(value)) {
stop("missing arguments", call. = FALSE)
}
value <- dplyr::enquo(value)
symbolSize <- dplyr::enquo(size)
symbol <- dplyr::enquo(symbol)
names <- dplyr::enquo(names)
if (!missing(category) && !missing(size)) {
e$chart$x$opts$series[[length(e$chart$x$opts$series)]]$categories <- .build_graph_category(nodes, dplyr::enquo(category))
if (isTRUE(legend)) {
e$chart$x$opts$legend$data <- append(e$chart$x$opts$legend$data, unique(nodes[[deparse(substitute(category))]]))
}