Home > OS >  ggplotly hover text doesn't work with geom_dotplot
ggplotly hover text doesn't work with geom_dotplot

Time:08-24

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

CodePudding user response:

According to the enter image description here

CodePudding user response:

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.)

  • Related