R. plotly - Shinyapp 中图形的填充或边距?

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

我有一个没有右边距(底部也没有)的情节图。找不到添加此额外内容的文档 space 所以我的 y 轴(此图处于水平模式)不会被剪切。

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 = ',') 
  })

数据集:

您可以通过以下方式下载数据:

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

或使用 dput 函数重新创建它:

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"))

我有两个可能的建议:

1) 将 l 从 200 增加到更多。但正如我所见,你的情节 space 和这无济于事;

2) 将pad增加到100。但是情节看起来不太好。

您有两个 pads 大小不同,这可能是您遇到问题的原因

根据您的 dput 数据,您需要做两件事,一件事解决您的问题,另一件事消除警告:

  • margin = m 从对 plot_ly 的调用移动到 layout 调用,如下面的代码所示。
  • 从对 plot_ly 的调用中删除 pad=4 选项,因为这会发出警告(即它是 plot_ly 的无效选项)

这主要是您使用方式的问题 plot_ly,不是什么闪亮的问题。我能够在 R 中使用 plot_ly 重现该问题,下面的代码解决了该问题。

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 = ',') 

这是结果图,边距space