Plotly截断X和Y轴标签

7

我有一个用plotly创建的图表。当我将它部署到我的shiny app上时,X和Y标签被裁剪了,如下图所示:

here.

我该如何防止这种情况发生?如果我使用普通的图表,则标签不会被裁剪,但我需要图表是交互式的。

以下是我创建图表的代码:

ui.r:

#creating app with shiny
library(shiny)
library(shinydashboard)

shinyUI(
  dashboardPage(
  dashboardHeader(title = "Dashboard"),
  dashboardSidebar(
  menuItem("Dashboard")
),
dashboardBody(
  fluidPage(
    box(plotlyOutput("bakePlot")),
    box(plotOutput("bakeMonthly"))
  )
)
)
)

server.r:

shinyServer(function(input, output){

output$bakePlot <- renderPlotly({
ggplot(sales_bakery, aes(ProductName, ProductSales))+ 
stat_summary(fun.y=sum,geom="bar",colour="red",fill="red",show.legend = FALSE) +
coord_cartesian(ylim = c(7000, 9500)) + ggtitle("January Sales in Bakery") + 
xlab("Category") + ylab("Quantity Sold")+
  theme(
        axis.title.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text.x = element_text(angle = 60, hjust = 1),
        axis.text.y = element_text(colour = "black", size = 14),
        panel.background = element_rect(fill = "white"),
        panel.grid.minor = element_blank(),
        panel.grid.major = element_blank(),
        axis.line = element_line(colour = "black", size = 1),
        legend.position = "none",
        plot.title = element_text(lineheight = 1.8, face = "bold"))
}) 

1
也许可以增加绘图边距,例如 + theme(plot.margin = unit(c(1, 1, 1, 2), "cm"))?最后一个数字是左边距,您可能需要尝试不同的值。 - Maurits Evers
1个回答

9
你可以通过为绘图设置边距来解决这个问题。
plot_ly(
    ...
  ) %>%
    layout(
      margin = list(b = 50, l = 50) # to fully display the x and y axis labels
    )

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接