R 图中的第二个 Y 轴 [英] Second Y-Axis in a R plotly graph

查看:26
本文介绍了R 图中的第二个 Y 轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我合并了 2 个图表并尝试添加第二个 y 轴,但每次我将 yaxis = "y2" 添加到我的代码中时,我都会失去我的条形图.

<代码>>MediaDate Spend Search_Visits Other_Visits MediaDate2>2016-04-01 $39654.36 19970 2899 2016 年 4 月>2016-05-01 $34446.28 14460 2658 2016 年 5 月>2016-06-01 $27402.36 12419 2608 2016 年 6 月

我原来的代码是:

p <- plot_ly(x= w$MediaDate2,y=w$Search_Visits,name = "付费搜索",类型=酒吧")p2 <- add_trace(p, x=w$MediaDate2, y=w$Other_Visits,name = "其他流量",类型=酒吧")花费_访问<-布局(p2,barmode =堆栈")花费访问 2 <- 花费访问 %>% 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")`

任何建议都会非常有帮助.谢谢

解决方案

快速查看

I combined 2 charts and I am trying to add the second y-axis, but every time I add the yaxis = "y2" to my code, I lose have the my bar graphs.

>    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

my original code is:

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") 

When I add the yaxis= "y2", only the area chart remains:

`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")` 

Any suggestions would be immensely helpful. Thank you

解决方案

See this quick Plotly tutorial for multiple axes. You need to specify the attributes for the second y axis in layout().

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"))

这篇关于R 图中的第二个 Y 轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆