使用带有计数和百分比的 Plotly 在 R 中打开 Pie Chart/Donut 图表
Open Pie Chart/Donut Chart in R using Plotly with count and percentage
我正在尝试使用 plotly 在 R 中制作圆环图。我试过 ggplot,但它无法提供我需要的效果。这是一个示例数据集:
library(dplyr)
testfile <- tibble(personID = 1:10,
status = c("bad", "good", "bad", "bad", "bad", "bad", "bad", "bad", "bad", "good"),
department = c("sales", "sales", "marketing", "sales", "marketing", "management", "management", "sales", "sales", "sales"))
此图表最终会出现在 PowerPoint 中,因此不需要响应式。相反,我需要饼图来说明,无需滚动它,属于每个状态 和 的百分比计数。此外,在饼图的中心,我希望它显示 "good" 类别中的百分比。
这是我目前拥有的代码。它的百分比在不滚动的情况下可见,但不是计数,而且中间没有百分比。
library(plotly)
p <- testfile %>%
group_by(status) %>%
summarize(count = n()) %>%
plot_ly(labels = ~status, values = ~count) %>%
add_pie(hole = 0.6) %>%
layout(title = "Ratio of Good to Bad", showlegend = F,
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = TRUE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = TRUE))
此外,如果您能展示如何按部门 facet_wrap 它,那将非常有帮助。我一直让它说 NULL!
谢谢!
如果您想在 pie/donut 图表的中央添加文本,您可以添加 annotation。
values <- testfile %>%
group_by(status) %>%
summarize(count = n())
good <- values %>% filter(status == 'good')
p <- layout(p, annotations=list(text=paste(good$count / sum(values$count) * 100, "%", sep=""), "showarrow"=F))
要更改饼图每个部分中显示的标签,您可以使用 text
。
p <- plot_ly(values, labels = ~status, values = ~count, text = ~count)
完整代码
library(dplyr)
library(plotly)
testfile <- tibble(personID = 1:10,
status = c("bad", "good", "bad", "bad", "bad", "bad", "bad", "bad", "bad", "good"),
department = c("sales", "sales", "marketing", "sales", "marketing", "management", "management", "sales", "sales", "sales"))
values <- testfile %>%
group_by(status) %>%
summarize(count = n())
good <- values %>% filter(status == 'good')
p <- plot_ly(values, labels = ~status, values = ~count, text = ~count) %>%
add_pie(hole = 0.6) %>%
layout(title = "Ratio of Good to Bad", showlegend = F,
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = TRUE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = TRUE))
p <- layout(p, annotations=list(text=paste(good$count / sum(values$count) * 100, "%", sep=""), "showarrow"=F))
p
我正在尝试使用 plotly 在 R 中制作圆环图。我试过 ggplot,但它无法提供我需要的效果。这是一个示例数据集:
library(dplyr)
testfile <- tibble(personID = 1:10,
status = c("bad", "good", "bad", "bad", "bad", "bad", "bad", "bad", "bad", "good"),
department = c("sales", "sales", "marketing", "sales", "marketing", "management", "management", "sales", "sales", "sales"))
此图表最终会出现在 PowerPoint 中,因此不需要响应式。相反,我需要饼图来说明,无需滚动它,属于每个状态 和 的百分比计数。此外,在饼图的中心,我希望它显示 "good" 类别中的百分比。
这是我目前拥有的代码。它的百分比在不滚动的情况下可见,但不是计数,而且中间没有百分比。
library(plotly)
p <- testfile %>%
group_by(status) %>%
summarize(count = n()) %>%
plot_ly(labels = ~status, values = ~count) %>%
add_pie(hole = 0.6) %>%
layout(title = "Ratio of Good to Bad", showlegend = F,
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = TRUE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = TRUE))
此外,如果您能展示如何按部门 facet_wrap 它,那将非常有帮助。我一直让它说 NULL!
谢谢!
如果您想在 pie/donut 图表的中央添加文本,您可以添加 annotation。
values <- testfile %>%
group_by(status) %>%
summarize(count = n())
good <- values %>% filter(status == 'good')
p <- layout(p, annotations=list(text=paste(good$count / sum(values$count) * 100, "%", sep=""), "showarrow"=F))
要更改饼图每个部分中显示的标签,您可以使用 text
。
p <- plot_ly(values, labels = ~status, values = ~count, text = ~count)
完整代码
library(dplyr)
library(plotly)
testfile <- tibble(personID = 1:10,
status = c("bad", "good", "bad", "bad", "bad", "bad", "bad", "bad", "bad", "good"),
department = c("sales", "sales", "marketing", "sales", "marketing", "management", "management", "sales", "sales", "sales"))
values <- testfile %>%
group_by(status) %>%
summarize(count = n())
good <- values %>% filter(status == 'good')
p <- plot_ly(values, labels = ~status, values = ~count, text = ~count) %>%
add_pie(hole = 0.6) %>%
layout(title = "Ratio of Good to Bad", showlegend = F,
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = TRUE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = TRUE))
p <- layout(p, annotations=list(text=paste(good$count / sum(values$count) * 100, "%", sep=""), "showarrow"=F))
p