简体   繁体   English

隐藏标签甜甜圈图表中的标签

[英]Hide labels in plotly donut chart r

I am working on a Shiny app where I am plotting a Donut chart. 我正在制作一个Shiny应用程序,我正在绘制一个圆环图。 The slices depend on the variable selected and sometimes are too small. 切片取决于所选的变量,有时太小。 In such cases the labels are displayed outside the chart like in the image below. 在这种情况下,标签会显示在图表外部,如下图所示。

在此输入图像描述

Is there a way to altogether hide all the labels (values with % sign) in the chart and only allow the hover action to show the details? 有没有办法在图表中隐藏所有标签(带有%符号的值),只允许悬停操作显示详细信息?

An reproducible code for a Donut Chart is as below: 圆环图的可重现代码如下:

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

You could set textinfo='none' to get the following donut plot which has no text in the pie elements but shows info on hovering. 您可以设置textinfo='none'以获取以下圆环图,其中饼图元素中没有文本但显示有关悬停的信息。

在此输入图像描述

You can control what is shown in a plotly pie chart using the textinfo and hoverinfo attributes. 您可以使用textinfohoverinfo属性控制绘制饼图中显示的textinfo One solution to your problem would be setting the textinfo = "none" and hoverinfo = "text" while specifying that text = ~manuf as in: 解决问题的一个方法是设置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

You can further customize the text that is shown on hover by pasting any combination of strings with <br> seperators, eg: 您可以通过使用<br>分隔符粘贴任何字符串组合来进一步自定义悬停时显示的文本,例如:

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

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM