Reed Cooley
Reed Cooley

Reputation: 31

ggplotly hover text doesn't work with geom_dotplot

I would like to add hover text annotations to a dot plot in R. But ggplotly doesn't seem to work with geom_dotplot.

I would like the final dotplot to look like something this:

df <- data.frame(year = c("2000", "2005", "2005", "2010", "2010", "2010"), name = c("George", "Michael", "Bob", "James", "Will", "Fabrizio"))

ggplot(df, aes(x = year)) + geom_dotplot(, stackratio = 1, fill = "darkgreen", stackgroups = TRUE) enter image description here

But then I want the "name" data to appear when you hover on a given dot.

The following code works with other graphs (geom_point, for example), but with geom_dotplot the look changes entirely, and the hover text doesn't work:

ggplotly(ggplot(df, aes(x = year, text = paste(name))) + geom_dotplot(, stackratio = 1, fill = "darkgreen", stackgroups = TRUE, binpositions="all"), tooltip = "text") enter image description here

Any help with this would be much appreciated! Thank you.

Reed

Upvotes: 1

Views: 225

Answers (2)

Reed Cooley
Reed Cooley

Reputation: 31

If hover text is not available with for geom_dotplot, then another solution would be to make a scatter plot that looks like a dot plot:

df <- data.frame(
    year = c("2000", "2005", "2005", "2010", "2010", "2010"),
    name = c("George", "Michael", "Bob", "James", "Will", "Fabrizio"),
    position = c("1", "1", "2", "1", "2", "3"))

p <- ggplot(df, aes(x = year, y = position,  text = paste(name)))
     + geom_point(colour = "darkgreen", alpha = .3, size = 5)
     + scale_y_continuous(limits = c(0, 10))
     + theme(axis.text.y=element_blank(),axis.ticks.y=element_blank(), 
     axis.title.y = element_blank())

ggplotly(p, tooltip = "text")

enter image description here

(The hover text doesn't work in the StackOverflow preview but it does in R Studio.)

Upvotes: 0

Quinten
Quinten

Reputation: 41225

According to the documentation it seems not possible to create hover texts with geom_dotplot in ggplotly. When you check this documenation, you can see that none of the options given hovers the labels. Even when using label or text in your aes with tooltip seems not working.

As an option, maybe you want to use fill to show the names like this:

library(ggplot2)
library(plotly)
p <- ggplot(df, aes(x = year, fill = name)) + 
  geom_dotplot(stackratio = 1, stackgroups = TRUE)

ggplotly(p)

Output:

enter image description here

Upvotes: 0

Related Questions