cooldood3490 cooldood3490 - 10 months ago 69
R Question

ggplotly: R, labeling trace names but keep one line

I'm creating a ggplotly for a dataframe that's very similar to

. Here's what I have so far:

ggplotly(ggplot(diamonds, aes(depth, colour = cut)) +
geom_density() +
xlim(55, 70))

It creates the following:

enter image description here

When you hover over the trace it shows: depth, cut, and density. I want to also show the clarity so I added the following:

ggplotly(ggplot(diamonds, aes(depth, text = paste("Clarity: ", clarity), colour = cut)) +
geom_density() +
xlim(55, 70))

That command creates the following: ggplot

When I hover of the curve it shows depth, Clarity, cut, and density. That's what I want. However, how do I keep the density as one curve like how it was in the first plot I created instead of multiple curves?


Does this work? Set the alpha of the extra lines to 0 (so they become transparent. Using geom_line as geom_density uses alpha for fill only. (system problems prevent testing)

  ggplot(diamonds, aes(depth, colour = cut)) +
    geom_density() +
    geom_line(aes(text = paste("Clarity: ", clarity)), stat="density", alpha=0) + 
    xlim(55, 70)