R plotly:在 Boxplot 上添加线段 plotly

R plotly : Add segment on Boxplot plotly

我们的想法是制作一个箱线图并在其上为每个人添加一条目标线。我制作了一个有效的箱线图,但我没有成功添加目标线("Obj" 列包含目标值)。

这是我的数据:

Variability_datas = structure(list(Pupil = c("PUPIL03", "PUPIL02", 
                           "PUPIL04", "PUPIL05", "PUPIL01", "PUPIL06", 
                           "PUPIL03", "PUPIL02", "PUPIL04", "PUPIL05", 
                           "PUPIL01", "PUPIL06", "PUPIL03", "PUPIL02", 
                           "PUPIL04", "PUPIL05", "PUPIL01", "PUPIL06", 
                           "PUPIL03", "PUPIL02", "PUPIL04", "PUPIL05", 
                           "PUPIL01", "PUPIL06", "PUPIL03", "PUPIL02", 
                           "PUPIL04", "PUPIL05", "PUPIL01", "PUPIL06", 
                           "PUPIL03", "PUPIL02", "PUPIL04", "PUPIL05", 
                           "PUPIL01", "PUPIL06", "PUPIL03", "PUPIL02", 
                           "PUPIL04", "PUPIL05", "PUPIL01", "PUPIL06", 
                           "PUPIL03", "PUPIL02", "PUPIL04", "PUPIL05", 
                           "PUPIL01", "PUPIL06", "PUPIL03", "PUPIL02", 
                           "PUPIL04", "PUPIL05", "PUPIL01", "PUPIL06", 
                           "PUPIL03", "PUPIL02", "PUPIL04", "PUPIL05", 
                           "PUPIL01", "PUPIL06", "PUPIL03", "PUPIL02", 
                           "PUPIL04", "PUPIL05", "PUPIL01", "PUPIL06", 
                           "PUPIL03", "PUPIL02", "PUPIL04", "PUPIL05", 
                           "PUPIL01", "PUPIL06", "PUPIL03", "PUPIL02", 
                           "PUPIL04", "PUPIL05", "PUPIL01", "PUPIL06", 
                           "PUPIL03", "PUPIL02", "PUPIL04", "PUPIL05", 
                           "PUPIL01", "PUPIL06", "PUPIL03", "PUPIL02", 
                           "PUPIL04", "PUPIL05", "PUPIL01", "PUPIL06", 
                           "PUPIL03", "PUPIL02", "PUPIL04", "PUPIL05", 
                           "PUPIL01", "PUPIL06", "PUPIL03", "PUPIL02", 
                           "PUPIL04", "PUPIL05", "PUPIL01", "PUPIL06", 
                           "PUPIL03", "PUPIL02", "PUPIL04", "PUPIL05", 
                           "PUPIL01", "PUPIL06", "PUPIL03", "PUPIL02", 
                           "PUPIL04", "PUPIL05", "PUPIL01", "PUPIL06", 
                           "PUPIL03", "PUPIL02", "PUPIL04", "PUPIL05", 
                           "PUPIL01", "PUPIL06", "PUPIL03", "PUPIL02", 
                           "PUPIL04", "PUPIL05", "PUPIL01", "PUPIL06", 
                           "PUPIL03", "PUPIL02", "PUPIL04", "PUPIL05", 
                           "PUPIL01", "PUPIL06", "PUPIL03", "PUPIL02", 
                           "PUPIL04", "PUPIL05", "PUPIL01", "PUPIL06", 
                           "PUPIL03", "PUPIL02", "PUPIL04", "PUPIL05", 
                           "PUPIL01", "PUPIL06", "PUPIL03", "PUPIL02", 
                           "PUPIL04", "PUPIL05", "PUPIL01", "PUPIL06", 
                           "PUPIL03", "PUPIL02", "PUPIL04", "PUPIL05", 
                           "PUPIL01", "PUPIL06", "PUPIL03", "PUPIL02", 
                           "PUPIL04", "PUPIL05", "PUPIL01", "PUPIL06", 
                           "PUPIL03", "PUPIL02", "PUPIL04", "PUPIL05"
  ), Score = c(0, 0.33, 
        1.51, 10.23, 58.18, 27.84, 0.4, 1.95, 0, 0, 40.16, 1.88, 0, 0.83, 
        1.18, 16.32, 3.05, 51.28, 2.12, 0, 18.2, 4.89, 0, 23.18, 0, 0, 
        28.93, 10.75, 0.05, 16.97, 1.05, 0.45, 56.8, 68.46, 1.24, 15.14, 
        0, 32, 12.87, 23.28, 0, 6.9, 0.45, 85.61, 14.68, 0, 0.15, 2.21, 
        0, 46.55, 4.36, 2.84, 0, 55.68, 0, 21.22, 5.78, 6.97, 9.06, 82.1, 
        0, 8.3, 0.34, 0, 59.28, 26.8, 21.29, 1.19, 0, 1.75, 27.2, 10.13, 
        19.66, 0, 0, 5.79, 35.69, 0.03, 0.12, 0, 1.1, 1.81, 5.25, 0, 
        25.27, 2, 15.28, 0.75, 2.52, 1.59, 8.17, 0, 0, 8.18, 2.64, 0, 
        4.62, 0, 1, 11.39, 4.67, 0, 0.64, 4.34, 1.14, 8.84, 0, 3.98, 
        0, 4.99, 0.57, 11.29, 17.43, 2.05, 5.21, 0.74, 14.14, 0, 25, 
        40.19, 4.81, 0, 4.49, 7.36, 11.84, 0, 0.66, 0, 25, 21.72, 13.41, 
        9.69, 0, 0.88, 23.83, 27.99, 0, 3.19, 0, 13.02, 12.14, 11.19, 
        22.39, 80.26, 0, 7.29, 0.33, 5.25, 16.81, 12.56, 0, 5.57, 0, 
        41.34, 2.66, 6.88, 0.87, 23.85, 0, 7.22, 0, 25.07, 0, 0.85, 1.76, 
        1.42), Obj = c(15, 15, 15, 15, 15, 15, 15, 15, 
                       15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 
                       15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 
                       15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 
                       15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 
                       15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 
                       15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 
                       15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 
                       15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 
                       15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 
                       15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15)), .Names = c("Pupil", 
                                                                                            "Score", "Obj"), class = "data.frame", row.names = c(NA, 186L))

箱线图代码:

library(plotly)
plot_ly(Variability_datas, x = ~Variability_datas$Pupil, y=Variability_datas$Score, type = "box", boxpoints = "all") %>% 
  layout(height = 650, width = 1200, 
         title= "Box plot", 
         yaxis = list(title = "Score"), 
         xaxis = list(title = "Pupil", tickangle=315), 
         margin = list(b = 115))

如有任何帮助,我们将不胜感激

使用 plotly 你也许可以使用 add_segments() 来完成。但使用 ggplot2ggplotly() 可能更容易。

你可以试试这个:

dat2 <- data.frame( x= c("1", "2", "3", "4", "5", "6"), y = c(15, 15, 15, 15, 15, 15))


a <- ggplot(data = Variability_datas, aes(x = Pupil, y = Score)) + geom_boxplot() + theme_bw() +
  geom_segment(data = dat2, color = "red", aes(x = as.numeric(x) - 0.3,
                                    y = y,
                                    xend = as.numeric(x) + 0.3,
                                    yend = y)) 

ggplotly(a)