使用 R 将悬停文本添加到箱线图中

Add hover text to boxplot in plolty using R

我已经遍历了一些示例,但无法修复悬停文本

这是一个最小的例子:

library(ggplot2)
library(plotly)

newhovertext=paste0(iris$Species,"<br>",iris$Sepal.Width)
g <- ggplot(iris,aes(x=Species,y=Sepal.Width,label=newhovertext)) + 
  geom_boxplot(alpha=0.5) 
 plotly::ggplotly(g,tooltip=label)

应用以上代码将产生下图:

我无法在悬停文本中显示植物名称,希望得到帮助

您可以执行以下操作:

library(ggplot2)
library(plotly)
library(datasets)
data(iris)

newhovertext=paste0(iris$Species,"<br>",iris$Sepal.Width)
g<-ggplot(iris,aes(x=Species,y=Sepal.Width, label=newhovertext))+
  geom_boxplot(alpha=0.5)
g %>% ggplotly()

然后它会在悬停时显示标签,对于更高级的解决方案,您应该考虑使用 shiny

您可以在以下位置看到结果图:http://rpubs.com/david_sriker/555513

您将看到其他值的不同解决方案:

gg_box <- iris %>%
  ggplot(aes(x=Species, y=Sepal.Width, text=paste("Species:",Species, "\n",
                                             "Width:", Sepal.Width))) +
  geom_boxplot()+

  #invisible layer of points
  geom_point(alpha = 0)

gg_box %>%
  ggplotly()

您可以在以下位置看到结果图:http://rpubs.com/david_sriker/555522