Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R. plotly - padding or margin for graph inside Shinyapp?

Tags:

r

plotly

r-plotly

I've a plotly plot that has not right left margin (neither at the bottom). Cannot find the documentation to add this extra space so my y axis (this plot is in horinzontal mode) don't get cut.

ui.R:

tabItem(tabName = "ga",        
            column(12, offset = 2,
            plotlyOutput("plot")
            )         
    )

server.R:

  sesiones_por_fuente <- reactive({ 


    sesiones_ga <- read_csv("https://www.dropbox.com/s/w2ggnb0p4mz2nus/sesiones-2018.csv?dl=1", skip = 0)

    sesiones_ga <- sesiones_ga %>%
                  group_by(sources) %>%
                  summarise(sessions = sum(sessions))


  })



  m <- list(
    l = 200,
    r = 50,
    b = 100,
    t = 100,
    pad = 20
  )


  output$plot <- renderPlotly({
    plot_ly(sesiones_por_fuente(), x = ~sessions, y = ~sources, type = 'bar',
            width = 1200, height = 600, margin = m, orientation = 'h', pad=4) %>% 
      layout(title = "Sesiones por mes",
             xaxis = list(title = ""),
             yaxis = list(title = "")) %>%
      layout(hovermode = 'compare',
             separators = ',') 
  })

data set:

You can download the data with:

sesiones_por_fuente <- read_csv("https://www.dropbox.com/s/w2ggnb0p4mz2nus/sesiones-2018.csv?dl=1", skip = 0)

or using recreate it with dput function:

sesiones_por_fuente <- structure(list(sources = c("adwords", "ccl", "criteo", "directo", 
"email", "facebookads", "onesignal", "organico", "redes sociales", 
"referencias", "rpp", "spam"), sessions = c(4534932L, 265532L, 
3959787L, 4290376L, 3870548L, 3125880L, 2345860L, 7002943L, 75382L, 
15061160L, 222730L, 5971162L)), class = c("tbl_df", "tbl", "data.frame"
), .Names = c("sources", "sessions"), row.names = c(NA, -12L), spec = structure(list(
    cols = structure(list(date = structure(list(format = ""), .Names = "format", class = c("collector_date", 
    "collector")), hour = structure(list(), class = c("collector_character", 
    "collector")), deviceCategory = structure(list(), class = c("collector_character", 
    "collector")), source = structure(list(), class = c("collector_character", 
    "collector")), medium = structure(list(), class = c("collector_character", 
    "collector")), sessions = structure(list(), class = c("collector_integer", 
    "collector")), year = structure(list(), class = c("collector_integer", 
    "collector")), month = structure(list(), class = c("collector_character", 
    "collector")), sources = structure(list(), class = c("collector_character", 
    "collector"))), .Names = c("date", "hour", "deviceCategory", 
    "source", "medium", "sessions", "year", "month", "sources"
    )), default = structure(list(), class = c("collector_guess", 
    "collector"))), .Names = c("cols", "default"), class = "col_spec"))

enter image description here

like image 434
Omar Gonzales Avatar asked Sep 11 '18 04:09

Omar Gonzales


1 Answers

Given your dput data, you need to do two things, one which addresses your issue and one which will get rid of a warning:

  • move margin = m from the call to plot_ly to the layout call as in the code below.
  • Remove the pad=4 option from the call to plot_ly as this gives a warning (i.e. it is an invalid option for plot_ly)

This is mainly an issue with how you are using plot_ly, not a shiny issue. I was able to reproduce the issue just using plot_ly in R, and the code below fixed the issue.

plot_ly(sesiones_por_fuente, x = ~sessions, y = ~sources, type = 'bar',
        width = 1200, height = 600, orientation = 'h') %>% 
  layout(title = "Sesiones por mes",
         xaxis = list(title = ""),
         yaxis = list(title = ""),
         margin = m) %>%
  layout(hovermode = 'compare',
         separators = ',') 

Here is the resulting plot with the extra space in the margin

Plotly Plot with expanded margin for y-axis

like image 82
steveb Avatar answered Sep 18 '22 15:09

steveb