Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a custom hoverinfo lables for Plotly boxplot?

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’

like image 992
magasr Avatar asked Nov 27 '17 11:11

magasr


2 Answers

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.

like image 121
Technophobe01 Avatar answered Oct 25 '22 14:10

Technophobe01


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

like image 23
Bernhard Klingenberg Avatar answered Oct 25 '22 12:10

Bernhard Klingenberg