7

I have a plot which I have created with plotly. When I deploy it to my shiny app the X and Y labels are cut off as you can see here:

here.

How can I prevent this from happening? If I use a normal plot the labels do not get cut off but I need the plot to be interactive.

Here is my code for creating the plot:

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"))
}) 
Syfer
  • 4,262
  • 3
  • 20
  • 37
Mavic
  • 153
  • 2
  • 2
  • 6
  • 1
    Perhaps increase the plot margin, e.g. `+ theme(plot.margin = unit(c(1, 1, 1, 2), "cm"))`? The last number is the left margin, you may want to play around with that. – Maurits Evers Mar 14 '18 at 23:54

1 Answers1

9

You can work around this by setting margins for the plot

plot_ly(
    ...
  ) %>%
    layout(
      margin = list(b = 50, l = 50) # to fully display the x and y axis labels
    )
lkq
  • 2,326
  • 1
  • 12
  • 22