Skip to content

Instantly share code, notes, and snippets.

@verajosemanuel
Last active January 31, 2021 13:45
Show Gist options
  • Save verajosemanuel/8ef1cd445a8f69926851295b15281656 to your computer and use it in GitHub Desktop.
Save verajosemanuel/8ef1cd445a8f69926851295b15281656 to your computer and use it in GitHub Desktop.
Add #tooltip content to #plotly
# Your first question was to customize the tooltip so it displays
# some of the variables in the dataset (including those not mapped to aesthetics).
# In your UPDATE#3 you introduce:text = paste("Province:", NAME_1) into your aes.
# If you want to add a second line of custom variables or text, just keep adding it into the
# brackets:text = paste("Province:", NAME_1, "Example III:", example1)
#
# To add a line break between both add <br> in the spot where you want the break to be, like:
text = paste("Province:", NAME_1, "<br>", "Example III:", example1)
# Your second question was to customize the tooltip so it does NOT display
# other (default) variables (that are mapped to aesthetics, such as the coordinates).
# I found this very easy addition to the ggplotly() function that did the trick for me:
ggplotly(gg, tooltip = c("text"))
# In my case, this removed ALL default variables that are shown in the tooltip and only
# showed those that are custom specified with text above. You can add other variables back
# in by doing ggplotly(gg, tooltip = c("text","x")) The order of the variables shown in
# the tooltip will be the same as the order specified in the tooltip argument.
# I found this documented here: https://github.com/ropensci/plotly/blob/master/R/ggplotly.R
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment