使用带有计数和百分比的Plotly在R中打开饼图/甜甜圈图 [英] Open Pie Chart/Donut Chart in R using Plotly with count and percentage
问题描述
我正在尝试使用plotly在R中制作一个甜甜圈图.我尝试了ggplot,但无法满足我的需要.这是一个示例数据集:
I'm trying to make a donut chart in R using plotly. I tried ggplot, but it wasn't able to give me the effect that I need. Here's a sample dataset:
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中显示,因此不需要响应.取而代之的是,我需要饼图说而不滚动到每个状态和的百分比.另外,在饼图的中心,我要说的是好"类别中的百分比.
This chart will end up in a PowerPoint, so it does not need to be responsive. Instead, I need the pie chart to say, without scrolling over it, the % that falls into each status and the count. Also, in the center of the pie chart, I want it to say the % that are in the "good" category.
这是我到目前为止的代码.它具有可见的百分比(无需滚动),但没有计数,并且中心没有百分比.
This is the code that I have so far. It has the percentage visible without scrolling but not the count and it does not have the percentage in the center.
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!
Also, if you could show how to facet_wrap it by department, that would be very helpful. I keep getting it to say NULL!
谢谢!
推荐答案
If you want to have a text in the center of your pie/donut chart you can add an 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
.
In order to change the label which is shown in each segment of your pie chart, you can use 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中打开饼图/甜甜圈图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!