-
Notifications
You must be signed in to change notification settings - Fork 4
/
tsplot.R
940 lines (840 loc) · 30.8 KB
/
tsplot.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
#' Plot Time Series
#'
#' Conveniently plot time series.
#'
#' @param ... multiple objects of class ts or a list of time series. All objects passed through the ... parameter relate to the standard left y-axis.
#' @param tsr list of time series objects of class ts.
#' @param ci list of confidence intervals for time series
#' @param left_as_bar logical should the series that relate to the left bar be drawn as (stacked) bar charts?
#' @param group_bar_chart logical should a bar chart be grouped instead of stacked?
#' @param relative_bar_chart logical Should time series be normalized such that bars range from 0 to 1? Defaults to FALSE. That way every sub bar (time series) is related to the global max. Hence do not expect every single bar to reach 1. This works for stacked and grouped charts and does not change anything but the scale of the chart.
#' @param left_as_band logical Should the time series assigned to the left axis be displayed as stacked area charts?
#' @param plot_title character title to be added to the plot
#' @param plot_subtitle character subtitle to be added to the plot
#' @param plot_subtitle_r character second subtitle to be added at the top right
#' @param find_ticks_function function to compute ticks.
#' @param overall_xlim integer overall x-axis limits, defaults to NULL.
#' @param overall_ylim integer overall y-axis limits, defaults to NULL.
#' @param manual_date_ticks character vector of manual date ticks.
#' @param manual_value_ticks_l numeric vector, forcing ticks to the left y-axis
#' @param manual_value_ticks_r numeric vector, forcing ticks to the right y-axis
#' @param manual_ticks_x numeric vector, forcing ticks on the x axis
#' @param theme list of default plot output parameters. Defaults to NULL, which leads to \code{\link{init_tsplot_theme}} being called. Please see the vignette for details about tweaking themes.
#' @param quiet logical suppress output, defaults to TRUE.
#' @param auto_legend logical should legends be printed automatically, defaults to TRUE.
#' @param output_format character Should the plot be drawn on screen or written to a file? Possible values are "plot" for screen output and "pdf". Default "plot"
#' @param filename character Path to the file to be written if \code{output_format} is "pdf". Default "tsplot.pdf"
#' @param close_graphics_device logical Should the graphics device of the output file be closed after \code{tsplot}? Set this to FALSE to be able to make modifications to the plot after \code{tsplot} finishes. Default TRUE
#'
#' @details
#' The ci parameter is a 3-level list of the form
#' list(
#' ts1 = list(
#' ci_value_1 = list(
#' ub = upper_bound_ts_object,
#' lb = lower_bound_ts_object
#' ),
#' ...
#' ),
#' ...
#' )
#'
#' See \code{vignette("tstools")} for details.
#'
#' @importFrom graphics rect axis box title mtext strheight
#' @importFrom grDevices dev.off pdf
#'
#' @export
tsplot <- function(...,
tsr = NULL,
ci = NULL,
left_as_bar = FALSE,
group_bar_chart = FALSE,
relative_bar_chart = FALSE,
left_as_band = FALSE,
plot_title = NULL,
plot_subtitle = NULL,
plot_subtitle_r = NULL,
find_ticks_function = "findTicks",
overall_xlim = NULL,
overall_ylim = NULL,
manual_date_ticks = NULL,
manual_value_ticks_l = NULL,
manual_value_ticks_r = NULL,
manual_ticks_x = NULL,
theme = NULL,
quiet = TRUE,
auto_legend = TRUE,
output_format = "plot",
filename = "tsplot",
close_graphics_device = TRUE) {
UseMethod("tsplot")
}
#' @export
tsplot.ts <- function(...,
tsr = NULL,
ci = NULL,
left_as_bar = FALSE,
group_bar_chart = FALSE,
relative_bar_chart = FALSE,
left_as_band = FALSE,
plot_title = NULL,
plot_subtitle = NULL,
plot_subtitle_r = NULL,
find_ticks_function = "findTicks",
overall_xlim = NULL,
overall_ylim = NULL,
manual_date_ticks = NULL,
manual_value_ticks_l = NULL,
manual_value_ticks_r = NULL,
manual_ticks_x = NULL,
theme = NULL,
quiet = TRUE,
auto_legend = TRUE,
output_format = "plot",
filename = "tsplot",
close_graphics_device = TRUE) {
li <- list(...)
tsplot(li,
tsr = tsr,
ci = ci,
left_as_bar = left_as_bar,
group_bar_chart = group_bar_chart,
relative_bar_chart = relative_bar_chart,
left_as_band = left_as_band,
plot_title = plot_title,
plot_subtitle = plot_subtitle,
plot_subtitle_r = plot_subtitle_r,
find_ticks_function = find_ticks_function,
manual_date_ticks = manual_date_ticks,
overall_xlim = overall_xlim,
overall_ylim = overall_ylim,
manual_value_ticks_l = manual_value_ticks_l,
manual_value_ticks_r = manual_value_ticks_r,
manual_ticks_x = manual_ticks_x,
quiet = quiet,
auto_legend = auto_legend,
theme = theme,
output_format = output_format,
filename = filename,
close_graphics_device = close_graphics_device
)
}
#' @export
tsplot.mts <- function(...,
tsr = NULL,
ci = NULL,
left_as_bar = FALSE,
group_bar_chart = FALSE,
relative_bar_chart = FALSE,
left_as_band = FALSE,
plot_title = NULL,
plot_subtitle = NULL,
plot_subtitle_r = NULL,
find_ticks_function = "findTicks",
overall_xlim = NULL,
overall_ylim = NULL,
manual_date_ticks = NULL,
manual_value_ticks_l = NULL,
manual_value_ticks_r = NULL,
manual_ticks_x = NULL,
theme = NULL,
quiet = TRUE,
auto_legend = TRUE,
output_format = "plot",
filename = "tsplot",
close_graphics_device = TRUE) {
li <- list(...)
if (length(li) > 1) {
stop("If you use multivariate time series objects (mts), make sure to pass only one object per axis. Place all time series you want to plot on one y-axis in one mts object or list of time series.")
} else {
data <- li[[1]]
if (nrow(data) == 1) {
warning("mts contains only a single row! This means it contains multiple time series of length 1, did you
create a ts out of a row of a data.frame? Converting to single ts.")
data <- ts(data[1, ], start = start(data), frequency = frequency(data))
}
tsplot(as.list(data),
tsr = tsr,
ci = ci,
left_as_bar = left_as_bar,
group_bar_chart = group_bar_chart,
relative_bar_chart = relative_bar_chart,
left_as_band = left_as_band,
plot_title = plot_title,
plot_subtitle = plot_subtitle,
plot_subtitle_r = plot_subtitle_r,
find_ticks_function = find_ticks_function,
overall_xlim = overall_xlim,
overall_ylim = overall_ylim,
manual_date_ticks = manual_date_ticks,
manual_value_ticks_l = manual_value_ticks_l,
manual_value_ticks_r = manual_value_ticks_r,
manual_ticks_x = manual_ticks_x,
quiet = quiet,
auto_legend = auto_legend,
theme = theme,
output_format = output_format,
filename = filename,
close_graphics_device = close_graphics_device
)
}
}
#' @export
tsplot.zoo <- function(...,
tsr = NULL,
ci = NULL,
left_as_bar = FALSE,
group_bar_chart = FALSE,
relative_bar_chart = FALSE,
left_as_band = FALSE,
plot_title = NULL,
plot_subtitle = NULL,
plot_subtitle_r = NULL,
find_ticks_function = "findTicks",
overall_xlim = NULL,
overall_ylim = NULL,
manual_date_ticks = NULL,
manual_value_ticks_l = NULL,
manual_value_ticks_r = NULL,
manual_ticks_x = NULL,
theme = NULL,
quiet = TRUE,
auto_legend = TRUE,
output_format = "plot",
filename = "tsplot",
close_graphics_device = TRUE) {
stop("zoo objets are not supported yet. Please convert your data to ts!")
}
#' @export
tsplot.xts <- function(...,
tsr = NULL,
ci = NULL,
left_as_bar = FALSE,
group_bar_chart = FALSE,
relative_bar_chart = FALSE,
left_as_band = FALSE,
plot_title = NULL,
plot_subtitle = NULL,
plot_subtitle_r = NULL,
find_ticks_function = "findTicks",
overall_xlim = NULL,
overall_ylim = NULL,
manual_date_ticks = NULL,
manual_value_ticks_l = NULL,
manual_value_ticks_r = NULL,
manual_ticks_x = NULL,
theme = NULL,
quiet = TRUE,
auto_legend = TRUE,
output_format = "plot",
filename = "tsplot",
close_graphics_device = TRUE) {
stop("xts objects are not supported yet. Please convert your data to ts if possible!")
}
#' @export
tsplot.list <- function(...,
tsr = NULL,
ci = NULL,
left_as_bar = FALSE,
group_bar_chart = FALSE,
relative_bar_chart = FALSE,
left_as_band = FALSE,
plot_title = NULL,
plot_subtitle = NULL,
plot_subtitle_r = NULL,
find_ticks_function = "findTicks",
overall_xlim = NULL,
overall_ylim = NULL,
manual_date_ticks = NULL,
manual_value_ticks_l = NULL,
manual_value_ticks_r = NULL,
manual_ticks_x = NULL,
theme = NULL,
quiet = TRUE,
auto_legend = TRUE,
output_format = "plot",
filename = "tsplot",
close_graphics_device = TRUE) {
tsl <- c(...)
if (inherits(tsr, "ts")) {
tsr <- list(tsr)
}
class_l <- sapply(tsl, "class")
non_ts_l <- class_l != "ts"
if (any(non_ts_l)) {
warning(
sprintf(
"Ignoring non-ts objects in list: %s\nCheck if those belong in the theme!",
paste(names(class_l[non_ts_l]), collapse = ", ")
)
)
tsl <- tsl[!non_ts_l]
}
tsl_lengths <- sapply(tsl, length)
if (any(tsl_lengths == 1) && !left_as_bar) {
warning("tsl contains series of length 1! Omitting those.")
tsl <- tsl[tsl_lengths > 1]
if (length(tsl) == 0) {
stop("No series with length greater 1 left, stopping!")
}
}
if (!is.null(tsr)) {
tsr_lengths <- sapply(tsr, length)
if (any(tsr_lengths == 1)) {
warning("tsr contains series of length 1! omitting those.")
tsr <- tsr[tsr_lengths > 1]
if (length(tsr) == 0) {
tsr <- NULL
}
}
}
# Sanity check for band plots
if (left_as_band) {
all_signs <- sign(unlist(tsl))
signs_consistent <- (any(all_signs < 0) & all(all_signs <= 0)) | (any(all_signs > 0) & all(all_signs >= 0))
if (!signs_consistent) {
warning("Found both positive and negative contributions in tsl!\nAre you sure a band plot is what you want?")
}
}
if (is.null(theme)) {
if (output_format != "plot") {
theme <- init_tsplot_print_theme()
} else {
theme <- init_tsplot_theme()
}
}
# Expand per-line parameters for recycling
total_n_ts <- length(tsl) + length(tsr)
expand_param <- function(theme, param_name) {
rep(theme[[param_name]], ceiling(total_n_ts / length(theme[[param_name]])))
}
theme$line_colors <- expand_param(theme, "line_colors")
theme$lwd <- expand_param(theme, "lwd")
theme$lty <- expand_param(theme, "lty")
theme$show_points <- expand_param(theme, "show_points")
theme$point_symbol <- expand_param(theme, "point_symbol")
theme$NA_continue_line <- expand_param(theme, "NA_continue_line")
theme$ci_colors <- expand_param(theme, "ci_colors")
# OPEN CORRECT GRAPHICS DEVICE
if (output_format != "plot") {
if (!grepl(sprintf("[.]%s$", output_format), filename)) {
filename <- sprintf("%s.%s", filename, output_format)
}
output_dim <- `if`(theme$output_wide, c(10 + 2 / 3, 6), c(8, 6))
if (output_format == "pdf") {
pdf(filename, width = output_dim[1], height = output_dim[2])
}
# } else if(output_format == "bmp") {
# bmp(filename, width = output_dim[1], height = output_dim[2], units = "in", res = theme$resolution)
# } else if(output_format == "jpeg" || output_format == "jpg") {
# jpeg(filename, width = output_dim[1], height = output_dim[2], units = "in", res = theme$resolution, quality = theme$jpeg_quality)
# } else if(output_format == "png") {
# png(filename, width = output_dim[1], height = output_dim[2], units = "in", res = theme$resolution)
# } else if(output_format == "tiff") {
#
# }
if (close_graphics_device) {
on.exit(dev.off())
}
}
# Set pointsize and mex pars
par(
ps = theme$pointsize,
mex = ifelse(output_format == "plot", 1, scale_theme_param_for_print(1, dev.size())),
lwd = ifelse(output_format == "plot", 1, scale_theme_param_for_print(1, dev.size()))
)
if (left_as_bar && relative_bar_chart) {
# Normalize ts
if (group_bar_chart) {
m <- Reduce("max", tsl)
} else {
sums <- Reduce("+", tsl)
m <- max(sums)
}
tsl <- lapply(tsl, "/", m)
}
# Set default names for legend if none provided (moved here for measuring margin)
right_name_start <- 0
if (is.null(names(tsl))) {
names(tsl) <- paste0("series_", 1:length(tsl))
right_name_start <- length(tsl)
}
if (is.null(names(tsr)) & !is.null(tsr)) {
if (is.list(tsr)) {
names(tsr) <- paste0("series_", 1:length(tsr) + right_name_start)
} else {
tsr <- list(tsr)
names(tsr) <- paste0("series_", right_name_start + 1)
}
}
margins <- theme$margins
if (is.na(margins[1])) {
if (theme$auto_bottom_margin || auto_legend) {
length_l <- length(tsl)
length_r <- length(tsr)
names_l <- names(tsl)
names_r <- names(tsr)
n_ci_l <- `if`(any(names_l %in% names(ci)), sum(sapply(ci[names_l], length)), 0)
n_ci_r <- `if`(any(names_r %in% names(ci)), sum(sapply(ci[names_r], length)), 0)
n_newline_ci <- lengths(regmatches(theme$ci_legend_label, gregexpr("\n", theme$ci_legend_label)))
n_newline_l <- max(lengths(regmatches(names_l, gregexpr("\n", names_l))))
n_newline_r <- `if`(is.null(tsr), 0, max(lengths(regmatches(names_r, gregexpr("\n", names_r)))))
if (n_ci_l > 0) {
n_newline_l <- max(n_newline_ci, n_newline_l)
}
if (n_ci_r > 0) {
n_newline_r <- max(n_newline_ci, n_newline_r)
}
n_legends_l_r <- c(
# Add length_x*n_legend_x since the height of all legend entries is determined by the tallest one
length_l + n_ci_l + (left_as_bar && theme$sum_as_line) + (length_l + n_ci_l) * n_newline_l,
length_r + n_ci_r + (length_r + n_ci_r) * n_newline_r
)
bigger_legend <- 1
if (theme$legend_all_left) {
n_legends <- sum(n_legends_l_r)
} else {
n_legends <- max(n_legends_l_r)
bigger_legend <- which.max(n_legends_l_r)
}
n_legend_lines <- ceiling(n_legends / theme$legend_col)
n_legend_entries <- `if`(bigger_legend == 1, length_l + n_ci_l, length_r + n_ci_r)
# strheight only really considers the number of newlines in the text to be measured
legend_height_in_in <- strheight(
paste(rep("\n", n_legend_lines - 1), collapse = ""),
units = "inches",
cex = theme$legend_font_size
) +
# space between legends
(n_legend_entries - 1) * theme$legend_font_size * (theme$legend_intersp_y - 1) * par("cin")[2]
single_line_height_in_in <- strheight("", units = "inches", cex = par("cex"))
# Add the height of a single line to account for the x ticks (more or less)
margins[1] <- 100 * (single_line_height_in_in + legend_height_in_in) / dev.size()[2] + theme$legend_margin_top + theme$legend_margin_bottom
} else {
margins[1] <- theme$default_bottom_margin
}
}
margins[c(1, 3)] <- margins[c(1, 3)] * dev.size()[2] / 100
margins[c(2, 4)] <- margins[c(2, 4)] * dev.size()[1] / 100
par(mai = margins)
cnames <- names(tsl)
# if(!is.null(tsr)) cnames <- names(tsr)
if (left_as_bar || left_as_band) {
# Combine ts
tsmat <- do.call("cbind", tsl)
if (!is.null(dim(tsmat)) && dim(tsmat)[2] > 1) {
# Set all NAs to 0 so range() works properly
tsmat[is.na(tsmat)] <- 0
ranges <- apply(tsmat, 1, function(r) {
if (group_bar_chart && !left_as_band) {
range(r)
} else {
range(c(sum(r[r < 0]), sum(r[r >= 0])))
}
})
tsl_r <- c(min(ranges[1, ]), max(ranges[2, ]))
} else {
# tsmat is still a single ts
tsl_r <- range(tsmat)
}
# Ensure 0 is part of the range when plotting bars
tsl_r[1] <- min(tsl_r[1], 0)
tsl_r[2] <- max(0, tsl_r[2])
} else {
# Determine range of tsl plus any potential confidence bands
tsl_r <- range(as.numeric(unlist(c(tsl, ci[names(tsl)]))), na.rm = TRUE)
}
if (!is.null(theme$y_range_min_size)) {
# Restrict y range to at least theme$y_range_min_size
tsl_r_size <- diff(tsl_r)
if (tsl_r_size < theme$y_range_min_size) {
tsl_r_mid <- 0.5 * tsl_r_size + tsl_r[1]
half_min_range_size <- 0.5 * theme$y_range_min_size
tsl_r <- c(tsl_r_mid - half_min_range_size, tsl_r_mid + half_min_range_size)
}
} else if (tsl_r[1] == tsl_r[2]) {
level <- tsl_r[1]
offset <- level %% 10
tsl_r <- c(level - 10 - offset, level + 10 - offset)
}
if (!is.null(tsr)) {
tsr <- sanitizeTsr(tsr)
tsr_r <- range(unlist(c(tsr, ci[names(tsr)])), na.rm = TRUE)
if (!is.null(theme$y_range_min_size)) {
tsr_r_size <- diff(tsr_r)
if (tsr_r_size < theme$y_range_min_size) {
tsr_r_mid <- 0.5 * tsr_r_size + tsr_r[1]
half_min_range_size <- 0.5 * theme$y_range_min_size
tsr_r <- c(tsr_r_mid - half_min_range_size, tsr_r_mid + half_min_range_size)
}
} else if (tsr_r[1] == tsr_r[2]) {
level <- tsr_r[1]
offset <- level %% 10
tsr_r <- c(level - 10 - offset, level + 10 - offset)
}
}
# CANVAS OPTIONS START #########################################
# so far manual date ticks are ignored.
global_x <- getGlobalXInfo(tsl, tsr, fill_up = theme$fill_year_with_nas, fill_up_start = theme$fill_up_start, theme$x_tick_dt, manual_ticks_x)
# y can't be global in the first place, cause
# tsr and tsl have different scales....
# time series left
if (!is.null(manual_value_ticks_l)) {
left_ticks <- manual_value_ticks_l
left_y <- list(
y_range = range(manual_value_ticks_l),
y_ticks = manual_value_ticks_l
)
} else {
left_ticks <- do.call(find_ticks_function, list(tsl_r, theme$y_grid_count, theme$preferred_y_gap_sizes, theme$y_tick_force_integers, theme$range_must_not_cross_zero))
left_y <- list(y_range = range(left_ticks), y_ticks = left_ticks)
# return("Only works with manual value ticks...")
}
# time series right
if (!is.null(tsr)) {
if (!is.null(manual_value_ticks_r)) {
if (length(manual_value_ticks_r) != length(left_y$y_ticks)) {
return("When using to manual tick position vectors, both need to be of same length! (Otherwise grids look ugly)")
}
right_ticks <- manual_value_ticks_r
right_y <- list(
y_range = range(manual_value_ticks_r),
y_ticks = manual_value_ticks_r
)
} else {
right_ticks <- do.call(find_ticks_function, list(tsr_r, length(left_ticks), theme$preferred_y_gap_sizes, theme$y_tick_force_integers, theme$range_must_not_cross_zero))
right_y <- list(y_range = range(right_ticks), y_ticks = right_ticks)
}
} else {
# define right_ticks anyway to avoid having to check for it further down
right_ticks <- 1
}
if (!theme$y_grid_count_strict && is.null(manual_value_ticks_l) && is.null(manual_value_ticks_r)) {
left_diff <- diff(left_ticks)
left_d <- left_diff[1]
left_ub <- left_ticks[length(left_ticks)]
left_lb <- left_ticks[1]
if (!is.null(tsr)) {
right_diff <- diff(right_ticks)
right_d <- right_diff[1]
right_ub <- right_ticks[length(left_ticks)]
right_lb <- right_ticks[1]
}
left_needs_extra_tick_top <- tsl_r[2] > left_ub - left_d * theme$y_tick_margin
if (left_needs_extra_tick_top) {
left_ticks <- c(left_ticks, left_ub + left_d)
if (!is.null(tsr)) {
right_ticks <- c(right_ticks, right_ub + right_d)
right_ub <- right_ub + right_d
}
}
left_needs_exta_tick_bottom <- tsl_r[1] < left_lb + left_d * theme$y_tick_margin
if (left_needs_exta_tick_bottom) {
left_ticks <- c(left_lb - left_d, left_ticks)
if (!is.null(tsr)) {
right_ticks <- c(right_lb - right_d, right_ticks)
right_lb <- right_lb - right_d
}
}
if (!is.null(tsr)) {
right_needs_extra_tick_top <- tsr_r[2] > right_ub - right_d * theme$y_tick_margin
if (right_needs_extra_tick_top) {
left_ticks <- c(left_ticks, left_ub + left_d)
right_ticks <- c(right_ticks, right_ub + right_d)
}
right_needs_extra_tick_bottom <- tsr_r[1] < right_lb + right_d * theme$y_tick_margin
if (right_needs_extra_tick_bottom) {
left_ticks <- c(left_lb - left_d, left_ticks)
right_ticks <- c(right_lb - right_d, right_ticks)
}
}
# Technically we could save ourselves all that correcting if manual ticks are not null.
# This is just a convenient place to check.
left_sign_ok <- (
sign(left_ticks[1]) == sign(left_y$y_ticks[1]) || sign(left_ticks[1]) == 0
) && (
sign(max(left_ticks)) == sign(max(left_y$y_ticks)) || sign(max(left_ticks)) == 0
)
right_sign_ok <-
is.null(tsr) || (
(
sign(right_ticks[1]) == sign(right_y$y_ticks[1]) || sign(right_ticks[1]) == 0
) && (
sign(max(right_ticks)) == sign(max(right_y$y_ticks)) || sign(max(right_ticks)) == 0
)
)
# Only touch ticks if both sides are ok
if (!theme$range_must_not_cross_zero || (left_sign_ok && right_sign_ok)) {
left_y <- list(y_range = range(left_ticks), y_ticks = left_ticks)
if (!is.null(tsr)) {
right_y <- list(y_range = range(right_ticks), y_ticks = right_ticks)
}
}
}
# CANVAS OPTIONS END #########################################
# BASE CANVAS
plot(NULL,
xlim = global_x$x_range,
ylim = left_y$y_range,
axes = F,
xlab = "",
ylab = "",
xaxs = theme$xaxs,
yaxs = theme$yaxs
)
if (theme$highlight_window) {
hlw_start <- theme$highlight_window_start
if (!any(is.na(hlw_start))) {
if (!is.list(hlw_start)) {
hlw_start <- list(hlw_start)
}
xl <- sapply(hlw_start, compute_decimal_time, theme$highlight_window_freq)
} else {
xl <- global_x$x_range[2] - 2
}
hlw_end <- theme$highlight_window_end
if (!any(is.na(hlw_end))) {
if (!is.list(hlw_end)) {
hlw_end <- list(hlw_end)
}
xr <- sapply(hlw_end, compute_decimal_time, theme$highlight_window_freq) + 1 / theme$highlight_window_freq
} else {
xr <- global_x$x_range[2]
}
n_start <- length(xl)
n_end <- length(xr)
if (n_start != n_end) {
warning(sprintf("%s highlight start points than end points specified! Dropping excess ones.", ifelse(n_start > n_end, "More", "Fewer")))
}
for (i in seq_along(xl)) {
rect(xl[i], left_y$y_range[1], xr[i], left_y$y_range[2],
col = theme$highlight_color,
border = NA
)
}
}
if (theme$show_y_grids) {
addYGrids(left_y$y_ticks, global_x$x_range, theme = theme)
}
if (!all(is.na(theme$highlight_y_values))) {
addYHighlights(global_x$x_range, theme)
}
# Split theme into left/right
tt_r <- theme
# Make sure we do not reuse line specs for the right axis (if left is not bars)
if (!(left_as_bar || left_as_band)) {
total_le <- length(tsl) + length(tsr)
start_r <- (total_le - (length(tsr) - 1)):total_le
tt_r$line_colors <- tt_r$line_colors[start_r]
tt_r$lwd <- tt_r$lwd[start_r]
tt_r$lty <- tt_r$lty[start_r]
tt_r$show_points <- tt_r$show_points[start_r]
tt_r$point_symbol <- tt_r$point_symbol[start_r]
tt_r$NA_continue_line <- tt_r$NA_continue_line[start_r]
tt_r$ci_colors <- tt_r$ci_colors[start_r]
}
# Draw all confidence bands here (so they don't overlap lines later)
# Or should they be drawn left first, then right as before? cf especially with left_as_bar == TRUE and
# CI somewhere in the middle of the series. How common a case is that though?
if (!left_as_bar) {
par(new = TRUE)
plot(NULL,
xlim = global_x$x_range,
ylim = left_y$y_range,
axes = F,
xlab = "",
ylab = "",
xaxs = theme$xaxs,
yaxs = theme$yaxs
)
ci_left <- ci[names(ci) %in% names(tsl)]
draw_ts_ci(ci_left, theme)
}
if (!is.null(tsr)) {
par(new = TRUE)
plot(NULL,
xlim = global_x$x_range,
ylim = right_y$y_range,
axes = F,
xlab = "",
ylab = "",
yaxs = theme$yaxs,
xaxs = theme$xaxs
)
ci_right <- ci[names(ci) %in% names(tsr)]
draw_ts_ci(ci_right, tt_r)
}
par(new = TRUE)
plot(NULL,
xlim = global_x$x_range,
ylim = left_y$y_range,
axes = F,
xlab = "",
ylab = "",
xaxs = theme$xaxs,
yaxs = theme$yaxs
)
if (left_as_bar) {
# draw barplot
draw_ts_bars(tsl,
group_bar_chart = group_bar_chart,
theme = theme
)
if (theme$sum_as_line) {
reduced <- Reduce("+", tsl)
draw_sum_as_line(reduced, theme)
}
} else {
# draw lineplot
draw_ts_lines(tsl, theme = theme, bandplot = left_as_band)
}
# RIGHT PLOT #######################
if (!is.null(tsr)) {
par(new = T)
plot(NULL,
xlim = global_x$x_range,
ylim = right_y$y_range,
axes = F,
xlab = "",
ylab = "",
yaxs = theme$yaxs,
xaxs = theme$xaxs
)
draw_ts_lines(tsr, theme = tt_r)
# RIGHT Y-Axis
if (theme$show_right_y_axis) {
axis(4, right_y$y_ticks,
las = theme$y_las,
lwd = theme$lwd_y_axis,
lwd.ticks = theme$lwd_y_ticks, tcl = theme$tcl_y_ticks
)
}
}
# DRAW AXES
par(new = TRUE)
plot(NULL,
xlim = global_x$x_range,
ylim = left_y$y_range,
axes = F,
xlab = "",
ylab = "",
xaxs = theme$xaxs,
yaxs = theme$yaxs
)
# Global X-Axis ###################
if (theme$show_x_axis) {
if (theme$yearly_ticks) {
if (theme$label_pos == "start" || theme$x_tick_dt != 1 || !is.null(manual_ticks_x)) {
axis(1, global_x$yearly_tick_pos,
labels = global_x$yearly_tick_pos,
lwd = theme$lwd_x_axis,
lwd.ticks = theme$lwd_yearly_ticks,
tcl = theme$tcl_yearly_tick,
padj = 0
)
} else {
axis(1, global_x$yearly_tick_pos,
labels = F,
lwd = theme$lwd_x_axis,
lwd.ticks = theme$lwd_yearly_ticks,
tcl = theme$tcl_yearly_tick
)
}
}
if (theme$quarterly_ticks && theme$x_tick_dt == 1 && is.null(manual_ticks_x)) {
overlap <- global_x$quarterly_tick_pos %in% global_x$yearly_tick_pos
q_ticks <- global_x$quarterly_tick_pos[!overlap]
q_labels <- global_x$year_labels_middle_q[!overlap]
if (theme$label_pos == "mid") {
axis(1, q_ticks,
labels = q_labels,
lwd = theme$lwd_x_axis,
lwd.ticks = theme$lwd_quarterly_ticks,
tcl = theme$tcl_quarterly_ticks,
padj = 0
)
} else {
axis(1, q_ticks,
labels = F,
lwd = theme$lwd_x_axis,
lwd.ticks = theme$lwd_quarterly_ticks,
tcl = theme$tcl_quarterly_ticks
)
}
}
}
# LEFT Y-AXIS
if (theme$show_left_y_axis) {
axis(2, left_y$y_ticks,
las = theme$y_las,
lwd = theme$lwd_y_axis,
lwd.ticks = theme$lwd_y_ticks, tcl = theme$tcl_y_ticks
)
}
if (!is.null(tsr)) {
par(new = T)
plot(NULL,
xlim = global_x$x_range,
ylim = right_y$y_range,
axes = F,
xlab = "",
ylab = "",
yaxs = theme$yaxs,
xaxs = theme$xaxs
)
# RIGHT Y-Axis
if (theme$show_right_y_axis) {
axis(4, right_y$y_ticks,
las = theme$y_las,
lwd = theme$lwd_y_axis,
lwd.ticks = theme$lwd_y_ticks, tcl = theme$tcl_y_ticks
)
}
}
# RESET USER COORDINATES TO LEFT SIDE
par(new = TRUE)
plot(NULL,
xlim = global_x$x_range,
ylim = left_y$y_range,
axes = F,
xlab = "",
ylab = "",
xaxs = theme$xaxs,
yaxs = theme$yaxs
)
if (theme$use_box) {
box(lwd = theme$lwd_box)
}
# add legend
if (auto_legend) {
ci_names <- lapply(names(ci), function(x) {
y <- gsub("%series%", x, theme$ci_legend_label)
if (grepl("%ci_value%", y)) {
parts <- strsplit(y, "%ci_value%")[[1]]
# in case %ci_value% is at the very end (see ?split)
if (length(parts) == 1) {
parts <- c(parts, "")
}
y <- paste0(parts[1], names(ci[[x]]), parts[2])
} else {
y <- rep(y, length(ci[[x]]))
}
y
})
names(ci_names) <- names(ci)
add_legend(names(tsl), names(tsr), ci_names,
theme = theme, left_as_bar = left_as_bar,
left_as_band = left_as_band
)
}
# add title and subtitle
add_title(plot_title, plot_subtitle, plot_subtitle_r, theme)
# return axes and tick info, as well as theme maybe?
if (!quiet) {
output <- list(
left_range = tsl_r,
right_range = tsr_r
)
}
}