我正在制作一个Shiny应用程序,我正在绘制一个圆环图。切片取决于所选的变量,有时太小。在这种情况下,标签会显示在图表外部,如下图所示。
有没有办法在图表中隐藏所有标签(带%符号的值),只允许悬停操作显示详细信息?
圆环图的可重现代码如下:
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
答案 0 :(得分:2)
您可以设置textinfo='none'
以获取以下圆环图,其中饼图元素中没有文字但显示有关悬停的信息。
答案 1 :(得分:1)
您可以使用textinfo
和hoverinfo
属性控制绘制饼图中显示的内容。问题的一个解决方案是设置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") %>%