隐藏标示甜甜圈图表r
问题描述:
我正在一个Shiny应用程序中绘制甜甜圈图表。切片取决于所选变量,有时太小。在这种情况下,标签显示在图表之外,如下图所示。隐藏标示甜甜圈图表r
有没有一种办法可以完全隐藏在图表中的所有标签(用%符号值),只允许悬停动作,以显示细节?
的圆环图的可重复的代码如下:
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
和hoverinfo
属性的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") %>%