2020# ' consider.
2121# ' @param max_levels Integer. Parameters with more than \code{max_levels}
2222# ' distinct values are dropped to keep the plot readable. Defaults to 25.
23+ # ' @param ylim_lower Numeric scalar or \code{NULL}. Optional lower display limit
24+ # ' for the y-axis. Uses \code{ggplot2::coord_cartesian()}, so data are not
25+ # ' removed before computing violin and boxplots. Defaults to \code{0}.
2326# ' @param lang Character. Plot language: \code{"de"} or \code{"en"}.
2427# '
2528# ' @return A ggplot object with facetted violin, boxplot, and jitter layers.
26- # '
27- # ' @examples
28- # ' \dontrun{
29- # ' library(readr)
30- # ' library(dplyr)
31- # ' library(tidyr)
32- # ' library(ggplot2)
33- # ' library(forcats)
34- # '
35- # ' df <- read_csv("simulation_results_optimisation.csv", show_col_types = FALSE)
36- # ' params <- c("connected_area", "mulde_area", "mulde_height",
37- # ' "filter_hydraulicconductivity", "filter_height",
38- # ' "storage_height", "bottom_hydraulicconductivity", "rain_factor")
39- # '
40- # ' p <- plot_main_effects(
41- # ' df = df,
42- # ' y = "n_overflows",
43- # ' params = params,
44- # ' max_levels = 20,
45- # ' lang = "de"
46- # ' )
47- # ' p
48- # ' }
49- # '
5029# ' @export
5130# ' @importFrom dplyr %>% select all_of group_by summarise left_join mutate n_distinct
5231# ' @importFrom tidyr pivot_longer
5332# ' @importFrom ggplot2 ggplot aes geom_violin geom_boxplot geom_jitter facet_wrap
54- # ' theme_bw theme labs element_text
33+ # ' theme_bw theme labs element_text coord_cartesian
5534# ' @importFrom forcats fct_reorder
5635# ' @importFrom stats median
5736# ' @importFrom rlang .data
5837plot_main_effects <- function (df ,
5938 y = " n_overflows" ,
6039 params ,
6140 max_levels = 25 ,
41+ ylim_lower = 0 ,
6242 lang = c(" de" , " en" )) {
6343
6444 lang <- match.arg(lang )
@@ -117,7 +97,7 @@ plot_main_effects <- function(df,
11797 values_to = " value"
11898 ) %> %
11999 dplyr :: mutate(
120- value = as.factor(value )
100+ value = as.factor(.data $ value )
121101 )
122102
123103 eff <- dl %> %
@@ -139,7 +119,7 @@ plot_main_effects <- function(df,
139119 parameter_label = forcats :: fct_reorder(.data $ parameter_label , .data $ effect , .desc = TRUE )
140120 )
141121
142- ggplot2 :: ggplot(dl , ggplot2 :: aes(x = .data $ value , y = .data [[y ]])) +
122+ p <- ggplot2 :: ggplot(dl , ggplot2 :: aes(x = .data $ value , y = .data [[y ]])) +
143123 ggplot2 :: geom_violin(trim = FALSE ) +
144124 ggplot2 :: geom_boxplot(width = 0.18 , outlier.alpha = 0 ) +
145125 ggplot2 :: geom_jitter(width = 0.15 , alpha = 0.15 , size = 1 ) +
@@ -153,4 +133,10 @@ plot_main_effects <- function(df,
153133 y = txt $ y_lab(y ),
154134 title = sprintf(txt $ title , txt $ y_lab(y ))
155135 )
136+
137+ if (! is.null(ylim_lower )) {
138+ p <- p + ggplot2 :: coord_cartesian(ylim = c(ylim_lower , NA ))
139+ }
140+
141+ p
156142}
0 commit comments