show right y axis in plotly chart with only 1 trace - r

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:

Related

R: subplot with plots that have multiple y-axis and x-axis

I am trying to draw a subplot that contains 2 plots. Each inner plots have two y-axis and two x-axis. I also want to match each y label with his lines color.
First I want to start with a simple example. Consider following code
library(plotly)
library(dplyr)
p1 <- economics %>% plot_ly()%>%
add_trace( x = ~date, y = ~unemploy,yaxis='y1',type='scatter',mode="lines+markers")%>%
add_trace(x = ~date, y = ~uempmed,yaxis='y2',type='scatter',mode="lines+markers") %>%
layout( yaxis2 = list(overlaying = "y1",side = "right"))
p2<-p1
>p1
The output:
subplot(p1, p2 , nrows = 2 , shareX = TRUE)
The output:
So the subplot does not work fine. How to fix it?
EDIT:
With a little change to the code as shown below and it work!
R code
library(plotly)
library(dplyr)
p1 <- economics %>% plot_ly()%>%
add_trace( x = ~date, y =~unemploy,yaxis='y',type='scatter',mode="lines+markers")%>%
add_trace(x = ~date, y = ~uempmed,yaxis='y2',type='scatter',mode="lines+markers") %>%
layout( yaxis = list(side = "left"),yaxis2 = list(overlaying = "y",side = "right"))
p2<-economics %>% plot_ly()%>%
add_trace( x = ~date, y = ~unemploy,yaxis='y',type='scatter',mode="lines+markers")%>%
add_trace(x = ~date, y = ~uempmed,yaxis='y2',type='scatter',mode="lines+markers") %>%
layout(yaxis = list(side = "left"), yaxis2 = list(overlaying = "y3",side = "right"))
subplot(p1,p2,nrows=2)
The output now is as follows:
To extend for have 3 plots:
library(plotly)
library(dplyr)
p1 <- economics %>% plot_ly()%>%
add_trace( x = ~date, y = ~unemploy,yaxis='y',type='scatter',mode="lines+markers")%>%
add_trace(x = ~date, y = ~uempmed,yaxis='y2',type='scatter',mode="lines+markers") %>%
layout( yaxis = list(side = "left"),yaxis2 = list(overlaying = "y",side = "right"))
p2<-economics %>% plot_ly()%>%
add_trace( x = ~date, y = ~unemploy,yaxis='y',type='scatter',mode="lines+markers")%>%
add_trace(x = ~date, y = ~uempmed,yaxis='y2',type='scatter',mode="lines+markers") %>%
layout(yaxis = list(side = "left"), yaxis2 = list(overlaying = "y3",side = "right"))
p3<-economics %>% plot_ly()%>%
add_trace( x = ~date, y = ~unemploy,yaxis='y',type='scatter',mode="lines+markers")%>%
add_trace(x = ~date, y = ~uempmed,yaxis='y2',type='scatter',mode="lines+markers") %>%
layout(yaxis = list(side = "left"), yaxis2 = list(overlaying = "y5",side = "right"))
subplot(p1,p2,p3,nrows=3)
The output:

How to add X label, Y label, z label in R-plotly contour plot

I have created a contour plot using R-plotly. Unfortunately, I am unable to add title of the plot, x axis title and, y axis title on the contour plot. I would also like to incorporate the Z labels (location name/sample ID) inside the plot. can any body help me to add those? My dataframe contains x=latitude, y=longitude, chla=Z, which is collected form 6 locations. My code is below
install.packages ("plotly")
library(plotly)
df<-read.csv("contour_Jan20.csv",head=T)
df
fig <- plot_ly(df,x= df$Lat, y=df$Long, z=df$Chl,
z = ~volcano, type = "contour", contours = list(showlabels = TRUE)
)
fig <- fig %>% colorbar(title = "Concentration \n in mg/L")
fig
Here's an example using mtcars -
library(plotly)
fig <- plot_ly(mtcars,x= ~mpg, y=~disp, z=~am, type = "contour",
contours = list(showlabels = TRUE))
fig <- fig %>%
colorbar(title = "Concentration \n in mg/L") %>%
layout(title = 'Title of the plot', plot_bgcolor = "#e5ecf6")
fig
fig <- plot_ly(mtcars,x= ~mpg, y=~disp, z=~am, type = "contour",
contours = list(showlabels = TRUE))
t <- list(
family = "Courier New",
size = 14,
color = "blue")
t1 <- list(
family = "Times New Roman",
color = "red"
)
t2 <- list(
family = "Courier New",
size = 14,
color = "green")
t3 <- list(family = 'Arial')
fig <- fig %>%
colorbar(title = "legend title", font =t2) %>%
layout(title = 'Plot title', font =t1,
plot_bgcolor = "#e5ecf6",xaxis = list(title = "X title", font = t3),
yaxis = list(title = "y title", font = t3))
fig

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

Multiple axes labelling in R Plotly

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

Resources