Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Second Y-Axis in a R plotly graph

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

like image 585
Nisha G. Avatar asked Sep 07 '16 23:09

Nisha G.


People also ask

How do you plot two Y axis in Plotly?

Note: At this time, Plotly Express does not support multiple Y axes on a single figure. To make such a figure, use the make_subplots() function in conjunction with graph objects as documented below.

How do I add a second Y axis in R?

Example: Create Plot with 2 Axes in R Let's deconstruct the code: par(mar = c(5, 4, 4, 4) + 0.3) – This code defines how much white space should be shown around the plot. It is important to leave enough space for the second y-axis. plot(x, y1, pch = 16, col = 2) – This code creates the first plot (i.e. the red dots).

How do you create a secondary axis in Plotly?

First, import the necessary functions from the Plotly package and create the secondary axes using the specs parameter in the make_subplots() function as shown. Plot a scatter plot with multiple y-axes. Make the chart readable by adding titles to the x and y axes.

Can you graph data with two Y axis?

You can use the Double Y-axis Graphs to plot two different data series. You can achieve this using a secondary y-axis on the right hand side.


2 Answers

Try this

plot_ly(df) %>%
  add_trace(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"))
like image 184
Karo Avatar answered Sep 23 '22 20:09

Karo


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

enter image description here

like image 26
Vance Lopez Avatar answered Sep 24 '22 20:09

Vance Lopez