关于javascript:如何为Plotly boxplot创建自定义hoverinfo标签?

How to make a custom hoverinfo lables for Plotly boxplot?

基本上,我对当前版本的Plotly有问题,该版本无法正确显示箱形图的hoverinfo。 它省略了标签(最小值,最大值,中位数等),因此当我绘制以下图时:

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

box plot without median, min and max labels

我没有必要的标签。

有没有一种方法可以给我自定义悬停标签,以便它们像这样Max:1.97,q3:0.84,Median:0.25等?

我的情节版本是" 4.7.1"


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

我希望它能帮助您指出正确的方向。 现在,最新版本的plotly和ggplot2确实显示了悬停值。 我的方法一直是创建文本标签,因为这使我可以使用模板功能。

T.

图形输出(ggplot2)

enter image description here

图形输出(打印)

enter image description here

代码示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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 \
(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 \
(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 \
"

                                              ))
  # 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; 约翰·布劳恩

该书非常详细地介绍了基本软件包,该软件包于2010年出版,仍然是深入了解的来源。


从github安装最新的dev版本可以解决此问题,请参阅github上的问题#1160