How to make a custom hoverinfo lables for Plotly boxplot?
Asked Answered
G

2

6

Basically I have a problem with the current version of Plotly which doesn't display hoverinfo for box plot correctly. It omits labels (min, max, median etc.), so when I plot the following plot:

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

box plot without median, min and max labels

I don't have the necessary labels.

Is there a way for me to give custom hover labels so they go like this Max: 1.97, q3: 0.84, Median: 0.25 etc.?

My Plotly version is ‘4.7.1’

Genuine answered 27/11, 2017 at 11:2 Comment(3)
I don't know if 7 days is enough for the next Plotly release: github.com/plotly/plotly.js/pull/2094Macroscopic
Not sure, but since you added the Javascript tag, have you considered D3 ( d3js.org ). That's the way to be able to fully customize everything... but you need a job worth the effort.Barling
@MikeWilliamson yeah, I put the javascript tag because I was hoping there was a simple javascript fix. I hope they fix it in the next version.Genuine
A
1

Installing the latest dev version from github solves the problem, see issue #1160 on github

Ashelman answered 31/1, 2018 at 0:15 Comment(1)
Thanks. It does. Hope the official release comes soon.Genuine
A
2

Here is an example using ggplot2, that you can map through to plotly.

I hope it helps point you in the right direction. The latest version of plotly and ggplot2 do now display hover values. My approach has been to create text labels as this allows me to roll into a template function that I can use.

T.


Graphics Output (ggplot2)

enter image description here

Graphics Output (plotly)

enter image description here

Code Example

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

Note: The code above is an evolution from a base graphics package boxplot example in:

  • Data Analysis and Graphics Using R, Third Edition By: John Maindonald; W. John Braun

the book covers the base package in great detail, it was published in 2010, still a great source of insight.

Antimere answered 9/12, 2017 at 18:9 Comment(0)
A
1

Installing the latest dev version from github solves the problem, see issue #1160 on github

Ashelman answered 31/1, 2018 at 0:15 Comment(1)
Thanks. It does. Hope the official release comes soon.Genuine

© 2022 - 2024 — McMap. All rights reserved.