@@ -562,9 +562,20 @@ gg2list <- function(p, width = NULL, height = NULL,
562
562
)
563
563
doms <- get_domains(nPanels , nRows , margins )
564
564
565
+ # check if secondary axis
566
+ y_attr = attributes(scale_y()$ secondary.axis )
567
+ if (' AxisSecondary' %in% y_attr $ class ){
568
+ layout_axes = c(' x' ,' y' ,' y2' )
569
+ }else {
570
+ layout_axes = c(' x' ,' y' )
571
+ }
572
+
565
573
for (i in seq_len(nPanels )) {
566
574
lay <- layout $ layout [i , ]
567
- for (xy in c(" x" , " y" )) {
575
+ for (la in layout_axes ) {
576
+ # find axis specific theme elements that inherit from their parent
577
+ xy = substr(la ,0 ,1 )
578
+ is_secondary = nchar(la )== 2
568
579
# find axis specific theme elements that inherit from their parent
569
580
theme_el <- function (el ) {
570
581
theme [[paste0(el , " ." , xy )]] %|| % theme [[el ]]
@@ -575,8 +586,12 @@ gg2list <- function(p, width = NULL, height = NULL,
575
586
axisLine <- theme_el(" axis.line" )
576
587
panelGrid <- theme_el(" panel.grid.major" ) %|| % theme_el(" panel.grid" )
577
588
stripText <- theme_el(" strip.text" )
578
-
579
- axisName <- lay [, paste0(xy , " axis" )]
589
+ axisName <- if (is_secondary ){
590
+ paste0(lay [, paste0(xy , " axis" )],' 2' )
591
+ }else {
592
+ lay [, paste0(xy , " axis" )]
593
+ }
594
+ # axisName = lay[, paste0(xy, "axis")]
580
595
anchor <- lay [, paste0(xy , " anchor" )]
581
596
rng <- layout $ panel_params [[i ]]
582
597
@@ -641,7 +656,11 @@ gg2list <- function(p, width = NULL, height = NULL,
641
656
# type of unit conversion
642
657
type <- if (xy == " x" ) " height" else " width"
643
658
# get axis title
644
- axisTitleText <- sc $ name %|| % plot $ labels [[xy ]] %|| % " "
659
+ axisTitleText <- if (is_secondary ){
660
+ sc $ secondary.axis $ name %|| % plot $ labels [[la ]] %|| % " "
661
+ }else {
662
+ sc $ name %|| % plot $ labels [[xy ]] %|| % " "
663
+ }
645
664
if (is_blank(axisTitle )) axisTitleText <- " "
646
665
647
666
# is this axis dynamic?
@@ -658,17 +677,19 @@ gg2list <- function(p, width = NULL, height = NULL,
658
677
isDateType <- isDynamic && isDate
659
678
isDiscrete <- identical(sc $ scale_name , " position_d" )
660
679
isDiscreteType <- isDynamic && isDiscrete
680
+ y_side_options = c(' left' ,' right' )
681
+ rng_sec_key = if (is_secondary ){' .sec' }else {' ' }
661
682
662
683
axisObj <- list (
663
684
# TODO: log type?
664
685
type = if (isDateType ) " date" else if (isDiscreteType ) " category" else " linear" ,
665
686
autorange = isDynamic ,
666
- range = rng [[paste0(xy , " .range" )]] %|| % rng [[paste0(xy , " _range" )]],
687
+ range = rng [[paste0(xy ,rng_sec_key , " .range" )]] %|| % rng [[paste0(xy , " _range" )]],
667
688
tickmode = if (isDynamic ) " auto" else " array" ,
668
- ticktext = rng [[paste0(xy , " .labels" )]],
669
- tickvals = rng [[paste0(xy , " .major" )]],
689
+ ticktext = rng [[paste0(xy ,rng_sec_key , " .labels" )]],
690
+ tickvals = rng [[paste0(xy ,rng_sec_key , " .major" )]],
670
691
categoryorder = " array" ,
671
- categoryarray = rng [[paste0(xy , " .labels" )]],
692
+ categoryarray = rng [[paste0(xy ,rng_sec_key , " .labels" )]],
672
693
nticks = nrow(rng ),
673
694
ticks = if (is_blank(axisTicks )) " " else " outside" ,
674
695
tickcolor = toRGB(axisTicks $ colour ),
@@ -688,9 +709,16 @@ gg2list <- function(p, width = NULL, height = NULL,
688
709
zeroline = FALSE ,
689
710
anchor = anchor ,
690
711
title = faced(axisTitleText , axisTitle $ face ),
691
- titlefont = text2font(axisTitle )
712
+ titlefont = text2font(axisTitle ),
713
+ side = if (is_secondary ){
714
+ y_side_options [y_side_options != sc $ position ]
715
+ }else {
716
+ sc $ position
717
+ }
692
718
)
693
-
719
+ if (is_secondary ){
720
+ axisObj $ overlaying = ' y'
721
+ }
694
722
# set scaleanchor/scaleratio if these are fixed coordinates
695
723
# the logic here is similar to what p$coordinates$aspect() does,
696
724
# but the ratio is scaled to the data range by plotly.js
@@ -982,7 +1010,7 @@ gg2list <- function(p, width = NULL, height = NULL,
982
1010
}
983
1011
984
1012
# try to merge marker/line traces that have the same values for these props
985
- props <- c(" x" , " y" , " text" , " type" , " xaxis" , " yaxis" , " name" )
1013
+ props <- c(" x" , " y" , " text" , " type" , " xaxis" , " yaxis" , " name" , ' yaxis2 ' )
986
1014
hashes <- vapply(traces , function (x ) digest :: digest(x [names(x ) %in% props ]), character (1 ))
987
1015
modes <- vapply(traces , function (x ) x $ mode %|| % " " , character (1 ))
988
1016
nhashes <- length(unique(hashes ))
0 commit comments