我试图弄清楚如何更改饼图中标签的文本位置或使其更具可读性(?)。 下面是示例和可复制代码的屏幕截图:
df <- data.frame(Gruppierung = c("Very long label", "Very very long label", "Long label", "Short label", "Long label / long label"),
freq = c(3.1, 6.2, 8.7, 20, 21))
library(plotly)
plot_ly(df, labels=~Gruppierung,values=~freq, marker = list(line = list(color = '#FFFFFF', width = 1)), type="pie",
textposition = 'auto',textinfo = 'text',
hoverinfo = 'text',source = "subset",
text=~paste(sub(" ","<br>",Gruppierung),":","<br>",paste0(freq,"%") ),
insidetextfont = list(color = '#FFFFFF')) %>%
layout(showlegend = FALSE,separators = ',.') %>% config(displayModeBar = F)
我的问题(我想实现的目标):
如果Label无法水平匹配其所在的空间,则应将其放置在饼图的外部
感谢帮助!
[!!] @Saurabh Chauhan建议的方法非常好,但是每次都不会匹配,这是下面的屏幕截图,即使在某些频率较高的标签下,也可能显示问题不是水平的:
答案 0 :(得分:1)
最简单的方法是检查cmake
。如果freq
小于某个值,例如freq
,则标签应按如下所示放置在外部:
less than 5%
正在运行的演示的屏幕截图:
已更新
(plot_ly(df, labels=~Gruppierung,values=~freq, marker = list(line = list(color = '#FFFFFF', width = 1)), type="pie",
textposition = ifelse(df$freq<5,"outside","inside"),textinfo = 'text',
hoverinfo = 'text',source = "subset",
text=~paste(sub(" ","<br>",Gruppierung),":","<br>",paste0(freq,"%") ),
insidetextfont = list(color = '#FFFFFF')) %>%
layout(showlegend = FALSE,separators = ',.') %>% config(displayModeBar = F)
时)的另一个结果: