尝试在 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/