I'm trying to create a tooltip that shows the density as well as the x-axis information using ggplot and ggplotly.
Using the mtcars data sets, I wanted to just create a tooltip for the density of the mpg that was cleaner.
The basic plot I was trying to create was using
data(mtcars)
ggplotly(
ggplot(mtcars)+
geom_density(
aes(x = mpg,
y = ..scaled..)
)+
scale_y_continuous(
name = 'Counters',
sec.axis = sec_axis(~ . * (max_dens/max_hist)*10, name = 'Density'))
)
which produces enter image description here
This tooltip is working, but I would essentially like to change the 'scaled' to something else. I've tried added the text to the aes like:
ggplotly(
ggplot(mtcars)+
geom_density(
aes(x = mpg,
y = ..scaled..,
text = paste('testing',mpg))
)
)
But that just creates a new density for each mpg.
The full example I want to use is combining both a histogram and density like:
data(mtcars)
nbins = 20
df = mtcars
min.brea <- round_any(min(df$mpg), diff(range(df$mpg))/nbins, floor)
max.brea <- round_any(max(df$mpg), diff(range(df$mpg))/nbins, ceiling)
breaks <- seq(min.brea, max.brea, diff(range(df$mpg/nbins)))
histdata <- hist(df$mpg, breaks=breaks, plot=FALSE, right=FALSE)
max_hist <- max(histdata$counts)
max_dens <- max(density(df$mpg)$y)
ggplotly(
ggplot(mtcars)+
geom_histogram(
aes(x = mpg,
text = paste('Testing', mpg)
),
bins = 30,
color = 'black',
fill = 'white'
)+
geom_density(
aes(x = mpg,
y = ..scaled..*(max_hist),
text = paste('Testing', mpg, max_hist*..scaled..))
)+
scale_y_continuous(
name = 'Counters',
sec.axis = sec_axis(~ . * (max_dens/max_hist), name = 'Density')),
tooltip = 'text')
But this really messes up the tooltip.
I was hoping there was a way to specify the tooltips for each one individually to be cleaner on the output side of things. Eventually this will go into an Rshiny app.
