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 at r-project.org> On Behalf Of Ben Bolker Sent: Friday, August 16, 2024 3:30 PM To: r-help at 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 at 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 at 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.
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 at 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 at r-project.org> On Behalf Of Ben Bolker > Sent: Friday, August 16, 2024 3:30 PM > To: r-help at 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 at 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 at 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 at 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.
That's not really a reprex Sibylle.? I did try to use it to see if I could work out what you were trying to do and help but there is so much in there that I suspect is distraction from the notch issue and its error message. Please can you give us something stripped of all unecessary things and tell us what you want? Something like data that we can read as a tribble() or from a dput() of your data and then only the packages you actually need for the plot (I think tidyverse alone might do) and then a ggplot() call stripped right down to what you need and a clear explanation of what you are trying to do in the geom_boxplot() call and how it uses the summarised data tibble. It may even be that if you do that, you will find what's causing the problem!? (I speak from bitter experience!!) Very best (all), Chris On 16/08/2024 17:51, SIBYLLE ST?CKLI via R-help wrote:> 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-- Chris Evans (he/him) Visiting Professor, UDLA, Quito, Ecuador & Honorary Professor, University of Roehampton, London, UK. Work web site: https://www.psyctc.org/psyctc/ CORE site: http://www.coresystemtrust.org.uk/ Personal site: https://www.psyctc.org/pelerinage2016/ Emeetings (Thursdays): https://www.psyctc.org/psyctc/booking-meetings-with-me/ (Beware: French time, generally an hour ahead of UK) <https://ombook.psyctc.org/book> [[alternative HTML version deleted]]