Skip to content

Commit cc90e7e

Browse files
committed
add second axis init
1 parent c05f001 commit cc90e7e

File tree

1 file changed

+39
-11
lines changed

1 file changed

+39
-11
lines changed

R/ggplotly.R

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -562,9 +562,20 @@ gg2list <- function(p, width = NULL, height = NULL,
562562
)
563563
doms <- get_domains(nPanels, nRows, margins)
564564

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+
565573
for (i in seq_len(nPanels)) {
566574
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
568579
# find axis specific theme elements that inherit from their parent
569580
theme_el <- function(el) {
570581
theme[[paste0(el, ".", xy)]] %||% theme[[el]]
@@ -575,8 +586,12 @@ gg2list <- function(p, width = NULL, height = NULL,
575586
axisLine <- theme_el("axis.line")
576587
panelGrid <- theme_el("panel.grid.major") %||% theme_el("panel.grid")
577588
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")]
580595
anchor <- lay[, paste0(xy, "anchor")]
581596
rng <- layout$panel_params[[i]]
582597

@@ -641,7 +656,11 @@ gg2list <- function(p, width = NULL, height = NULL,
641656
# type of unit conversion
642657
type <- if (xy == "x") "height" else "width"
643658
# 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+
}
645664
if (is_blank(axisTitle)) axisTitleText <- ""
646665

647666
# is this axis dynamic?
@@ -658,17 +677,19 @@ gg2list <- function(p, width = NULL, height = NULL,
658677
isDateType <- isDynamic && isDate
659678
isDiscrete <- identical(sc$scale_name, "position_d")
660679
isDiscreteType <- isDynamic && isDiscrete
680+
y_side_options = c('left','right')
681+
rng_sec_key = if(is_secondary){'.sec'}else{''}
661682

662683
axisObj <- list(
663684
# TODO: log type?
664685
type = if (isDateType) "date" else if (isDiscreteType) "category" else "linear",
665686
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")]],
667688
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")]],
670691
categoryorder = "array",
671-
categoryarray = rng[[paste0(xy, ".labels")]],
692+
categoryarray = rng[[paste0(xy,rng_sec_key, ".labels")]],
672693
nticks = nrow(rng),
673694
ticks = if (is_blank(axisTicks)) "" else "outside",
674695
tickcolor = toRGB(axisTicks$colour),
@@ -688,9 +709,16 @@ gg2list <- function(p, width = NULL, height = NULL,
688709
zeroline = FALSE,
689710
anchor = anchor,
690711
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+
}
692718
)
693-
719+
if(is_secondary){
720+
axisObj$overlaying = 'y'
721+
}
694722
# set scaleanchor/scaleratio if these are fixed coordinates
695723
# the logic here is similar to what p$coordinates$aspect() does,
696724
# but the ratio is scaled to the data range by plotly.js
@@ -982,7 +1010,7 @@ gg2list <- function(p, width = NULL, height = NULL,
9821010
}
9831011

9841012
# 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')
9861014
hashes <- vapply(traces, function(x) digest::digest(x[names(x) %in% props]), character(1))
9871015
modes <- vapply(traces, function(x) x$mode %||% "", character(1))
9881016
nhashes <- length(unique(hashes))

0 commit comments

Comments
 (0)