2017-03-15 116 views
0

我正在一個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 

回答

1

您可以設置textinfo='none'獲得其在餡餅元素沒有文字,但顯示了盤旋信息如下甜甜圈情節。

enter image description here

1

您可以控制使用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