Formatting mouse over labels in plotly when using ggplotly
37,423
Solution 1
plotly
can make use of the line break HTML tag. You can get what your after using the <br>
tag for a newline:
g <- ggplot(df, aes(x,y)) +
geom_point(aes(text=sprintf("letter: %s<br>Letter: %s", a, b)))
(gg <- ggplotly(g))
Solution 2
See the tooltip
argument to ggplotly(). For instance, to show only the species name (e.g. virginica
for the top right point) on hover:
g <- ggplot(tail(iris), aes(Petal.Length, Sepal.Length, text=Species)) + geom_point()
ggplotly(g, tooltip="text")
Other examples:
ggplotly(g, tooltip="x") # Petal.Length: 5.7
ggplotly(g, tooltip="Petal.Length") # Petal.Length: 5.7
ggplotly(g, tooltip=c("x", "y"))
The last example will show the two-line tooltip
Petal.Length: 5.7
Sepal.Length: 6.7
Author by
drmariod
Updated on April 24, 2020Comments
-
drmariod about 4 years
I am struggling with text formatting when using
ggplotly
and the mouse over functionality.library(plotly) df <- data.frame(a=letters, b=LETTERS, x=runif(26), y=runif(26)) g <- ggplot(df, aes(x,y)) + geom_point(aes(text=sprintf('letter: %s\nLetter: %s', a, b))) g (gg <- ggplotly(g))
I would like to have some formatted text or at least a newline in my mouse over label. Is there a good documentation on how to design this mouse over bubble thing?
-
drmariod over 8 yearsAwesome, I didn't thought about that... This makes even so much more fun possible!!!
-
Nick over 6 yearsTooltip for the win.
-
Wakan Tanka over 6 years@drmariod this gives me an following error:
Error: (converted from warning) Ignoring unknown aesthetics: text
ggplot2 is in version 2.2.1 -
user1420372 about 6 yearsUsing this method in a line chart with multiple lines (differentiated by colour), I had to specifically add an aesthetic for group. i.e. geom_line(aes(x=x, y=y, text="", color=z, group=z)) without text the group=z is not necessary. I then used ggplotly(p, tooltip="text") as per answer by @Jon Olav Vik.
-
Piyush Verma about 6 yearsThanks, Jon. That answer helped to fix my issue. Have a nice day.
-
Andrew McCartney almost 4 yearsis it possible to do this for variables that are not included in the original plot? To hover over a data point and see some/all other variables for that point?
-
Jon Olav Vik almost 4 years@AndrewMcCartney That would be Species in my first example. The "text" aesthetic does not really exist and is not used by geom_point().
-
Alperen Taciroglu over 3 yearsFor my case I also needed geom_line() variation. Code below worked: myPlot <- ggplot(data = df, aes(x = x, y = y, label = a, text = b)) + geom_line(aes(x = x, y = y, text = "")) ggplotly(myPlot , tooltip="text")
-
Ricky over 2 years@JonOlavVik thank you super helpful. What if you want to add to the "text" aesthetic without overwriting it all-together?
-
Jon Olav Vik over 2 years@Ricky "text" is not really an aesthetic; perhaps you're thinking about "label"? You can use the label aesthetic yet have tooltips: ggplotly(ggplot(tail(iris), aes(Petal.Length, Sepal.Length, label=Species, text=Sepal.Length)) + geom_text(), tooltip="text")