简体   繁体   中英

R plotly second y axis title not aligned with horizontal legend

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

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"))

广告

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM