0

Using an example from"https://plot.ly/r/"

library(plotly)

d <- diamonds[sample(nrow(diamonds), 1000), ]

p <- ggplot(data = d, aes(x = carat, y = price)) +
geom_point(aes(text = paste("Clarity:", clarity)), size = 4) +
geom_smooth(aes(colour = cut, fill = cut)) + facet_wrap(~ cut)

I am wondering how I could make plotly to show additonal (customized) information on the different facet labels "Fair", "Good", "Premium" and "Ideal" when moving the mouse over them.

Patrick Balada
  • 1,330
  • 1
  • 18
  • 37

1 Answers1

0

You dont really have a plotly object there. You have to wrap it into a ggplotly call. You can define tooltips in the geom_point text argument.

Is this what you are looking for?

library(plotly)

d <- diamonds[sample(nrow(diamonds), 1000), ]

p <- ggplot(data = d, aes(x = carat, y = price)) +
  geom_point(aes(text = paste(cut, " <br> Clarity:", clarity)), size = 4) +
  geom_smooth(aes(colour = cut, fill = cut)) + facet_wrap(~ cut)

ggplotly(p, tooltip = "text")
SeGa
  • 9,454
  • 3
  • 31
  • 70
  • @ SeGa Thank you for your quick reply and the clarification concering plotly/ggplotly. Maybe this wasn't clearly highlighted in the text anymore but I am looking for a way to show additional text when hovering over the titles and not the observations/data points. – Patrick Balada Dec 20 '18 at 16:34
  • So no matter where you hover on a facet, it should always show the "cut"? Maybe you can achieve this with `htmlwidgets::onRender()`. Not to sure about it though. – SeGa Dec 20 '18 at 17:00
  • It does not necessarily need to be everywhere - only the titles would actually be even better. Thanks for the hint, I will have a look. – Patrick Balada Dec 20 '18 at 17:06
  • With tiles you mean the facets or? – SeGa Dec 20 '18 at 17:08
  • Yes, sorry. That was wrong. Facet labels would be the correct term I guess. – Patrick Balada Dec 20 '18 at 17:10
  • I tried to use `geom_rect` or `geom_area` to create transparent rectangles over the whole facets, but it is only showing the labels, when hovering above the corners. – SeGa Dec 21 '18 at 12:54