简体   繁体   中英

Store plotly tooltip information outside of ggplot object

Is it possible to store plotly tooltip information outside of a ggplot object? For example, I would like to go from this:

library(tidyverse)
library(plotly)

p1 <- mtcars %>% 
  ggplot(aes(x = mpg, y = hp, text = paste0("<b>MPG: </b>", mpg, "<br>",
                                            "<b>HP: </b>", hp))) +
  geom_point()

ggplotly(p1, tooltip = "text")

To this:

tooltip_text <- paste0("<b>MPG: </b>", mpg, "<br>",
                       "<b>HP: </b>", hp)

p1 <- mtcars %>% 
  ggplot(aes(x = mpg, y = hp, text = tooltip_text)) +
  geom_point()

ggplotly(p1, tooltip = "text")

Using quote and the bang-bang operator !! to unquote:

tooltip_text <- quote(paste0("<b>MPG: </b>", mpg, "<br>",
                         "<b>HP: </b>", hp))

p1 <- mtcars %>% 
  ggplot(aes(x = mpg, y = hp, text = !! tooltip_text)) +
  geom_point()

ggplotly(p1, tooltip = "text")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM