gpt4 book ai didi

R:双 Y 轴绘图?

转载 作者:行者123 更新时间:2023-12-02 17:04:37 25 4
gpt4 key购买 nike

我找到了这个问题,但答案不是最新的,无法产生正确的结果。

Second Y-Axis in a R plotly graph

如何绘制双 y 轴图?

df <- data.frame(MediaDate = as.Date(c("2016-04-01","2016-05-01","2016-06-01"), format = "%Y-%m-%d"),
Spend = c(39654, 34446, 27402),
Visits = c(19970, 14450, 12419))



plot_ly(df, x = ~MediaDate, y = ~Spend, type = "bar", name = "Spend") %>%
add_trace(x = ~MediaDate, y = ~Visits, mode = "lines", yaxis = "y2", name = "Visits") %>%
layout(yaxis2 = list(overlaying = "y", side = "right"))

产生:

enter image description here

我需要什么(但不是一条条和一条线,而是 2 条线):

enter image description here

最佳答案

这里有一个方法可以做到这一点:

df <- data.frame(MediaDate = as.Date(c("2016-04-01","2016-05-01","2016-06-01"), 
format = "%Y-%m-%d"),
Spend = c(39654, 34446, 27402),
Visits = c(19970, 14450, 12419))

old.y <- list(
side = "left",
title = "Spend"
)

new.y <- list(
overlaying = "y",
side = "right",
title = "Visits"
)

plot_ly(df) %>%
add_lines(x = ~MediaDate, y = ~Spend, yaxis="y1") %>%
add_lines(x = ~MediaDate, y = ~Visits, yaxis = "y2") %>%
layout(yaxis2 = new.y, yaxis = old.y, xaxis = list(title="MediaDate"))

enter image description here

关于R:双 Y 轴绘图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52340981/

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