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

标签 r shiny plotly

尝试在 Shiny 的应用程序中渲染 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"
        }
    }

在 Shiny 的代码中指定此布局时面临问题。对于像标题这样的简单对象,它是有效的(如下所示)。

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 布局?

最佳答案

我自己找到了问题的答案。 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

关于r - 如何在 renderPlotly (Shiny r) 中指定详细布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39701242/

相关文章:

R 传单 - 在已打开弹出窗口的 map 上添加新标记

r - 如何使用Shiny服务器在本地计算机上对R Shiny应用程序进行Docker化?

r - 使用本地数据集部署 Shiny 的应用程序

python - 如何叠加不同日期的图?

r - 屏蔽 R 中的最高设置位?

r - 使用 sub 提取路径的一部分

python - Plotly:如何为所有子图设置 xticks?

r - 使用 rpart 生成桑基图的决策树

R Shiny : How to write loop for observeEvent

r - 使用 Dockerfile 安装 R 包