2017-08-17 171 views
2

我在R中创建了一个在同一侧有多个y坐标轴的绘图。然而,额外的轴重叠在图上,并且当图更嘈杂时会引起问题。用多个坐标轴绘制

这是我有:

enter image description here

虽然我需要的是这样的:

enter image description here

示例代码:

library(plotly) 
ay <- list(
    tickfont = list(color = "red"), 
    overlaying = "y", 
    side = "left", 
    title = "second y axis", 
    position = 0.1 
) 
p <- plot_ly() %>% 
    add_lines(x = ~1:3, y = ~10*(1:3), name = "slope of 10") %>% 
    add_lines(x = ~2:4, y = ~1:3, name = "slope of 1", yaxis = "y2") %>% 
    layout(
    title = "Double Y Axis", yaxis2 = ay, 
    xaxis = list(title="x") 
) 

p 

回答

1

据我所知,多个y轴不相关在R中用plotly进行管理,并且可用于规避上述问题的唯一技巧是调谐margin属性,如提议的here

library(plotly) 
ay <- list(
    tickfont = list(color = "red"), 
    overlaying = "y", 
    side = "left", 
    title = "second y axis", 
    anchor="free" 
) 

p <- plot_ly() %>% 
    add_lines(x = ~1:3, y = ~10*(1:3), name = "slope of 10") %>% 
    add_lines(x = ~2:4, y = ~1:3, name = "slope of 1", yaxis = "y2") %>% 
    layout(
    title = "Double Y Axis", yaxis2 = ay, 
    xaxis = list(title="x"), 
    yaxis = list(showline = FALSE, side="left"), 
    margin=list(pad = 50, b = 90, l = 150, r = 90) 
) 
p 

enter image description here

+0

谢谢!这对于当我有2 y轴的时候非常有用,但是当我有更多的时候,我仍然有第2,第3等重叠。有任何想法吗? –