问题描述
我正在Plotly中制作甜甜圈图/开放式饼图.由于此问题的帮助,,我的图能够走得很远.但是,现在我需要自定义文本大小,角度和颜色的帮助.
I'm working on making a donut chart/open pie chart in Plotly. Because of help from this question, Open Pie Chart/Donut Chart in R using Plotly with count and percentage, I was able to get fairly far with my plot. However, now I need help customizing the text size, angle, and color.
这是数据集:
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"))
这是到目前为止绘制剧情的代码.它具有每种状态的计数和百分比,并且在中心具有良好"状态的个体的百分比.
Here's the code to make the plot thus far. It has the count and percentage for each status, and in the center, it has the percentage of "good" status individuals.
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文件时,它看起来是第二种方式(非常错误).
This code yields these two plots. Surprisingly, it looks the first way when I click "zoom" on R Studio, but it looks the second (very wrong) way when I click export and open the PNG file.
放大的图(大部分正确)
Zoomed plot (mostly correct)
导出的情节(看起来与应有的方式非常不同...)
Exported plot (looks very different than how it should...)
这个放大的图完全是我想要的,除了三个问题:
This zoomed in plot is exactly what I want except for three issues:
-
我希望中心文本(20%)更大.我试过了p<-layout(p,注解=列表(text = paste(good $ count/sum(values $ count)* 100,%",sep ="),"showarrow" = F,大小= 25)),但没有执行任何操作.
I want the center text (20%) to be larger. I've tried p <- layout(p, annotations=list(text=paste(good$count / sum(values$count) * 100, "%", sep=""), "showarrow"=F, size = 25)) but that did nothing.
我希望两个标签(2个20%和8个80%)都为白色,而不仅仅是8个80%.我以为color = I("white")可以解决问题,但是输出与我完全不指定颜色时相同.
I want both of the labels (2 20% and 8 80%) to be white, not just the 8 80%. I thought color = I("white") would do the trick, but the output is the same as when I don't specify the color at all.
我希望标签是水平的而不是弯曲的,以使其适合条形图.
I want the labels to be horizontal instead of curving and for them to fit within the bars.
可选:为什么导出与放大时看起来不一样?我需要它看起来像放大版本.另外,还有一种简单的方法让它说n = 2而不是2吗?
Optional: Why does it look different when it is exported vs zoomed in? I need it to look like the zoomed in version. Also, is there also an easy way to get it to say n = 2 instead of just 2?
谢谢!
这段代码使标签水平放置,但是它们太小了.当我添加size = 15或任何数字时,它总是使字体具有相同的大字体,但不再是水平字体.这也解决了导出和缩放不一致的问题.它还固定了中心文字的大小.我只需要标签既是白色的,较大的又是水平的.
This code makes the labels horizontal, but they are too small. When I add size = 15 or any number, it always makes the font the same large size but no longer horizontal. This also fixes the issue where the export and zoom are inconsistent. It also fixes the center text size. I just need the labels to both be white, larger in size, and horizontal.
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
推荐答案
更新,我破解了代码!这是有效的代码
Update, I cracked the code! Here's the code that worked
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
这是情节现在的样子:
这篇关于在R的Plotly中自定义文本(文本大小,文本颜色,文本角度)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!