2017-08-17 170 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等重疊。有任何想法嗎? –