Multiple Axes in R

How to make a graph with multiple axes (dual y-axis plots, plots with secondary axes) in R.


New to Plotly?

Plotly is a free and open-source graphing library for R. We recommend you read our Getting Started guide for the latest installation or upgrade instructions, then move on to our Plotly Fundamentals tutorials or dive straight in to some Basic Charts tutorials.

Multiple Y Axes and Plotly

Two Y Axes

library(plotly)

fig <- plot_ly()
# Add traces
fig <- fig %>% add_trace(x = ~1:3, y = ~10*(4:6), name = "yaxis data", mode = "lines+markers", type = "scatter")

ay <- list(
  tickfont = list(color = "red"),
  overlaying = "y",
  side = "right",
  title = "<b>secondary</b> yaxis title")

fig <- fig %>% add_trace(x = ~2:4, y = ~4:6, name = "yaxis 2 data", yaxis = "y2", mode = "lines+markers", type = "scatter")

# Set figure title, x and y-axes titles
fig <- fig %>% layout(
  title = "Double Y Axis Example", yaxis2 = ay,
  xaxis = list(title="xaxis title "),
  yaxis = list(title="<b>primary</b> yaxis title")
)%>%
  layout(plot_bgcolor='#e5ecf6',
          xaxis = list(
            zerolinecolor = '#ffff',
            zerolinewidth = 2,
            gridcolor = 'ffff'),
          yaxis = list(
            zerolinecolor = '#ffff',
            zerolinewidth = 2,
            gridcolor = 'ffff')
          )

fig

Multiple axes in Dash

Dash for R is an open-source framework for building analytical applications, with no Javascript required, and it is tightly integrated with the Plotly graphing library.

Learn about how to install Dash for R at https://dashr.plot.ly/installation.

Everywhere in this page that you see fig, you can display the same figure in a Dash for R application by passing it to the figure argument.

library(dash)
library(dashCoreComponents)
library(dashHtmlComponents)
library(plotly)

app <- Dash$new()

app$layout(
  htmlDiv(
    list(
      dccGraph(id = 'graph'),
      htmlLabel("Red line's axis:"),
      dccRadioItems(
        id='radio',
        options = list(list(label = "Primary", value = "Primary"),
                       list(label = "Secondary", value = "Secondary")),
        value = 'Secondary'
      )
    )
  )
)
app$callback(
  output(id = 'graph', property='figure'),
  params=list(input(id='radio', property='value')),
  function(value) {
    if(value == 'Primary'){
      fig <- plot_ly()
      fig <- fig %>% add_trace(x = ~1:3, y = ~10*(4:6), name = "yaxis data", mode = "lines+markers", type = "scatter")
      fig <- fig %>% add_trace(x = ~2:4, y = ~4:6, name = "yaxis 2 data", mode = "lines+markers", type = "scatter")
      fig <- fig %>% layout(
        title = "Double Y Axis Example",
        xaxis = list(title="xaxis title"),
        yaxis = list(title="<b>primary</b> yaxis title")
      )%>%
        layout(plot_bgcolor='#e5ecf6',
          xaxis = list(
            zerolinecolor = '#ffff',
            zerolinewidth = 2,
            gridcolor = 'ffff'),
          yaxis = list(
            zerolinecolor = '#ffff',
            zerolinewidth = 2,
            gridcolor = 'ffff')
          )
      return(fig)
    }
    else{
      fig <- plot_ly()
      fig <- fig %>% add_trace(x = ~1:3, y = ~10*(4:6), name = "yaxis data", mode = "lines+markers", type = "scatter")

      ay <- list(
        overlaying = "y",
        side = "right",
        title = "<b>secondary</b> yaxis title")

      fig <- fig %>% add_trace(x = ~2:4, y = ~4:6, name = "yaxis 2 data", yaxis = "y2", mode = "lines+markers", type = "scatter")

      fig <- fig %>% layout(
        title = "Double Y Axis Example", yaxis2 = ay,
        xaxis = list(title="xaxis title"),
        yaxis = list(title="<b>primary</b> yaxis title")
      )%>%
        layout(plot_bgcolor='#e5ecf6',
          xaxis = list(
            zerolinecolor = '#ffff',
            zerolinewidth = 2,
            gridcolor = 'ffff'),
          yaxis = list(
            zerolinecolor = '#ffff',
            zerolinewidth = 2,
            gridcolor = 'ffff')
          )
      return(fig)
    }
  })
#app$run_server()

Use app$run_server() to run the dash file.

Multiple Y-Axes Subplots

library(plotly)
# Top left
p1 <- plot_ly() %>%
  add_trace(x = c(1, 2, 3), y = c(2, 52, 62),
                              type="scatter",mode="lines+markers",yaxis="y", name="yaxis data") %>%
  add_trace(x = c(1, 2, 3), y = c(40, 50, 60),
            type="scatter",mode="lines+markers",yaxis="y2", name="yaxis2 data") %>%
  layout(yaxis=list(side="left"),
         yaxis2=list(side="right",overlaying="y"),
         showlegend=TRUE)
# Top right
p2 <-plot_ly() %>%
  add_trace(x = c(1, 2, 3), y = c(2, 52, 62),
                             type="scatter",mode="lines+markers",yaxis="y", name="yaxis3 data") %>%
  add_trace(x = c(1, 2, 3), y = c(40, 50, 60),
            type="scatter",mode="lines+markers",yaxis="y2", name="yaxis4 data") %>%
  layout(yaxis=list(side="left"),
         yaxis2=list(side="right",overlaying="y3"),
         showlegend=TRUE)
# Bottom left
p3 <- plot_ly() %>%
  add_trace(x = c(1, 2, 3), y = c(2, 52, 62),
            type="scatter",mode="lines+markers",yaxis="y", name="yaxis5 data") %>%
  add_trace(x = c(1, 2, 3), y = c(40, 50, 60),
            type="scatter",mode="lines+markers",yaxis="y2", name="yaxis6 data") %>%
  layout(yaxis=list(side="left"),
         yaxis2=list(side="right",overlaying="y5"),
         showlegend=TRUE)
# Bottom right
p4 <-plot_ly() %>%
  add_trace(x = c(1, 2, 3), y = c(2, 52, 62),
                             type="scatter",mode="lines+markers",yaxis="y", name="yaxis7 data") %>%
  add_trace(x = c(1, 2, 3), y = c(40, 50, 60),
            type="scatter",mode="lines+markers",yaxis="y2", name="yaxis8 data") %>%
  layout(yaxis=list(side="left"),
         yaxis2=list(side="right",overlaying="y7"),
         showlegend=TRUE)

p <- subplot(p1,p2,p3,p4,nrows = 2, margin = 0.05)%>%
  layout(legend = list(x = 1.05, y = 1))%>%
  layout(plot_bgcolor='#e5ecf6',
          xaxis = list(
            zerolinecolor = '#ffff',
            zerolinewidth = 2,
            gridcolor = 'ffff'),
          yaxis = list(
            zerolinecolor = '#ffff',
            zerolinewidth = 2,
            gridcolor = 'ffff')
          )
p

Multiple Axes

Using Plotly for creating a figure with multiple axes

library(plotly)

fig <- plot_ly(width = 700)
fig <- fig %>% add_trace(x = ~1:3, y = ~4:6, name = "yaxis1 data", mode = "lines+markers", type = "scatter")

y2 <- list(
  tickfont = list(color = "#ff7f0e"),
  titlefont = list(color = "#ff7f0e"),
  overlaying = "y",
  side = "left",
  anchor="free",
  position=0.15,
  title = "yaxis2 title")


fig <- fig %>% add_trace(x = ~2:4, y = ~10*(4:6), name = "yaxis2 data", yaxis = "y2", mode = "lines+markers", type = "scatter")

y3 <- list(
  tickfont = list(color = "#d62728"),
  titlefont = list(color = "#d62728"),
  overlaying = "y",
  side = "right",
  title = "yaxis3 title")


fig <- fig %>% add_trace(x = ~4:6, y = ~1000*(4:6), name = "yaxis3 data", yaxis = "y3", mode = "lines+markers", type = "scatter")

y4 <- list(
  tickfont = list(color = "#9467bd"),
  titlefont = list(color = "#9467bd"),
  overlaying = "y",
  side = "right",
  anchor="free",
  position=0.85,
  title = "yaxis4 title")


fig <- fig %>% add_trace(x = ~5:7, y = ~10000*(4:6), name = "yaxis4 data", yaxis = "y4", mode = "lines+markers", type = "scatter")

fig <- fig %>% layout(
  title = "multiple y-axes example", yaxis2 = y2, yaxis3 = y3, yaxis4 = y4,
  xaxis = list(title = '', domain = c(0.3, 0.7)),
  yaxis = list(title="yaxis title",
               tickfont = list(color = "#1f77b4"),
               titlefont = list(color = "#1f77b4")
               )
)%>%
  layout(plot_bgcolor='#e5ecf6',
          xaxis = list(
            zerolinecolor = '#ffff',
            zerolinewidth = 2,
            gridcolor = 'ffff'),
          yaxis = list(
            zerolinecolor = '#ffff',
            zerolinewidth = 2,
            gridcolor = 'ffff')
          )

fig

Reference

All of the y-axis properties are found here: https://plotly.com/r/reference/layout/yaxis/. For more information on creating subplots see the Subplots in R section.

What About Dash?

Dash for R is an open-source framework for building analytical applications, with no Javascript required, and it is tightly integrated with the Plotly graphing library.

Learn about how to install Dash for R at https://dashr.plot.ly/installation.

Everywhere in this page that you see fig, you can display the same figure in a Dash for R application by passing it to the figure argument of the Graph component from the built-in dashCoreComponents package like this:

library(plotly)

fig <- plot_ly() 
# fig <- fig %>% add_trace( ... )
# fig <- fig %>% layout( ... ) 

library(dash)
library(dashCoreComponents)
library(dashHtmlComponents)

app <- Dash$new()
app$layout(
    htmlDiv(
        list(
            dccGraph(figure=fig) 
        )
     )
)

app$run_server(debug=TRUE, dev_tools_hot_reload=FALSE)