Вы можете использовать add_lines на каждом графике, чтобы добавить горизонтальную линию, и, установив name = 'cd' или 'ad', вы пометите легенды:
library(reshape2)
library(dplyr)
library(plotly)
cd = data.frame(id = 1:100, norm = rnorm(100), poi = rpois(100, 1), exp = rexp(100,1))
cd = melt(cd, id= "id")
p1 = plot_ly(cd, x=~variable, y=~value, type = 'violin',name = 'cd', box =
list(visible =T), meanline = list(visible = T))%>% add_lines(x = ~variable, y =
rep(1.8,length(cd$id)),showlegend=FALSE)%>%
layout(annotations = list(text = sprintf("Seeds=10, Coupons=5", 1:10),
font = list(size=16), xanchor = "center", xref = "paper", yref =
"paper", yanchor = "top", showarrow = F, y =1, x=0.5))
ad = data.frame(id = 1:100, norm = rnorm(100), poi = rpois(100, 1), exp =
rexp(100,1))
ad = melt(ad, id= "id")
p2 = plot_ly(ad, x=~variable, y=~value, type = 'violin',name = 'ad',box =
list(visible =T), meanline = list(visible = T))%>% add_lines(x = ~variable, y =
rep(1.8,length(ad$id)),showlegend=FALSE)%>%
layout(annotations = list(text = sprintf("Seeds=20, Coupons=5", 1:10),
font = list(size=16), xanchor = "center", xref = "paper", yref =
"paper", yanchor = "top", showarrow = F, y =1, x=0.5))
vline <- function(x = 0, color = "red") {
list(
type = "line",
y0 = 0,
y1 = 1,
yref = "paper",
x0 = x,
x1 = x,
line = list(color = color)
)
}
subplot(p1,p2, shareY = T)