简体   繁体   English

R plotly 第二个 y 轴标题未与水平图例对齐

[英]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:当我将 plotly 中 1 中的 2 个图与水平图例组合时,第二个 y 轴标题(右侧)没有很好地对齐......它覆盖了刻度线,请参见:

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?是否修复了带有水平图例的 y2 标题? 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:在第二个 y 轴的定义中设置automargin = TRUE

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

广告

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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