Skip to content

Plotly - automatically style multiple axes #95

@eroten

Description

@eroten

Implement something like this in council_layout()

  n_y_axes <- names(a_plotly$x$layout) %>% 
    .[stringr::str_detect(., "yaxis")] %>%
    stringr::str_remove("yaxis") %>% 
    as.numeric() %>%
    max(na.rm = TRUE)
  
  
  n_x_axes <- names(a_plotly$x$layout) %>% 
    .[stringr::str_detect(., "xaxis")] %>%
    stringr::str_remove("xaxis") %>% 
    as.numeric() %>%
    max(na.rm = TRUE)

 p <- a_plotly %>%
    plotly::layout(
      margin = plotly_margin,
      barmode = "group",
      bargap = 0.1,
      ......

  # multi-axis adjustments ------
  if(sum(n_x_axes, n_y_axes) > 1){
    # fix annotations
    purrr::map(
      c(1:length(plotly_format$x$layout$annotations)),
      function(x){
        plotly_format$x$layout$annotations[[x]]$font <<- list(
          family = "Arial Narrow",
          size = 18
        )
      }
    )
    
    # fix all x axes
    
    purrr::map(
      paste0("xaxis", c(1:n_x_axes)),
      function(which_ax){
        plotly_format$x$layout[[which_ax]]$tickfont <<- list(
          family = "Arial Narrow",
          size = 14
        )
      }
    )
    
    
    # fix all y axes
    purrr::map(
      paste0("yaxis", c(1:n_y_axes)),
      function(which_ax){
        plotly_format$x$layout[[which_ax]]$tickfont <<- list(
          family = "Arial Narrow",
          size = 14
        )
      }
    )
    
    # determine the number of rows by dividing the number of plots by 3
    # rounding UP when not round number
    n_facet_rows <- ceiling(n_y_axes/3)
    # determine the middle-most row
    # if 4 rows, middle row is 2nd
    # if 5 rows, middle row is 3rd
    # if 6 rows, middle row is 3rd
    # if 7 rows, middle row is 4th
    middle_row <- mean(c(1, n_facet_rows)) %>% floor()
    
    label_which_y <- case_when(middle_row == 1 ~ 2,  
                               middle_row == 2 ~ 4,
                               middle_row == 3 ~ 7,
                               middle_row == 4 ~ 10,
                               middle_row == 5 ~ 13)
    
    
    plotly_format$x$layout[[paste0("yaxis", label_which_y)]]$title <- 
      list(
        text = y_title,
        font = list(
          family = "Arial Narrow",
          size = 20
        )
      )
    
    label_which_x <- case_when(n_x_axes %% 3 == 0 ~ n_x_axes - 1,
                               TRUE ~ n_x_axes)
    
    plotly_format$x$layout[[paste0("xaxis",label_which_x)]]$title <- 
      list(
        text = x_title,
        font = list(
          family = "Arial Narrow",
          size = 20
        )
      )
    
    plotly_format$x$layout$xaxis$title <- ""
    plotly_format$x$layout$yaxis$title <- ""
    
    plotly_format$x$layoutAttrs[[1]]$xaxis$title <- ""
    plotly_format$x$layoutAttrs[[1]]$yaxis$title <- ""
  }
  

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions