|
| 1 | +# Multiple Axes in R |
| 2 | +How to make a graph with multiple axes (dual y-axis plots, plots with secondary axes) in R. |
| 3 | + |
| 4 | + |
| 5 | +### Multiple Y Axes and Plotly |
| 6 | + |
| 7 | +> *Note*: At this time, Plotly does not support multiple Y axes on a single figure. To make such a figure, use the [`subplot()`](https://plotly.com/r/subplot-charts/) function as documented below. |
| 8 | +
|
| 9 | + |
| 10 | +## Two Y Axes |
| 11 | + |
| 12 | +```{r} |
| 13 | +library(plotly) |
| 14 | + |
| 15 | +fig <- plot_ly() |
| 16 | +# Add traces |
| 17 | +fig <- fig %>% add_trace(x = ~1:3, y = ~10*(4:6), name = "yaxis data", mode = "lines+markers", type = "scatter") |
| 18 | + |
| 19 | +ay <- list( |
| 20 | + tickfont = list(color = "red"), |
| 21 | + overlaying = "y", |
| 22 | + side = "right", |
| 23 | + title = "<b>secondary</b> yaxis title") |
| 24 | + |
| 25 | +fig <- fig %>% add_trace(x = ~2:4, y = ~4:6, name = "yaxis 2 data", yaxis = "y2", mode = "lines+markers", type = "scatter") |
| 26 | +
|
| 27 | +# Set figure title, x and y-axes titles |
| 28 | +fig <- fig %>% layout( |
| 29 | + title = "Double Y Axis Example", yaxis2 = ay, |
| 30 | + xaxis = list(title="xaxis title "), |
| 31 | + yaxis = list(title="<b>primary</b> yaxis title") |
| 32 | +) |
| 33 | + |
| 34 | +fig |
| 35 | +``` |
| 36 | + |
| 37 | +## Multiple axes in Dash |
| 38 | + |
| 39 | +[Dash for R](https://dashr.plotly.com) is an open-source framework for building analytical applications, with no Javascript required, and it is tightly integrated with the Plotly graphing library. |
| 40 | + |
| 41 | +Learn about how to install Dash for R at https://dashr.plot.ly/installation. |
| 42 | + |
| 43 | +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. |
| 44 | + |
| 45 | +```{r} |
| 46 | +library(dash) |
| 47 | +library(dashCoreComponents) |
| 48 | +library(dashHtmlComponents) |
| 49 | +library(plotly) |
| 50 | +
|
| 51 | +app <- Dash$new() |
| 52 | +
|
| 53 | +app$layout( |
| 54 | + htmlDiv( |
| 55 | + list( |
| 56 | + dccGraph(id = 'graph'), |
| 57 | + htmlLabel("Red line's axis:"), |
| 58 | + dccRadioItems( |
| 59 | + id='radio', |
| 60 | + options = list(list(label = "Primary", value = "Primary"), |
| 61 | + list(label = "Secondary", value = "Secondary")), |
| 62 | + value = 'Secondary' |
| 63 | + ) |
| 64 | + ) |
| 65 | + ) |
| 66 | +) |
| 67 | +app$callback( |
| 68 | + output(id = 'graph', property='figure'), |
| 69 | + params=list(input(id='radio', property='value')), |
| 70 | + function(value) { |
| 71 | + if(value == 'Primary'){ |
| 72 | + fig <- plot_ly() |
| 73 | + fig <- fig %>% add_trace(x = ~1:3, y = ~10*(4:6), name = "yaxis data", mode = "lines+markers", type = "scatter") |
| 74 | + fig <- fig %>% add_trace(x = ~2:4, y = ~4:6, name = "yaxis 2 data", mode = "lines+markers", type = "scatter") |
| 75 | + fig <- fig %>% layout( |
| 76 | + title = "Double Y Axis Example", |
| 77 | + xaxis = list(title="xaxis title"), |
| 78 | + yaxis = list(title="<b>primary</b> yaxis title") |
| 79 | + ) |
| 80 | + return(fig) |
| 81 | + } |
| 82 | + else{ |
| 83 | + fig <- plot_ly() |
| 84 | + fig <- fig %>% add_trace(x = ~1:3, y = ~10*(4:6), name = "yaxis data", mode = "lines+markers", type = "scatter") |
| 85 | + |
| 86 | + ay <- list( |
| 87 | + overlaying = "y", |
| 88 | + side = "right", |
| 89 | + title = "<b>secondary</b> yaxis title") |
| 90 | + |
| 91 | + fig <- fig %>% add_trace(x = ~2:4, y = ~4:6, name = "yaxis 2 data", yaxis = "y2", mode = "lines+markers", type = "scatter") |
| 92 | + |
| 93 | + fig <- fig %>% layout( |
| 94 | + title = "Double Y Axis Example", yaxis2 = ay, |
| 95 | + xaxis = list(title="xaxis title"), |
| 96 | + yaxis = list(title="<b>primary</b> yaxis title") |
| 97 | + ) |
| 98 | + return(fig) |
| 99 | + } |
| 100 | + }) |
| 101 | +#app$run_server() |
| 102 | +``` |
| 103 | + |
| 104 | +Use `app$run_server()` to run the dash file. |
| 105 | + |
| 106 | +## Multiple Y-Axes Subplots |
| 107 | + |
| 108 | +```{r} |
| 109 | +library(plotly) |
| 110 | +# Top left |
| 111 | +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") %>% |
| 112 | + add_trace(x = c(1, 2, 3), y = c(40, 50, 60),type="scatter",mode="lines+markers",yaxis="y2", name="yaxis2 data") %>% |
| 113 | + layout(yaxis=list(side="left"), |
| 114 | + yaxis2=list(side="right",overlaying="y"), |
| 115 | + showlegend=TRUE) |
| 116 | +# Top right |
| 117 | +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") %>% |
| 118 | + add_trace(x = c(1, 2, 3), y = c(40, 50, 60),type="scatter",mode="lines+markers",yaxis="y2", name="yaxis4 data") %>% |
| 119 | + layout(yaxis=list(side="left"), |
| 120 | + yaxis2=list(side="right",overlaying="y3"), |
| 121 | + showlegend=TRUE) |
| 122 | +# Bottom left |
| 123 | +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") %>% |
| 124 | + add_trace(x = c(1, 2, 3), y = c(40, 50, 60),type="scatter",mode="lines+markers",yaxis="y2", name="yaxis6 data") %>% |
| 125 | + layout(yaxis=list(side="left"), |
| 126 | + yaxis2=list(side="right",overlaying="y5"), |
| 127 | + showlegend=TRUE) |
| 128 | +# Bottom right |
| 129 | +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") %>% |
| 130 | + add_trace(x = c(1, 2, 3), y = c(40, 50, 60),type="scatter",mode="lines+markers",yaxis="y2", name="yaxis8 data") %>% |
| 131 | + layout(yaxis=list(side="left"), |
| 132 | + yaxis2=list(side="right",overlaying="y7"), |
| 133 | + showlegend=TRUE) |
| 134 | +
|
| 135 | +p <- subplot(p1,p2,p3,p4,nrows = 2, margin = 0.05)%>% layout(legend = list(x = 1.05, y = 1)) |
| 136 | +p |
| 137 | +``` |
| 138 | + |
| 139 | +## Multiple Axes |
| 140 | + |
| 141 | +Using Plotly for creating a figure with multiple axes |
| 142 | + |
| 143 | +```{r} |
| 144 | +library(plotly) |
| 145 | + |
| 146 | +fig <- plot_ly(width = 700) |
| 147 | +fig <- fig %>% add_trace(x = ~1:3, y = ~4:6, name = "yaxis1 data", mode = "lines+markers", type = "scatter") |
| 148 | + |
| 149 | +y2 <- list( |
| 150 | + tickfont = list(color = "#ff7f0e"), |
| 151 | + titlefont = list(color = "#ff7f0e"), |
| 152 | + overlaying = "y", |
| 153 | + side = "left", |
| 154 | + anchor="free", |
| 155 | + position=0.15, |
| 156 | + title = "yaxis2 title") |
| 157 | + |
| 158 | + |
| 159 | +fig <- fig %>% add_trace(x = ~2:4, y = ~10*(4:6), name = "yaxis2 data", yaxis = "y2", mode = "lines+markers", type = "scatter") |
| 160 | + |
| 161 | +y3 <- list( |
| 162 | + tickfont = list(color = "#d62728"), |
| 163 | + titlefont = list(color = "#d62728"), |
| 164 | + overlaying = "y", |
| 165 | + side = "right", |
| 166 | + title = "yaxis3 title") |
| 167 | + |
| 168 | + |
| 169 | +fig <- fig %>% add_trace(x = ~4:6, y = ~1000*(4:6), name = "yaxis3 data", yaxis = "y3", mode = "lines+markers", type = "scatter") |
| 170 | + |
| 171 | +y4 <- list( |
| 172 | + tickfont = list(color = "#9467bd"), |
| 173 | + titlefont = list(color = "#9467bd"), |
| 174 | + overlaying = "y", |
| 175 | + side = "right", |
| 176 | + anchor="free", |
| 177 | + position=0.85, |
| 178 | + title = "yaxis4 title") |
| 179 | + |
| 180 | + |
| 181 | +fig <- fig %>% add_trace(x = ~5:7, y = ~10000*(4:6), name = "yaxis4 data", yaxis = "y4", mode = "lines+markers", type = "scatter") |
| 182 | + |
| 183 | +fig <- fig %>% layout( |
| 184 | + title = "multiple y-axes example", yaxis2 = y2, yaxis3 = y3, yaxis4 = y4, |
| 185 | + xaxis = list(title = '', domain = c(0.3, 0.7)), |
| 186 | + yaxis = list(title="yaxis title", |
| 187 | + tickfont = list(color = "#1f77b4"), |
| 188 | + titlefont = list(color = "#1f77b4") |
| 189 | + ) |
| 190 | +) |
| 191 | + |
| 192 | +fig |
| 193 | +``` |
| 194 | + |
| 195 | +### Reference |
| 196 | +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](https://plotly.com/r/subplot-charts/) section. |
0 commit comments