使用带有计数和百分比的 Plotly 在 R 中打开饼图/甜甜圈图 [英] Open Pie Chart/Donut Chart in R using Plotly with count and percentage

查看:18
本文介绍了使用带有计数和百分比的 Plotly 在 R 中打开饼图/甜甜圈图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 plotly 在 R 中制作圆环图.我尝试了 ggplot,但它无法给我所需的效果.这是一个示例数据集:

库(dplyr)测试文件 <- tibble(personID = 1:10,状态= c(坏",好",坏",坏",坏",坏",坏",坏",坏",好"),部门= c(销售",销售",营销",销售",营销",管理",管理",销售",销售",销售"))

此图表最终会出现在 PowerPoint 中,因此它不需要响应式.相反,我需要饼图在不滚动的情况下说出属于每个状态的百分比计数.此外,在饼图的中心,我希望它显示好"类别中的百分比.

这是我到目前为止的代码.它具有不滚动可见的百分比,但没有计数,并且在中心没有百分比.

图书馆(情节)p <- 测试文件 %>%group_by(状态) %>%总结(计数= n())%>%plot_ly(labels = ~status, values = ~count) %>%add_pie(孔 = 0.6) %>%layout(title = "好坏比率", showlegend = F,xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = TRUE),yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = TRUE))

另外,如果您可以展示如何按部门进行 facet_wrap,那将非常有帮助.我一直在说 NULL!

谢谢!

解决方案

如果你想在饼图/甜甜圈图的中心添加一个文本,你可以添加一个

完整代码

库(dplyr)图书馆(情节)测试文件 <- tibble(personID = 1:10,状态= c(坏",好",坏",坏",坏",坏",坏",坏",坏",好"),部门= c(销售",销售",营销",销售",营销",管理",管理",销售",销售",销售"))值 <- 测试文件 %>%group_by(状态) %>%总结(计数 = n())好 <- 值 %>% 过滤器(状态 == '好')p <- plot_ly(值,标签=〜状态,值=〜计数,文本=〜计数)%>%add_pie(孔 = 0.6) %>%layout(title = "好坏比率", 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

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"))

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))

Also, if you could show how to facet_wrap it by department, that would be very helpful. I keep getting it to say NULL!

Thank you!

解决方案

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))

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)

Complete code

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆