## 13.13 帕雷托图

# 数据来自 https://github.com/plotly/datasets
dat <- data.frame(
complaint = c(
"Small portions", "Overpriced",
"Wait time", "Food is tasteless", "No atmosphere", "Not clean",
"Too noisy", "Food is too salty", "Unfriendly staff", "Food not fresh"
),
count = c( 621L, 789L, 109L, 65L, 45L, 30L, 27L, 15L, 12L, 9L)
)

dat <- dat[order(-dat$count), ] %>% transform(cumulative = round(100 * cumsum(count) / sum(count), digits = 2)) # complaint 按 count 降序排列 dat$complaint <- reorder(x = dat$complaint, X = dat$count, FUN = function(x) 1/(1 + x))

plot_ly(data = dat) %>%
x = ~complaint, y = ~count,
showlegend = F, color = I("gray60")
) %>%
x = ~complaint, y = ~cumulative, yaxis = "y2",
showlegend = F, color = I("gray40")
) %>%
layout(
yaxis2 = list(
tickfont = list(color = "black"),
overlaying = "y",
side = "right",
title = "累积百分比（%）",
showgrid = F
),
xaxis = list(title = "投诉类型", showgrid = F, showline = F),
yaxis = list(title = "数量", showgrid = F, showline = F)
)

reorder() 对 complaint 按照降序还是升序由 FUN 函数的单调性决定，单调增对应升序，单调减对应降序