Reputation: 176
Trying to render a Plotly graph in a shiny app. The basic graph is getting generated but unable to format the axes. Able to get the desired graph options in the Plotly Web Application with the layout as given below.
"layout": {
"autosize": true,
"height": 831.625,
"hovermode": "x",
"width": 1480,
"xaxis": {
"autorange": true,
"hoverformat": "%Y-%m",
"range": [
1356978600000,
1391193000000
],
"rangeselector": {
"buttons": [
{
"label": "reset",
"step": "all"
},
{
"label": "#1",
"step": "month"
}
]
},
"tickformat": "%Y-%m",
"tickmode": "auto",
"tickprefix": "",
"title": "B",
"type": "date"
},
"yaxis": {
"autorange": true,
"range": [
-19.888888888888893,
397.8888888888889
],
"title": "A",
"type": "linear"
}
}
Facing issue in specifying this layout within the shiny code. For simple objects like title, it works (as shown below).
output$plot <- renderPlotly({
new_plot <- plot_ly(plot_data, x = plot_data$FY_Month, y = plot_data$Booking_Amount , name = "Test Data (Actual)")
new_plot <- layout(new_plot,title = "Random Title")
new_plot
})
How do I give the complex xaxis and yaxis layouts?
Upvotes: 2
Views: 2379
Reputation: 176
Found an answer to the question myself. Both xaxis and yaxis layouts can be specified as shown below:
x_axis <- list(
title = "Fiscal Year / Month",
autorange = "true",
hoverformat = "%Y-%m",
tickformat = "%Y-%m",
tickmode = "auto",
type = "date"
)
y_axis <- list(
title = "A",
type ="linear"
)
output$plot <- renderPlotly({
new_plot <- plot_ly(plot_data, x = plot_data$FY_Month, y = plot_data$Booking_Amount , name = "Test Data (Actual)")
new_plot <- layout(new_plot,title = "Random Title")
new_plot <- layout(new_plot, xaxis=x_axis, yaxis = y_axis)
new_plot
})
Similarly, other such formats can be specified. Used the following reference.Plotly R API reference doc for Axes Labels
Upvotes: 2