Edit labels in tooltip for plotly maps using ggplot2 in r

前端 未结 1 1539
半阙折子戏
半阙折子戏 2020-12-28 23:21

I know this question has been asked a number of times but I think some of the underlying syntax for plotly has changed since those questions have been asked. Using ggp

相关标签:
1条回答
  • 2020-12-29 00:13

    I am new to plotly too but have come across a similar problem for my ggplot2 bubble plots when using ggplotly(). I have finally found a solution that works for me and thought it might help you, too, although I haven't tried it for choropleth maps.

    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

    This solution worked (in principle) for me using R 3.1.1 and plotly 3.4.13

    0 讨论(0)
提交回复
热议问题