使用Plotly在RShiny中绘制水平漏斗图的问题求助
R Plotly 水平漏斗图修正方案
核心调整点
要生成水平漏斗图,仅需在add_trace函数中补充orientation = "h"参数即可,之前配置失效大概率是参数值填写错误、或参数未写到add_trace的配置项内。
修正后基础绘图代码
library(plotly) fig <- plot_ly() fig <- fig %>% add_trace( type = "funnel", orientation = "h", y = c("网站访问", "下载", "潜在客户", "申请报价", "已发送账单"), x = c(39, 27.4, 20.6, 11, 2) ) fig <- fig %>% layout(yaxis = list(categoryarray = c("网站访问", "下载", "潜在客户", "申请报价", "已发送账单"))) fig
R Shiny 嵌入示例
如果需要把该图集成到Shiny应用中,可参考以下完整代码:
library(shiny) library(plotly) # UI定义 ui <- fluidPage( plotlyOutput("horizontal_funnel") ) # 服务端逻辑 server <- function(input, output) { output$horizontal_funnel <- renderPlotly({ fig <- plot_ly() fig <- fig %>% add_trace( type = "funnel", orientation = "h", y = c("网站访问", "下载", "潜在客户", "申请报价", "已发送账单"), x = c(39, 27.4, 20.6, 11, 2) ) fig <- fig %>% layout(yaxis = list(categoryarray = c("网站访问", "下载", "潜在客户", "申请报价", "已发送账单"))) return(fig) }) } # 启动应用 shinyApp(ui, server)
内容的提问来源于stack exchange,提问作者Sameer Qayyum
相关产品推荐
相关产品推荐

