z-score limits for boxplots/ adding single lines to different grouped boxplot

Viewed 17

I have several measured values from different sources, I want to put an upper and lower limit for a given Median of a single test ID. I have different tests grouped together as you see in the picture I have several so to say, each test have about 5 sources and each source has 3 Measured values. therefore I have put boxplots for each source over its data and had all the tests with the boxplots of the different sources grouped in one source. my problem starts when I want to put a z score limit over the data just one z score per test is registerd but i would rather have a certain line limit over all the boxplots and not have just single points where they are all connected ( see the pic )

here is my code without the data

## Libraries call
library(readxl)

require(tidyverse)

require(rlang)

library(dplyr)

require(tidyr)

require(stringr)

require(plotly)

require(ggplot2)

require(matrixStats)

require(openxlsx)


############################
# source comparision Functions
############################
  
  # Mean und Median bauen
  df$Mean = rowMeans(as.matrix(df[,c(6,7,8)]),na.rm = TRUE)

  df$Median = rowMedians(as.matrix(df[,c(6,7,8)]),na.rm = TRUE)

  # summarize for TestID

  df_sum <-df%>%

    group_by(TestID)%>% 

    summarise(Mean=mean(Mean)
              ,Max=max(Mean)
              ,Min=min(Mean)
              ,Median=median(Median)
              ,Std=sd(Mean)
              ,Mad=mad(Mean)
              ,z_limit_std=2*Std
              ,z_limit_mad=2*Mad
    )

  # Merge von summary und DLG Daten
  df_Median<- df[,c('TestID','Median')]

  df_sum_Median <- df_Median%>% group_by(TestID)%>% summarise(Median=median(Median))
  
  
  
  
  
  df = merge(x = df, y = df_sum, by = "TestID")

  ############################
  #Box Plot
  ############################
  
  Plot_Data_df <- data.frame(df$TestID
                             ,df$`measured_value 1`
                             ,df$`measured_value 2`
                             ,df$`measured_value 3`
                             ,df$Median.y
                             ,df$z_limit_std)
  
  
  # Daten in einem String umformen und die measured_valuee mit subset Daten mit NA
  dfboxplot <- data.frame(TestID = rep(paste0(Plot_Data_df$df.TestID, '_Test'), 3)
                          ,measured_value = c(Plot_Data_df$df..measured_value.1.,
                                              Plot_Data_df$df..measured_value.2.,
                                              Plot_Data_df$df..measured_value.3.)
                          ,Median = rep(Plot_Data_df$df.Median.y, 3)
                          ,z_limit = rep(Plot_Data_df$df.z_limit_std, 3)
                          )
  
  
  dfboxplot$lower_limit <- dfboxplot$Median - dfboxplot$z_limit

  dfboxplot$upper_limit  <- dfboxplot$Median + dfboxplot$z_limit
  
  plot <-plot_ly(dfboxplot, x = ~TestID, y = ~measured_value , color = ~Lab, type = "box",inherit=FALSE) %>%

    layout(boxmode = "group",
           xaxis = list(title='Test ID'),
           yaxis = list(title= ' measured_value'))%>%
    

    plotly::add_lines(data = dfboxplot     # lower limit einführen
                      ,y= ~Median
                      ,x= ~TestID
                      ,type = 'scatter' 
                      ,mode = 'lines'
                      ,showlegend = FALSE
                      ,line = list(color = 'rgb(0, 0, 0)', 
                                   width = 1) 
                      ,name = 'Median'
    )%>% plotly::add_lines(data = dfboxplot     # lower limit einführen
                      ,y= ~upper_limit
                      ,x= ~TestID
                      ,type = 'scatter' 
                      ,mode = 'lines'
                      ,showlegend = FALSE
                      ,line = list(color = 'rgb(200, 0, 0)', 
                                   width = 1) 
                      ,name = 'upper limit'
    )%>% 

  # 
 
plot
0 Answers
Related