Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotly cutting off X and Y labels

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"))
}) 
like image 704
Mavic Avatar asked Mar 14 '18 22:03

Mavic


People also ask

How do you remove axis labels in Plotly?

Toggling axis labels The axis tick mark labels can be disabled by setting the showticklabels axis property to False . Here is an example of disabling tick labels in all subplots for a faceted figure created using Plotly Express.

How do you flip the Y axis in Plotly?

How do you flip the Y-axis in Plotly? You can rotate the plot by setting x=-x , and/or y=-y .


1 Answers

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
    )
like image 102
lkq Avatar answered Oct 05 '22 06:10

lkq