Multiple axes labelling in R Plotly - r

I am trying to make a dual axis plot using plotly in R. However, I am unable to name the first y-axis using the following code whicI i found here: https://plot.ly/r/multiple-axes/
library(plotly)
ay <- list(
tickfont = list(color = "red"),
overlaying = "y",
side = "right",
title = "second y axis"
)
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")
)
I would really appreciate it if someone could offer some advice.

Are you looking for this:
library(plotly)
library(dplyr)
ay2 <- list(
tickfont = list(color = "red"),
side = "left",
title = "first y axis"
)
ay <- list(
tickfont = list(color = "red"),
overlaying = "y",
side = "right",
title = "second y axis"
)
plot_ly() %>%
add_lines(x = ~1:3, y = ~10*(1:3), name = "slope of 10", yaxis = "y1") %>%
add_lines(x = ~2:4, y = ~1:3, name = "slope of 1", yaxis = "y2") %>%
layout(
title = "Double Y Axis", yaxis2 = ay, yaxis = ay2,
xaxis = list(title="x")
)

Related

show right y axis in plotly chart with only 1 trace

In R plotly, I want to show a single line chart that has the y axis labels on BOTH the left and the right sides. I understand how to do this with 2 or more traces, but I can't find anywhere that shows how to do it with only 1 trace on the chart. Here's a basic example - it only shows the y axis on the left but I want it to appear on both sides:
library(plotly)
ay <- list(
tickfont = list(color = "red"),
overlaying = "y",
side = "right",
title = "second y axis"
)
fig <- plot_ly()
fig <- fig %>% add_lines(x = ~1:3, y = ~10*(1:3), name = "slope of 10")
fig <- fig %>% layout(
title = "Double Y Axis", yaxis2 = ay,
xaxis = list(title="x")
)
fig
You can add the same values in a new axis like this:
library(plotly)
#Setup
ay <- list(
tickfont = list(color = "red"),
overlaying = "y",
side = "right",
title = "second y axis"
)
#Plot
fig <- plot_ly()
fig <- fig %>% add_lines(x = ~1:3, y = ~10*(1:3), name = "slope of 10", yaxis = "y2")
fig <- fig %>%
add_lines(x = ~1:3, y = ~10*(1:3), name = "slope of 10") %>%
layout(
title = "Double Y Axis", yaxis2 = ay,
xaxis = list(title="x")
)
Output:
If you want to remove the legend:
#Plot 2
fig <- plot_ly()
fig <- fig %>% add_lines(x = ~1:3, y = ~10*(1:3), name = "slope of 10", yaxis = "y2")
fig <- fig %>%
add_lines(x = ~1:3, y = ~10*(1:3), name = "slope of 10") %>%
layout(
title = "Double Y Axis", yaxis2 = ay,
xaxis = list(title="x"),showlegend = FALSE
)
Output:

Adding second axis to plotly plot without simultaneously adding trace

Since ggplotly does not supportggplot2's sec.axis (Adding second Y axis on ggplotly), I want to add a second axis to the plotly-object instead. However, I do not wish to add any new trace.
Example:
library(plotly)
ay <- list(
tickfont = list(color = "red"),
overlaying = "y",
side = "right",
title = "second y axis"
)
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
How do I accomplish showing yaxis = "y2" without add_lines or adding any other trace?
One way to achieve this is to do what you have done and change the color of whatever you add for the second axis to "transparent", and turn off the hoverinfo and legend entry for the line :
library(plotly)
ay <- list(
tickfont = list(color = "red"),
overlaying = "y",
side = "right",
title = "second y axis"
)
p <- plot_ly() %>%
add_lines(x = ~1:3, y = ~10*(1:3), name = "slope of 10") %>%
add_lines(x = ~2:4, y = ~1:3, color = I("transparent"), name = "", yaxis = "y2", hoverinfo='skip', showlegend=FALSE) %>%
layout(
title = "Double Y Axis", yaxis2 = ay,
xaxis = list(title="x")
)
p

R + Plot.ly + add_trace

I'm trying a second Y axis to my line chart?
plot_ly(C,x =~Ymd,y=~Spots,mode="lines") # This works.
plot_ly(C,x =~Ymd,y=~ma90,mode="lines") # This works.
p<- plot_ly(C,x =~Ymd,y=~Spots,mode="lines")%>% # This doesn't work
add_trace(p,y=~ma90,mode="lines")
You need to specify yaxis = <new yaxis name> in the trace.
library(plotly)
ay <- list(
tickfont = list(color = "red"),
overlaying = "y",
side = "right",
title = "second y axis"
)
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")
)

To label the graph by y axis on the line chart in plotly in R

To label the line by the value of y axis. I have plotted two graph in one ,in the first i am getting my result as of my need but in second it is showing values at lower axis
how to display all value of x axis on the x plane .In my case i want to display all values from 1:18 in the x plane
ay <- list(
tickfont = list(color = "red"),
overlaying = "y",
side = "right",
title = "Modal price"
)
p <- plot_ly(M, x = ~Week, y = ~`Arrival(Tonnes)`,
type= "bar",mode= 'marker',
marker = list(size = 10),name ="Weekly Arrival") %>%
add_annotations(x = seq(1:18),
y = M$`Arrival(Tonnes)`,
text = M$`Arrival(Tonnes)`,
xref = "x",
yref = "y",
showarrow = TRUE,
arrowsize = .01,
ax = 0,
ay = -20) %>%
#2nd graph addition
add_lines(x = ~Week, y = ~`Modal price`, name = "Weekly Modal prices",
marker = list(size = 10), yaxis = "y2",mode="marker") %>%
layout(
title = "Model price vs Arrivals in Weeks", yaxis2 = ay,
xaxis = list(title="Weeks") ) %>%
##Problem here the values are not attaching with the graph continuity
add_annotations(x=M$Week,y=M$`Modal price`,text=M$`Modal
price`,xref="x",yref="y",
showarrow=TRUE,arrowsize=0.1,ax=20,ay=-20)

Plot.ly Double- Axis Formatting Content in R

I am having trouble formatting an axis using Plot.ly Double Axis graph. I am trying to get one of the y-axis to contain the '$' symbol in front of the number but I wasn't able to find any code for this. Hope someone can help me figure this out.
ay <- list(
tickfont = list(color = "red"),
overlaying = "y",
side = "right"
)
p <- plot_ly(data = df, x = days, y = sales, name = "Sales",type="bar") %>%
add_trace(x = days, y = Sales/Day, name = "Sales/Day", yaxis = "y2") %>%
layout(title = "Double Y Axis", yaxis2 = ay)
So in the above code I want to add a '$' symbol to both y axes (Sales and Sales/day).
Since you did not provide a reproducible example, I will use an example from the plotly website. You could try:
library(plotly)
ay <- list(
tickfont = list(color = "red"),
overlaying = "y",
side = "right"
)
plot_ly(x = 1:3, y = 10*(1:3), name = "slope of 10") %>%
add_trace(x = 2:4, y = 1:3, name = "slope of 1", yaxis = "y2") %>%
layout(title = "Double Y Axis", yaxis2 = ay, yaxis = list(showtickprefix = "all", tickprefix = "$"))

Resources