繁体   English   中英

如何为Plotly boxplot制作自定义的hoverinfo标签?

[英]How to make a custom hoverinfo lables for Plotly boxplot?

基本上我对当前版本的Plotly有一个问题,它没有正确显示盒子图的hoverinfo。 它省略了标签(最小值,最大值,中位数等),因此当我绘制下面的图时:

plot_ly(y = ~rnorm(50), type = "box")

没有中位数,最小和最大标签的箱形图

我没有必要的标签。

有没有办法让我给自定义悬停标签,所以他们像这样Max:1.97,q3:0.84,中位数:0.25等?

我的Plotly版本是'4.7.1'

以下是使用ggplot2的示例,您可以将其映射到plotly。

我希望它有助于指出你正确的方向。 最新版本的plotly和ggplot2现在可以显示悬停值。 我的方法是创建文本标签,因为这允许我滚动到我可以使用的模板功能。

T.


图形输出(ggplot2)

在此输入图像描述

图形输出(图示)

在此输入图像描述

代码示例

require(DAAG)
require(ggplot2)
require(plotly)
data("possum")

dset <- possum
here <- possum$sex == "f"
dname <- as.character(substitute(possum))
xnam <- as.character(substitute(x))
x <- dset[here, "totlngth"]

yLabel <- c("Total length (cm)")

## Pull in boxplot stats for use in mapping data later to boxplot
z <- boxplot.stats(x)
xlim <- range(c(z$stats, z$out))
xlim <- xlim + c(-0.025, 0.05) * diff(xlim)
ylim <- c(0.55, 1.5)

top <- 0.7
chh <- par()$cxy[2]
chw <- par()$cxy[1]

gp <- ggplot(data = possum, aes(y = totlngth, x = ""))
gp <- gp + stat_boxplot(geom = 'errorbar', width = .1)
gp <- gp + geom_boxplot(#width = .3,
                        outlier.color = "blue",
                        outlier.shape = 2)
gp <- gp + stat_summary(fun.y = mean,
                        geom = "point",
                        shape = 5,
                        size = 4)
gp <- gp + xlab(NULL)
gp <- gp + ylab(yLabel)
gp <- gp + theme(axis.ticks.x = element_blank(),
                 panel.grid.major = element_blank(),
                 panel.grid.minor = element_blank(),
                 panel.background = element_blank())

gp <- gp + geom_text(data = data.frame(), aes(x = top + 1.5 * chh,
                                            y = z$stats[5],
                                            label = "Largest value \n(there are no outliers)"
                                            ))

gp <- gp + geom_text(data = data.frame(), aes(x = top + 1.5 * chh,
                                              y = z$stats[4],
                                              label = "upper quartile"
                                              ))

gp <- gp + geom_text(data = data.frame(), aes(x = top + 1.5 * chh,
                                              y = z$stats[3],
                                              label = "median"
                                              ))

gp <- gp + geom_text(data = data.frame(), aes(x = top + 1.5 * chh,
                                              y = z$stats[2],
                                              label = "lower quartile"
                                              ))

gp <- gp + geom_text(data = data.frame(), aes(x = top + 1.5 * chh,
                                              y = z$stats[1],
                                              label = "Smallest value \n(outliers excepted)"
                                              ))
if (!is.null(z$out)) {
  gp <- gp + geom_text(data = data.frame(), aes(x = top + 1.5 * chh,
                                              y = z$out[1],
                                              label = "Outlier \n"
                                              ))
  # Display outlier
  gp <- gp + geom_text(data = data.frame(), aes( x = rep(top - chh, 2),
                                                 y = z$out[1] + .5,
                                                 label = c(format(round(z$out[1], 2)))))

}

av <- mean(z$stats[c(2, 4)])
q1 <- z$stats[2]
q3 <- z$stats[4]
qtop <- q3 + 0.5 * chh


# Largest Value
gp <- gp + geom_text(data = data.frame(), aes( x = rep(top - chh, 2),
                                               y = z$stats[5],
                                               label = c(format(round(z$stats[5], 2)))))


# Upper Quartile
gp <- gp + geom_text(data = data.frame(), aes( x = rep(top - chh, 2),
                                               y = q1,
                                               label = c(format(round(q1, 2)))))

# Lower Quartile
gp <- gp + geom_text(data = data.frame(), aes( x = rep(top - chh, 2),
                                               y = q3,
                                               label = c(format(round(q3, 2)))))

gp

p <- ggplotly(gp)
p

注意:上面的代码是从基础图形包boxplot示例演变而来的:

  • 数据分析和图形使用R,第三版作者:John Maindonald; W.约翰布劳恩

这本书非常详细地介绍了基础包,它于2010年出版,仍然是一个很好的见解来源。

从github安装最新的dev版本解决了这个问题,请参阅github上的问题#1160

暂无
暂无

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

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