[R] boxplot notch

Bert Gunter bgunter@4567 @end|ng |rom gm@||@com
Fri Aug 16 18:11:20 CEST 2024


Probably should be:
rep("yes", length.out = len)[ypos]

as yes is probably a nonexistent variable.

But I didn't examine your code in detail, so just ignore if I'm wrong.

-- Bert

On Fri, Aug 16, 2024 at 8:51 AM SIBYLLE STÖCKLI via R-help
<r-help using r-project.org> wrote:
>
> Thanks Ben,
>
> Here the reproducible example.
> It works without notch=TRUE, but provides an error with notch=TURE
>
> Error in `geom_boxplot()`:
> ! Problem while converting geom to grob.
> ℹ Error occurred in the 1st layer.
> Caused by error in `ans[ypos] <- rep(yes, length.out = len)[ypos]`:
> ! replacement has length zero
> Run `rlang::last_trace()` to see where the error occurred.
> Warning message:
> In rep(yes, length.out = len) : 'x' is NULL so the result will be NULL
>
>
> Data
> Farm_ID Jahr    Bio     QI_A
> 1       2015    1       9.5
> 2       2018    1       15.7
> 3       2020    1       21.5
> 1       2015    1       50.5
> 2       2018    1       12.9
> 3       2020    1       11.2
> 1       2015    1       30.6
> 2       2018    1       28.7
> 3       2020    1       29.8
> 1       2015    1       30.1
> 2       2018    1       NA
> 3       2020    1       16.9
> 1       2015    0       6.5
> 2       2018    0       7.9
> 3       2020    0       10.2
> 1       2015    0       11.2
> 2       2018    0       18.5
> 3       2020    0       29.5
> 1       2015    0       25.1
> 2       2018    0       16.1
> 3       2020    0       15.9
> 1       2015    0       10.1
> 2       2018    0       8.4
> 3       2020    0       3.5
> 1       2015    0       NA
> 2       2018    0       NA
> 3       2020    0       3.5
>
>
> Code
> setwd("C:/Users/Sibylle Stöckli/Desktop/")
> #.libPaths()
> getwd()
>
> #libraries laden
> library("ggplot2")
> library("gridExtra")
> library(scales)
> library(nlme)
> library(arm)
> library(blmeco)
> library(stats)
> library(dplyr)
> library(ggpubr)
> library(patchwork)
> library(plotrix)
> library(tidyverse)
> library(dplyr)
>
> #read data
> MS = read.delim("Test1.txt", na.strings="NA")
> names(MS)
>
> MS$Jahr<-as.numeric(MS$Jahr)
> MS$Bio<-as.factor(MS$Bio)
> str(MS)
>
> ##### boxplot BFF QI
>
> MS1<- MS %>% filter(QI_A!="NA") %>% droplevels()
> MS1$Jahr<-as.factor(MS1$Jahr)
>
> MS1s <- MS1 %>%
>   group_by(MS1$Jahr, MS1$Bio) %>%
>   summarise(
>     y0 = quantile(QI_A, 0.05),
>     y25 = quantile(QI_A, 0.25),
>     y50 = mean(QI_A),
>     y75 = quantile(QI_A, 0.75),
>     y100 = quantile(QI_A, 0.95))
>
> MS1s
> colnames(MS1s)[1]<-"Jahr"
> colnames(MS1s)[2]<-"Bio"
> MS1s
>
> p1<-ggplot(MS1s, aes(Jahr,  fill = as.factor(Bio))) +
>   geom_boxplot(
>     aes(ymin = y0, lower = y25, middle = y50, upper = y75, ymax = y100),
>     stat = "identity", notch=TRUE
>   ) +
>   theme(panel.background = element_blank())+
>   theme(axis.line = element_line(colour = "black"))+
>   theme(axis.text=element_text(size=18))+
>   theme(axis.title=element_text(size=20))+
>   ylab("Anteil BFF an LN [%]") +xlab("Jahr")+
>   scale_color_manual(values=c("red","darkgreen"), labels=c("ÖLN", "BIO"))+
>   scale_fill_manual(values=c("red","darkgreen"), labels= c("ÖLN", "BIO"))+
>   theme(legend.title = element_blank())+
>   theme(legend.text=element_text(size=20))
> p1<-p1 + expand_limits(y=c(0, 80))
> p1
>
> -----Original Message-----
> From: R-help <r-help-bounces using r-project.org> On Behalf Of Ben Bolker
> Sent: Friday, August 16, 2024 3:30 PM
> To: r-help using r-project.org
> Subject: Re: [R] boxplot notch
>
>    I don't see anything obviously wrong here. There may be something subtle, but we probably won't be able to help without a reproducible example ...
>
> On 2024-08-16 9:24 a.m., SIBYLLE STÖCKLI via R-help wrote:
> > Dear community
> >
> >
> >
> > I tried the following code using geom_boxplot() and notch=TRUE. Does
> > anyone know if the command  notch=TRUE  is at the wrong place in my
> > special code construct?
> >
> >
> >
> > Without notch=TRUE the code provides the planned ggplot.
> >
> >
> >
> > Kind regards
> >
> > Sibylle
> >
> >
> >
> > Code:
> >
> >
> >
> > MS1<- MS %>% filter(QI_A!="NA") %>% droplevels()
> >
> > MS1$Jahr<-as.factor(MS1$Jahr)
> >
> >
> >
> > MS1s <- MS1 %>%
> >
> >    group_by(MS1$Jahr, MS1$Bio) %>%
> >
> >    summarise(
> >
> >      y0 = quantile(QI_A, 0.05),
> >
> >      y25 = quantile(QI_A, 0.25),
> >
> >      y50 = mean(QI_A),
> >
> >      y75 = quantile(QI_A, 0.75),
> >
> >      y100 = quantile(QI_A, 0.95))
> >
> >
> >
> > MS1s
> >
> > colnames(MS1s)[1]<-"Jahr"
> >
> > colnames(MS1s)[2]<-"Bio"
> >
> > MS1s
> >
> >
> >
> > p1<-ggplot(MS1s, aes(Jahr,  fill = as.factor(Bio))) +
> >
> >    geom_boxplot(
> >
> >      aes(ymin = y0, lower = y25, middle = y50, upper = y75, ymax =
> > y100),
> >
> >      stat = "identity", notch=TRUE
> >
> >    ) +
> >
> >    theme(panel.background = element_blank())+
> >
> >    theme(axis.line = element_line(colour = "black"))+
> >
> >    theme(axis.text=element_text(size=18))+
> >
> >    theme(axis.title=element_text(size=20))+
> >
> >    ylab("Anteil BFF an LN [%]") +xlab("Jahr")+
> >
> >    scale_color_manual(values=c("red","darkgreen"), labels=c(" LN",
> > "BIO"))+
> >
> >    scale_fill_manual(values=c("red","darkgreen"), labels= c(" LN",
> > "BIO"))+
> >
> >    theme(legend.title = element_blank())+
> >
> >    theme(legend.text=element_text(size=20))
> >
> > p1<-p1 + expand_limits(y=c(0, 80))
> >
> > p1
> >
> >
> >       [[alternative HTML version deleted]]
> >
> >
> > ______________________________________________
> > R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> > http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
>
> --
> Dr. Benjamin Bolker
> Professor, Mathematics & Statistics and Biology, McMaster University Director, School of Computational Science and Engineering  > E-mail is sent at my convenience; I don't expect replies outside of working hours.
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list