【问题标题】:R plotly second y axis title not aligned with horizontal legendR plotly second y axis title not aligned with horizontal legend
【发布时间】:2022-12-27 15:51:07
【问题描述】:

When I combine 2 plots in 1 in plotly with horizontal legend the 2nd y-axis title (right-side) is not well aligned... it covers the ticks see:

x1 <- rnorm(50)
x2 <- rnorm(10)

fit <- density(x1, na.rm = T)

fig <- plot_ly() 

fig <- fig %>% 
  
  add_histogram(x = ~x1, name = "X1", marker = list(color = "red")
  ) %>%
  
  add_histogram(x = ~x2, name = "X2", marker = list(color ="#blue")
  ) %>% 
  
  add_lines(x = fit$x, y = fit$y, yaxis = "y2", name = "Density", line = list(color = "#33228875", inherit = F)
   )%>%
  
  layout(yaxis2 = list(title = 'testesttesttesttesttesttesttesttest',
                       overlaying = "y", 
                       side = "right", 
                       rangemode = "tozero"),
                       legend = list(orientation = "h")
  )

With normal right aligned legend it goes well

Any fix for the y2 title with horizontal legend? I thought about adding a small white picture to the right outside the paper-area but obviously doesn't make any sense

【问题讨论】:

    标签: r plotly


    【解决方案1】:

    Set automargin = TRUE in the definition of the second y-axis:

    fig <- plot_ly()  %>% 
      add_histogram(x = ~x1, name = "X1", marker = list(color = "red")) %>%
      add_histogram(x = ~x2, name = "X2", marker = list(color ="#blue")) %>% 
      add_lines(x = fit$x, y = fit$y, yaxis = "y2", name = "Density",
                line = list(color = "#33228875", inherit = FALSE)) %>%
      layout(yaxis2 = list(title = 'testesttesttesttesttesttesttesttest',
                           overlaying = "y",
                           automargin = TRUE,
                           side = "right",
                           rangemode = "tozero"),
            legend = list(orientation = "h"))
    

    【讨论】:

    • thank you SO MUCH! Worked!
    猜你喜欢
    • 2020-10-18
    • 2012-11-21
    • 2015-11-06
    • 2022-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-25
    • 1970-01-01
    相关资源
    最近更新 更多