我已经苦恼了好几天,希望能得到帮助。我有一个包含"High","Medium"或"low"值的表格数据的列'results'。我想使用plotly创建饼图,通过计算数据集中"High","Medium"和"Low"的数量,并尝试为每个类别分配一种颜色。下面是我的代码。我已经尝试过将cols1和cols2作为标记以及其他几种方法,但似乎都不起作用。请注意,这是一个动态表格,因此可能存在没有高级或中级等情况,因此我不能只使用list(c("tomato","orange","olivedrab")作为标记。颜色必须与结果类别相对应。
tab <- count(data, results)
tab <- transform(tab,
results_ord = factor(
results ,
levels = c('High', 'Medium', 'Low'),
ordered = TRUE
))
cols1 <-
c(
High = "olivedrab",
Medium = "orange",
Low = "tomato"
)
cols2 <-
c(ifelse(
tab$results_ord == "High",
"olivedrab",
ifelse(
tab$results_ord == "Medium",
"orange",
"tomato"
)
))
plot_ly(
tab,
labels = results_ord,
values = n,
marker = list(cols2),
type = "pie"
) %>%
layout(title = "Results")
thanks,
曼努吉·阿格拉沃
cols1 <- c( "olivedrab", "orange", "tomato" ) plot_ly( tab, labels = results_ord, values = n, marker = marker(color=results_ord,colour=list(cols1)), type = "pie" ) %>% layout(title = "Results")
请问您能告诉我正确的语法吗? - Manoj Agrawal