gpt4 book ai didi

r - R绘图图中的第二个Y轴

转载 作者:行者123 更新时间:2023-12-03 17:51:01 26 4
gpt4 key购买 nike

我合并了2个图表,并试图添加第二个y轴,但是每次我在代码中添加yaxis =“y2”时,我都会丢失我的条形图。

>    MediaDate     Spend Search_Visits Other_Visits MediaDate2 
> 2016-04-01 $39654.36 19970 2899 Apr 2016
> 2016-05-01 $34446.28 14460 2658 May 2016
> 2016-06-01 $27402.36 12419 2608 Jun 2016

我的原始代码是:
p <- plot_ly(x= w$MediaDate2,y=w$Search_Visits,name = "Paid Search",
type = "bar")
p2 <- add_trace(p, x=w$MediaDate2, y=w$Other_Visits,name = "Other Traffic",
type = "bar")
spend_visits <- layout(p2, barmode = "stack")
spendvisits2 <- spend_visits %>% add_trace(data=w, x=MediaDate2, y=round(Spend,0), fill="tonexty", mode="lines",
text=w$MediaDate2, hoverinfo='name+y+text', name="Spend")

当我添加yaxis =“y2”时,仅保留面积图:
`spendvisits2 <- spend_visits %>% add_trace(data=w, x=MediaDate2,     y=round(Spend,0), yxis="y2" fill="tonexty", mode="lines",
text=w$MediaDate2, hoverinfo='name+y+text', name="Spend")`

任何建议都将大有帮助。谢谢

最佳答案

有关多个轴,请参见此快速Plotly tutorial。您需要在layout()中指定第二个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

关于r - R绘图图中的第二个Y轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39380227/

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