如何在renderPlotly(Shiny r)中指定详细布局

时间:2016-09-26 11:09:03

标签: r shiny plotly

尝试在闪亮的应用中渲染Plotly图表。基本图表已生成但无法格式化轴。能够在Plotly Web应用程序中获得所需的图形选项,其布局如下所示。

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

在闪亮的代码中指定此布局时遇到问题。对于像标题这样的简单对象,它可以工作(如下所示)。

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

如何提供复杂的xaxis和yaxis布局?

1 个答案:

答案 0 :(得分:1)

我自己找到了问题的答案。 xaxis和yaxis布局都可以如下所示指定:

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

同样,可以指定其他此类格式。使用以下参考。Plotly R API reference doc for Axes Labels