[英]Create funnelarea chart from data frame using plotly rstudio
I want to create a funnelarea chart using plotly, this is the example from plotly:我想使用 plotly 创建一个漏斗图,这是 plotly 的示例:
fig <- plot_ly(
type = "funnelarea",
values = c(5, 4, 3, 2, 1),
text = c("The 1st","The 2nd", "The 3rd", "The 4th", "The 5th"),
marker = list(colors = c("deepskyblue", "lightsalmon", "tan", "teal", "silver"),
line = list(color = c("wheat", "wheat", "blue", "wheat", "wheat"), width = c(0, 1, 5,
0, 4))),
textfont = list(family = "Old Standard TT, serif", size = 13, color = "black"),
opacity = 0.65)
fig
I would like to use a dataframe to fill this chart, use categories from my dataframe columns instead of text and values but I can't find the way to to it.我想使用数据框来填充此图表,使用数据框列中的类别而不是文本和值,但我找不到方法。
Example of my dataframe我的数据框示例
funnel_stage size purchaser_payment
1. Available for Sale 10 10000
1. Available for Sale 15 15000
2. Pending on Sale 8 8000
2. Pending on Sale 9 9000
3. Already Sold 1 1000
3. Already Sold 45 45000
3. Already Sold 12 12000
I would like my funnel filled counting the number of times of repetition of first column, It would be like:我希望我的漏斗填充计算第一列的重复次数,就像:
It's probably easiest if you first bring your data into a shape with one row per category in the correct order, see df2
below.如果您首先以正确的顺序将数据转换为每个类别一行的形状,这可能是最简单的,请参阅下面的df2
。
library("dplyr")
library("plotly")
df <- structure(
list(funnel_stage = c("Available for Sale", "Available for Sale",
"Pending on Sale", "Pending on Sale", "Already Sold",
"Already Sold", "Already Sold"),
size = c(10L, 15L, 8L, 9L, 1L, 45L, 12L),
purchaser_payment = c(10000L, 15000L, 8000L, 9000L, 1000L, 45000L, 12000L)),
class = "data.frame", row.names = c(NA, -7L))
df$funnel_stage <- factor(df$funnel_stage, levels = c("Available for Sale",
"Pending on Sale",
"Already Sold"))
df2 <- df %>%
group_by(funnel_stage) %>%
count()
df2
#> # A tibble: 3 x 2
#> # Groups: funnel_stage [3]
#> funnel_stage n
#> <fct> <int>
#> 1 Available for Sale 2
#> 2 Pending on Sale 2
#> 3 Already Sold 3
plot_ly() %>%
add_trace(
type = "funnelarea",
values = df2$n,
text = df2$funnel_stage)
packageVersion("dplyr")
#> [1] '0.8.5'
packageVersion("plotly")
#> [1] '4.9.2.1'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.