gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 12:23:46 26 4
gpt4 key购买 nike

我有一个没有右边距的 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

最佳答案

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

  • 搬家 margin = m从调用 plot_lylayout调用如下代码。
  • 删除 pad=4来自调用 plot_ly 的选项因为这会发出警告(即它是 plot_ly 的无效选项)

  • 这主要是您使用方式的问题 plot_ly ,不是一个 Shiny 的问题。我仅使用 plot_ly 就能重现该问题在 R 中,下面的代码解决了这个问题。
    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

    关于R. plotly - Shinyapp 内部图形的填充或边距?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52268601/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com