我正在Plotly中制作甜甜圈图/开放式饼图。由于这个问题Open Pie Chart/Donut Chart in R using Plotly with count and percentage的帮助,我的情节变得相当遥远。但是,现在我需要自定义文本大小,角度和颜色的帮助。
这是数据集:
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"))
这是到目前为止绘制剧情的代码。它具有每种状态的计数和百分比,并且在中心具有“良好”状态的个体的百分比。
library(plotly)
plot <- plot_ly(values, labels = ~status, values = ~count, text = ~count, color = I("white")) %>%
add_pie(hole = 0.6) %>%
layout(title = "Ratio of Good to Bad", showlegend = F, position = "topcenter",
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = TRUE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = TRUE))
plot <- layout(p, annotations=list(text=paste(good$count / sum(values$count) * 100, "%", sep=""), "showarrow"=F))
plot
此代码产生这两个图。令人惊讶的是,当我单击R Studio上的“缩放”时,它看起来是第一种方式,但是当我单击“导出”并打开PNG文件时,它看起来是第二种(非常错误)。
这个放大的图完全是我想要的,除了三个问题:
我希望中心文本(20%)更大。我试过p <-layout(p,注解=列表(text = paste(good $ count / sum(values $ count)* 100,“%”,sep =“”),“ showarrow” = F,size = 25)),但是什么也没做。
我希望两个标签(2个20%和8个80%)都是白色,而不仅仅是8个80%。我以为color = I(“ white”)可以解决问题,但是输出与我完全不指定颜色时相同。
我希望标签是水平的而不是弯曲的,以使其适合条形图。
可选:为什么导出与放大时看起来不一样?我需要它看起来像放大版本。另外,还有一种简单的方法让它说n = 2而不是2吗?
谢谢!
编辑:此代码使标签水平放置,但它们太小。当我添加size = 15或任何数字时,它总是使字体大小相同,但不再水平。这也解决了导出和缩放不一致的问题。它还固定了中心文本的大小。我只需要标签既是白色的,较大的又是水平的。
plot <- plot_ly(values, labels = ~status, values = ~count, text = ~count) %>%
add_pie(hole = 0.6) %>%
layout(title = "Ratio of Good to Bad", showlegend = F, position = "topcenter",
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = TRUE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = TRUE),
annotations=list(text=paste(good$count / sum(values$count) * 100, "%", sep=""), "showarrow"=F, font=list(size = 40)))
plot
答案 0 :(得分:0)
更新,我破解了代码!这是有效的代码
library(plotly)
library(dplyr)
values <- testfile %>%
group_by(status) %>%
summarize(count = n())
t <- list(size = 14, color = "white")
good <- values %>% filter(status == 'good')
plot <- plot_ly(values, labels = ~status, values = ~count, text = ~count, textfont = list(color = "white", size = 40)) %>%
add_pie(hole = 0.6) %>%
layout(title = "Ratio of Good to Bad", showlegend = TRUE,
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = TRUE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = TRUE),
annotations=list(text=paste(good$count / sum(values$count) * 100, "%", sep=""), "showarrow"=F, font=list(size = 180, color = "black")))
plot
这是情节现在的样子: