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

标签 javascript r plotly boxplot r-plotly

基本上,当前版本的 Plotly 存在问题,它无法正确显示箱线图的悬停信息。它省略了标签(最小值、最大值、中值等),所以当我绘制以下图时:

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

box plot without median, min and max labels

我没有必要的标签。

有没有办法让我提供自定义悬停标签,使它们像这样最大:1.97,q3:0.84,中位数:0.25 等?

我的 Plotly 版本是“4.7.1”

最佳答案

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

希望它能帮助您指明正确的方向。最新版本的 plotly 和 ggplot2 现在可以显示悬停值。我的方法是创建文本标签,因为这允许我进入我可以使用的模板函数。

T.


图形输出(ggplot2)

enter image description here

图形输出(绘图)

enter image description here

代码示例

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 年出版,仍然是一个很好的见解来源。

关于javascript - 如何为 Plotly boxplot 制作自定义 hoverinfo 标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47509383/

相关文章:

Javascript 文件链接无法正常工作

javascript - 使用 jQuery 更改显示的项目数

r - Xgboost h2o 在 WIndows 操作系统上出现错误?

python - Plotly:如何修改直方图的悬停模板?

javascript - jQuery 无法将 Sortable 与同位素一起使用

javascript - 如何在 Javascript 中解析以下 JSON?

r - MonetDB 是否支持> 70k 列的表

r - 忽略 R 函数中的错误 - try() 函数不起作用

python - 修复 y 轴的比例/大小

javascript - 可以巧妙地使用动态 html 表作为源数据吗?