Black Lives Matter. Please consider donating to Black Girls Code today.
Dash Enterprise delivers an incredible 21x cost savings 💸Download the e-book!

Density by group with plot_ly

Hello, I’ve nee trying to plot several density functions in the same plot, with ggplo2 is very easy, but i want to do it with native plotly, i dont want to repeat the code. Is there an easier way that what i have? Thank you.

densitiesC1 <- lapply(dG2013[which(dG2013$Cuadrante == 'C1'),3:4],function(x) density(x,na.rm = TRUE))
densitiesC2 <- lapply(dG2013[which(dG2013$Cuadrante == 'C2'),3:4],function(x) density(x,na.rm = TRUE))
densitiesC3 <- lapply(dG2013[which(dG2013$Cuadrante == 'C3'),3:4],function(x) density(x,na.rm = TRUE))
densitiesC4 <- lapply(dG2013[which(dG2013$Cuadrante == 'C4'),3:4],function(x) density(x,na.rm = TRUE))

plot_ly()%>%
  add_lines(x=densitiesC1$Temperatura$x,y=densitiesC1$Temperatura$y,fill="tozeroy")%>%
  add_lines(x=densitiesC2$Temperatura$x,y=densitiesC2$Temperatura$y,fill="tozeroy")%>%
  add_lines(x=densitiesC3$Temperatura$x,y=densitiesC3$Temperatura$y,fill="tozeroy")%>%
  add_lines(x=densitiesC4$Temperatura$x,y=densitiesC4$Temperatura$y,fill="tozeroy")

Hey @TonoRocker,

Merge your dataframes by ids and then:

   df %>%
   plot_ly() %>%
   add_lines(x=~x,y=~y,split=~ids,fill="tozeroy")