【问题标题】:Open Pie Chart/Donut Chart in R using Plotly with count and percentage使用带有计数和百分比的 Plotly 在 R 中打开饼图/甜甜圈图
【发布时间】:2019-10-25 02:48:22
【问题描述】:

我正在尝试使用 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 中,因此它不需要响应式。相反,我需要饼图在不滚动的情况下说出属于每个状态的百分比计数。此外,在饼图的中心,我希望它显示“好”类别中的百分比。

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

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!

谢谢!

【问题讨论】:

    标签: r charts count plotly donut-chart


    【解决方案1】:

    如果您想在饼图/甜甜圈图的中心添加文字,可以添加 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
    

    【讨论】:

    • @Maximillian,首先,谢谢!第二,最后一个问题,如何更改字体大小?它尝试在注释中添加 size = 25 ,但它一直产生相同的大小。另外,您知道我将如何按部门对这些结果进行 facet_wrap 吗?再次感谢您!
    • @Maximillian,我尝试查看这些链接,但我的编辑似乎仍然不起作用。对于中心文本,我按照说明使用: p
    • 我认为应该是font=list(size=18)。如果没有,请打开一个单独的问题。 Stackoverflow 建议每个帖子只有一个问题。
    • 感谢您的建议。它确实有效,但不幸的是它覆盖了旧字体的“新”字体。我会采纳您的建议并开一张新票——再次感谢您的宝贵时间!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-13
    相关资源
    最近更新 更多