gpt4 book ai didi

r - 多轴绘图

转载 作者:行者123 更新时间:2023-12-01 16:15:47 24 4
gpt4 key购买 nike

我在 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

最佳答案

据我所知,R 的 plotly 中的多个 y 轴未正确管理,规避上述问题的唯一技巧是调整 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

关于r - 多轴绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45730139/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com