绘图切断 X 和 Y 标签

Mav*_*vic 4 r ggplot2 plotly ggplotly r-plotly

我有一个我用 plotly 创建的情节。当我将它部署到我闪亮的应用程序时,X 和 Y 标签被切断,如下所示:

这里。

我怎样才能防止这种情况发生?如果我使用普通图,标签不会被切断,但我需要图是交互式的。

这是我创建情节的代码:

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"))
  )
)
)
)
Run Code Online (Sandbox Code Playgroud)

服务器.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"))
}) 
Run Code Online (Sandbox Code Playgroud)

Keq*_* Li 5

您可以通过设置图的边距来解决此问题

plot_ly(
    ...
  ) %>%
    layout(
      margin = list(b = 50, l = 50) # to fully display the x and y axis labels
    )
Run Code Online (Sandbox Code Playgroud)