我正试着用圆圈圈图来巧妙地使用R。我试过格格图,但它不能给我我所需要的效果。下面是一个示例数据集:
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,这将非常有帮助。我一直让它说空!
谢谢!
发布于 2019-06-10 09:54:47
如果您希望在饼/甜甜圈图的中间有一个文本,您可以添加一个注解。
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
https://stackoverflow.com/questions/56530842
复制