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

时间:2018-09-11 04:20:08

标签: r plotly r-plotly

我有一个没有右左边界(都不在底部)的绘图。找不到添加此额外空间的文档,因此我的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"))

enter image description here

2 个答案:

答案 0 :(得分:2)

鉴于您的dput数据,您需要做两件事,一是解决您的问题,另一是摆脱警告:

  • margin = m从呼叫移至plot_lylayout呼叫,如下代码。
  • pad=4选项从调用中移至plot_ly,因为这会发出警告(即,它对于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 = ',') 

这是在边距中有多余空间的结果图

Plotly Plot with expanded margin for y-axis

答案 1 :(得分:1)

我有两个可能的建议:

1)将l从200增加到更多。但是,正如我所看到的那样,您根据情节空间所讲的话并没有帮助;

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

您有两个大小不同的pads,也许这会导致您遇到问题