From 5f53f1c9f6a3c9f504aec88b1e536af212a146d5 Mon Sep 17 00:00:00 2001 From: ddspys Date: Mon, 11 Nov 2024 05:21:30 +0000 Subject: [PATCH] change box plot color setting --- R/bar.R | 2 +- R/box.R | 3 ++- R/line.R | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 69 insertions(+), 8 deletions(-) diff --git a/R/bar.R b/R/bar.R index d4411c4d..ddee6fe2 100644 --- a/R/bar.R +++ b/R/bar.R @@ -600,7 +600,7 @@ barServer <- function(id, data, data_label, data_varStruct = NULL, nfactor.limit min = 0, max = 1, value = 0.5 ), sliderInput(session$ns("pvaly"), "y-axis", - min = 0, max = 1, value = 1 + min = 0, max = 1, value = 0.7 ), h3("Pair P-value position"), sliderInput(session$ns("p_pvalfont"), "P-value font size", diff --git a/R/box.R b/R/box.R index 939c076c..dd5ac6a6 100644 --- a/R/box.R +++ b/R/box.R @@ -473,7 +473,8 @@ boxServer <- function(id, data, data_label, data_varStruct = NULL, nfactor.limit fillcolor <- "white" if (input$fillcolor) { - fillcolor <- "gray" + fillcolor <- color + color <- "black" } pval.font.size <- c(input$pvalfont, input$p_pvalfont, input$p_pvalfont / 10) pval.coord <- c(input$pvalx, input$pvaly) diff --git a/R/line.R b/R/line.R index 978eb6aa..a439d23e 100644 --- a/R/line.R +++ b/R/line.R @@ -52,7 +52,8 @@ lineUI <- function(id, label = "lineplot") { checkboxInput(ns("label"), "Label"), uiOutput(ns("pvalue")), uiOutput(ns("subvar")), - uiOutput(ns("subval")) + uiOutput(ns("subval")), + # uiOutput(ns("size")), # uiOutput(ns("position.dodge")) @@ -175,6 +176,9 @@ lineServer <- function(id, data, data_label, data_varStruct = NULL, nfactor.limi ) }) + + + output$strata_line <- renderUI({ strata_vars <- setdiff(vlist()$factor_vars, vlist()$except_vars) strata_vars <- setdiff(strata_vars, input$x_line) @@ -264,6 +268,7 @@ lineServer <- function(id, data, data_label, data_varStruct = NULL, nfactor.limi }) + observeEvent(input$subcheck, { output$subvar <- renderUI({ req(input$subcheck == T) @@ -363,8 +368,22 @@ lineServer <- function(id, data, data_label, data_varStruct = NULL, nfactor.limi outUI }) + y_r <- reactiveVal(NULL) + + observeEvent(c(input$isStrata, + input$x_line, + input$y_line, + input$strata, + input$pvalfont, + input$s_pvalue, + input$positiondodge, + input$label), { + y_r(NULL) + + }) + lineInput <- reactive({ - req(c(input$x_line, input$y_line, input$strata, input$pvalfont, input$s_pvalue, input$positiondodge, input$label)) + req(c(input$x_line, input$y_line, input$strata, input$pvalfont, input$s_pvalue, input$positiondodge, input$label, input$y_range)) req(input$isStrata != "None") data <- data.table(data()) @@ -406,6 +425,7 @@ lineServer <- function(id, data, data_label, data_varStruct = NULL, nfactor.limi line.point.size <- input$pointsize pval.font.size <- input$pvalfont + res.plot <- ggpubr::ggline(data, input$x_line, input$y_line, color = color, add = add, add.params = add.params, conf.int = input$lineci, xlab = label[variable == input$x_line, var_label][1], @@ -414,24 +434,40 @@ lineServer <- function(id, data, data_label, data_varStruct = NULL, nfactor.limi size = line.size, point.size = line.point.size, linetype = linetype + ) + + + + if (input$rev_y) { res.plot <- res.plot + ggplot2::scale_y_reverse() } + y_range_internal <- ggplot_build(res.plot)$layout$panel_scales_y[[1]]$range$range + if (input$isStrata & input$strata != "None") { res.plot <- res.plot + ggpubr::stat_compare_means( method = input$s_pvalue, size = pval.font.size, + label.y = 1.3 * (y_range_internal[2] - y_range_internal[1]) + y_range_internal[1], + #label.y = 1.3 * (input$y_range[2] - input$y_range[1]) + input$y_range[1], + #label.y = 1.3 * (y_r()[2] - y_r()[1]) + y_r()[1], aes( label = scales::label_pvalue(add_p = TRUE)(after_stat(p)), group = !!sym(input$strata) - ), + ) ) } + if(!is.null(y_r())){ + res.plot <- res.plot + coord_cartesian(ylim = input$y_range) + } + + + if (input$label) { if (con3 <- input$strata != "None") { res.plot <- res.plot + @@ -439,7 +475,7 @@ lineServer <- function(id, data, data_label, data_varStruct = NULL, nfactor.limi fun.data = function(x) { return(data.frame(y = mean(x), label = round(mean(x), 2))) }, - geom = "label", + geom = "label_repel", aes( label = !!sym(input$y_line), group = !!sym(input$strata) @@ -451,16 +487,38 @@ lineServer <- function(id, data, data_label, data_varStruct = NULL, nfactor.limi fun.data = function(x) { return(data.frame(y = mean(x), label = round(do.call(add[1], list(x = x))[[1]], 2))) }, - geom = "label", - aes(label = !!sym(input$y_line), ) + geom = "label_repel", + aes(label = !!sym(input$y_line)) ) } } + + return(res.plot) }) + + + + + + observeEvent(lineInput(), { + if(is.null(y_r())){ + y_r(ggplot_build(lineInput())$layout$panel_scales_y[[1]]$range$range) + } + }) + + observeEvent(y_r(),{ + if(!is.null(y_r())){ + range <- y_r()[2] - y_r()[1] + updateSliderInput(session, "y_range", min = round(y_r()[1] - range * 0.5), max = round(y_r()[2] + range * 0.5), value = y_r()) + } + }) + + + output$downloadControls <- renderUI({ tagList( column( @@ -526,6 +584,8 @@ lineServer <- function(id, data, data_label, data_varStruct = NULL, nfactor.limi column(6, numericInput(session$ns("pointsize"), "Point size", step = 0.5, value = 0.5)) ), sliderInput(session$ns("positiondodge"), "Position dodge", min = 0, max = 1, value = 0), + h3("X - Y range setting"), + sliderInput(session$ns("y_range"), "Y axis range", min = 0, max = 10000, value = c(0, 10000)), h3("P-value position"), sliderInput(session$ns("pvalfont"), "P-value font size", min = 1, max = 10, value = 4