Search code examples
rggplot2ggplotly

remove duplicate horizontal line in ggplotly


When I switch from ggplot to ggplotly, "Virginica" seems to be duplicate with an horizontal line at 0.

data(iris)
library(plotly)
library(ggplot2)

plot_iris <- ggplot(iris, aes(x=Petal.Width, color = Species))+geom_density(aes(y = stat(count)))
ggplotly(plot_iris)

enter image description here

We see 2 blue lines, one of which is at 0 but with a good count. How can I remove this line ?


Solution

  • The problem can be resolved by specifying geom = 'line' inside stat_density:

    plotly::ggplotly(
       ggplot(iris, aes(x = Petal.Width, color = Species)) +
         stat_density(geom = 'line')
    )
    

    enter image description here