Increase distance between text and title on the y-axis does not work in plotly

Hello I have created a shiny app which includes a scatter plot cretaed with plotly. My problem is that I cannot increase the space between the axis titles(y) and axis labels despite using axis.title.y = element_text(margin = margin(t = 0, r = 20, b = 0, l = 20)) .The plot is updated every time I change the inputs so both the axes titles and labels change every time.

#ui.r
    library(shiny)
    library(ggplot2)
    library(plotly)
    fluidPage(

      # App title ----
      titlePanel(div("CROSS CORRELATION",style = "color:blue")),

      # Sidebar layout with input and output definitions ----
      sidebarLayout(

        # Sidebar panel for inputs ----
        sidebarPanel(



        ),
        # Main panel for displaying outputs ----
        mainPanel(

          tabsetPanel(type = "tabs",
                      tabPanel("Table",
                               shiny::dataTableOutput("contents")),
                      tabPanel("Correlation Plot",
                               fluidRow(
                                 column(3, uiOutput("lx1")),
                                 column(3,uiOutput("lx2"))),
                               hr(),
                               fluidRow(
                                 plotlyOutput("sc"))
                      ))
          )))
    #server.r
    function(input, output) {


      output$contents <- shiny::renderDataTable({

        iris
      })


      output$lx1<-renderUI({
        selectInput("lx1", label = h4("Select 1st Expression Profile"), 
                    choices = colnames(iris[,1:4]), 
                    selected = "Lex1")
      })
      output$lx2<-renderUI({
        selectInput("lx2", label = h4("Select 2nd Expression Profile"), 
                    choices = colnames(iris[,1:4]), 
                    selected = "Lex2")
      })


      output$sc<-renderPlotly({

        p1 <- ggplot(iris, aes_string(x = input$lx1, y = input$lx2))+

          # Change the point options in geom_point
          geom_point(color = "darkblue") +
          # Change the title of the plot (can change axis titles
          # in this option as well and add subtitle)
          labs(title = "Cross Correlation") +
          # Change where the tick marks are
          scale_x_continuous(breaks = seq(0, 2.5, 30)) +
          scale_y_continuous(breaks = seq(0, 2.5, 30)) +
          # Change how the text looks for each element
          theme(title = element_text(family = "Calibri", 
                                     size = 10, 
                                     face = "bold"), 
                axis.title = element_text(family = "Calibri Light", 
                                          size = 16, 
                                          face = "bold", 
                                          color = "darkgrey"), 
                axis.text = element_text(family = "Calibri", 
                                         size = 11),
                             axis.title.y = element_text(margin = margin(t = 0, r = 20, b = 0, l = 20))
)+
          theme_bw()+
          geom_smooth(method = input$td)+
          annotate("text", x = 10, y = 10, label = as.character(input$an))
        ggplotly(p1) %>%
          layout(hoverlabel = list(bgcolor = "white", 
                                   font = list(family = "Calibri", 
                                               size = 9, 
                                               color = "black")))

      }) 




    }

You are passing both theme(...) and theme_bw() to ggplot().
I believe the theme_bw() overrides your custom settings. Can you try running it without theme_bw()?
Rgds,
Peter

2 Likes