2

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!

J.Sabree
  • 2,280
  • 19
  • 48

1 Answers1

6

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)

enter image description here

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
Maximilian Peters
  • 30,348
  • 12
  • 86
  • 99
  • @Maximillian, first, THANK YOU! Second, one last question, how do I change the font size? It tried adding size = 25 in the annotations, but it keeps yielding the same size. Also, do you know how I would facet_wrap these results by department? Thank you again! – J.Sabree Jun 10 '19 at 18:07
  • For the center text: https://plot.ly/r/reference/#layout-annotations-items-annotation-font. For the labels: https://plot.ly/r/reference/#pie-textfont – Maximilian Peters Jun 10 '19 at 18:25
  • @Maximillian, I tried looking on those links, but my edits still seem not to work. For the center text, I followed the instructions and use: p <- layout(p, annotations=list(text=paste(good$count / sum(values$count) * 100, "%", sep=""), "showarrow"=F, size = 25)) but that did nothing. Edit: It works for the labels when I view it outside the viewer. It still does not work for the center text. – J.Sabree Jun 10 '19 at 18:55
  • I think it should be `font=list(size=18)`. If not, please open a separate question. Stackoverflow suggests to have only one question per post. – Maximilian Peters Jun 10 '19 at 19:01
  • 1
    thanks for the suggestion. It does work, but unfortunately it overlays the "new" font over the old. I'll take your suggestion and open a new ticket--once again I appreciate your time! – J.Sabree Jun 10 '19 at 19:13
  • Link to new question in case future users would benefit: https://stackoverflow.com/questions/56532527/customizing-text-text-size-text-color-text-angle-in-plotly-for-r – J.Sabree Jun 10 '19 at 19:36