隐藏标示甜甜圈图表r

问题描述:

我正在一个Shiny应用程序中绘制甜甜圈图表。切片取决于所选变量,有时太小。在这种情况下,标签显示在图表之外,如下图所示。隐藏标示甜甜圈图表r

enter image description here

有没有一种办法可以完全隐藏在图表中的所有标签(用%符号值),只允许悬停动作,以显示细节?

的圆环图的可重复的代码如下:

library(plotly) 
library(tidyr) 
library(dplyr) 
# Get Manufacturer 
mtcars$manuf <- sapply(strsplit(rownames(mtcars), " "), "[[", 1) 

p <- mtcars %>% 
    group_by(manuf) %>% 
    summarize(count = n()) %>% 
    plot_ly(labels = ~manuf, values = ~count) %>% 
    add_pie(hole = 0.6) %>% 
    layout(title = "Donut charts using Plotly", showlegend = F, 
     xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE), 
     yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE)) 

p 

您可以设置textinfo='none'获得其在馅饼元素没有文字,但显示了盘旋信息如下甜甜圈情节。

enter image description here

您可以控制使用textinfohoverinfo属性的plotly饼图中所示。其中一个解决问题的方法是设置textinfo = "none"hoverinfo = "text"同时指定text = ~manuf为:

library(plotly) 
library(tidyr) 
library(dplyr) 
# Get Manufacturer 
mtcars$manuf <- sapply(strsplit(rownames(mtcars), " "), "[[", 1) 

p <- mtcars %>% 
    group_by(manuf) %>% 
    summarize(count = n()) %>% 
    plot_ly(text = ~manuf, values = ~count, textinfo = "none", hoverinfo = "text") %>% 
    add_pie(hole = 0.6) %>% 
    layout(title = "Donut charts using Plotly", showlegend = F, 
     xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE), 
     yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE)) 

p 

您还可以自定义显示在悬停粘贴字符串的任意组合与<br>分隔符的文本,比如:

plot_ly(text = ~paste("Manuf.: ", manuf , "<br> Number: ", count) , values = ~count, textinfo = "none", hoverinfo = "text") %>% 

enter image description here