[英]Plotly JavaScript - Donut Chart - Hoverinfo, trying to add in custom definitions using array - Small issue with my code
[英]How to make a custom hoverinfo lables for Plotly boxplot?
以下是使用ggplot2的示例,您可以将其映射到plotly。
我希望它有助于指出你正确的方向。 最新版本的plotly和ggplot2现在可以显示悬停值。 我的方法是创建文本标签,因为这允许我滚动到我可以使用的模板功能。
T.
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示例演变而来的:
这本书非常详细地介绍了基础包,它于2010年出版,仍然是一个很好的见解来源。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.