I'm trying to generate a interactive plot with ggplotly
package.
I generated a plot with ggplot
and use ggplotly
function to make it interactive. I wonder how I can add more info to the tooltip.
Here is the example:
df=data.frame(id=rep(1:5, each=2),
grp = rep(1:2, each=5),
grp2 = rep(c('A','B'), 5),
grp3 = LETTERS[1:10],
value1 = runif(10),
value2 = rnorm(10))
g=df %>%
ggplot(aes(x=grp3, y=value1, text=id))+
geom_bar(stat = 'identity')
ggplotly(g, tooltip = c('grp3', 'value1','id', 'value2', 'grp1'))
The tooltip only shows grp3
, value1
and id
.
My question is:
1: How can I show other info like value2
, grp1
etc?
2: How can I format the tooltip? For example, I want to show id: xxx
, instead of just a number.
I want to use ggplotly
function to achieve this, since it's so convenient to convert a ggplot to interactive plot.
thanks a lot.