changing the width of my errorbars in a grouped boxplot without it being uncorrectly centered

Hello there,

I am quite new to R but I have a problem to which i couldnt find a helpful answer
I made a grouped boxplot for my Interventionsseite and Kontrollseite over the course of my experiment.
Trying to add errorbars puts them uncentered until i delete my x and y labeling.

I tried the following code:
pd = position_dodge(width = 0.5)
ggplot(data=data_bds,mapping = aes(x=t,y=v,fill=Interventionsseite))+
stat_boxplot(geom = 'errorbar', position=pd, width=0.25)+
geom_boxplot(outlier.colour = "white",width=0.4, position=pd)+

But my Errorbars are still not centered.
Thanks in advance for your tips.

And thats the full code:
pacman::p_load(pacman,rio,tidyverse)
library(ggplot2)
library(tidyr)
df3<-import("BeinDSK3.xlsx",which=1)%>%
as_tibble() %>%
mutate(Proband=as.factor(Proband))%>%
mutate(Interventionsseite=as.factor(Interventionsseite))%>%
print()

pd = position_dodge(width = 0.5)

#erzeugen long table
data_bds<-gather(df3,t,v, "0":"9")
data_bds~Interventionsseit
data_bds$t<-as.factor(data_bds$t)

ggplot(data=data_bds,mapping = aes(x=t,y=v,fill=Interventionsseite))+
geom_hline(yintercept = 0,size=0.6,color="gray",linetype=2)+
stat_boxplot(geom = 'errorbar', position=pd, width=0.25)+
geom_boxplot(outlier.colour = "white",width=0.4, position=pd)+
scale_x_discrete(name="Zeitfenster",label=c("t0'",
"2'","5'", "15'","30'","60'","90'","p2'",
"p10'","p20'"))+
scale_y_continuous(name = "Δ-TOI in mlO2/min/100g",
breaks = seq(-40,30,by=10),limits = c(-40,30))+
scale_fill_manual(values=c("gray35","gray96"),
name=NULL,labels=c("Interventionsseite","Kontrollseite"))+
theme(legend.position = "bottom")+
theme(panel.background = element_blank(),legend.key = element_blank())+
theme( axis.line=element_line(size=1/10,
color='black'))+
theme(axis.text.x=element_text(size=11))+
theme(axis.text.y = element_text(size=11))

beispiel

1 Like

If I invent some data, the plot looks correct. Can you post a subset of data_bds that produces a plot with the problem? You can use the output of dput() to post the data. For example,

dput(head(data_bds,50))

will give the first 50 rows of data_bds.

library(tidyverse)
#> Warning: package 'tibble' was built under R version 4.1.2
set.seed(123)
data_bds <- data.frame(t=sample(c("t0'","2'"),100,replace = TRUE),
                       v=runif(100,-20,20),
                       Interventionsseite=sample(c("Interventionsseite","Kontrollseite"),100,replace = TRUE))

pd = position_dodge(width = 0.5)
ggplot(data=data_bds,mapping = aes(x=t,y=v,fill=Interventionsseite))+
  geom_hline(yintercept = 0,size=0.6,color="gray",linetype=2)+
  stat_boxplot(geom = 'errorbar', position=pd, width=0.25)+
  geom_boxplot(outlier.colour = "white",width=0.4, position=pd)+
  scale_x_discrete(name="Zeitfenster",label=c("t0'",
                                              "2'","5'", "15'","30'","60'","90'","p2'",
                                              "p10'","p20'"))+
  scale_y_continuous(name = "<U+0394>-TOI in mlO2/min/100g",
                     breaks = seq(-40,30,by=10),limits = c(-40,30))+
  scale_fill_manual(values=c("gray35","gray96"),
                    name=NULL,labels=c("Interventionsseite","Kontrollseite"))+
  theme(legend.position = "bottom")+
  theme(panel.background = element_blank(),legend.key = element_blank())+
  theme( axis.line=element_line(size=1/10,
                                color='black'))+
  theme(axis.text.x=element_text(size=11))+
  theme(axis.text.y = element_text(size=11))

Created on 2022-02-08 by the reprex package (v2.0.1)

Hey thank you for your quick response
Here is the shortened data version and also a copy of the excel raw data below

dput(head(data_bds,50))
structure(list(Proband = structure(c(7L, 8L, 6L, 3L, 10L, 4L,
5L, 1L, 2L, 9L, NA, 7L, 8L, 6L, 3L, 10L, 4L, 5L, 1L, 2L, 9L,
7L, 8L, 6L, 3L, 10L, 4L, 5L, 1L, 2L, 9L, NA, 7L, 8L, 6L, 3L,
10L, 4L, 5L, 1L, 2L, 9L, 7L, 8L, 6L, 3L, 10L, 4L, 5L, 1L), .Label = c("15",
"151", "152", "153", "154", "155", "156", "158", "162", "163"
), class = "factor"), Interventionsseite = structure(c(1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, NA, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, NA, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L
), .Label = c("Int", "Kont"), class = "factor"), t = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L), .Label = c("0", "1", "2", "3", "4", "5", "6", "7", "8",
"9"), class = "factor"), v = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
NA, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5.39999999999999, 12.1, -9.8,
-5, 6.5, -13.9, -23.7, 0.349999999999994, -13.5, -8.39999999999999,
NA, -1.9, -0.600000000000009, 0.700000000000003, -4.15000000000001,
-2.8, -3.39999999999999, 1.35000000000001, -5.15000000000001,
-1.85, -4.7, 5.7, 9.8, -22.7, -6.6, -2.8, -11.5, -24.3, 4.8)), row.names = c(NA,
-50L), class = c("tbl_df", "tbl", "data.frame"))

Proband 0 1 2 3 4 5 6 7 8 9 Interventionsseite
156 0,00 5,40 5,70 5,50 5,50 7,10 0,50 -5,20 -14,80 -15,30 Int
158 0,00 12,10 9,80 5,50 9,80 14,80 2,00 2,10 5,80 5,70 Int
155 0,00 -9,80 -22,70 -23,90 -25,60 -23,70 -24,20 19,20 16,80 7,30 Int
152 0,00 -5,00 -6,60 -8,30 -7,45 -3,60 -2,50 6,10 3,95 3,05 Int
163 0,00 6,50 -2,80 -14,35 -11,40 -12,50 -25,60 -0,60 -0,40 -5,20 Int
153 0,00 -13,90 -11,50 -10,00 -6,30 -6,50 -5,20 8,40 10,65 7,70 Int
154 0,00 -23,70 -24,30 -23,30 -26,20 -28,80 -29,80 -4,30 3,90 -9,45 Int
15 0,00 0,35 4,80 4,90 1,80 -2,65 -0,15 6,90 3,20 1,90 Int
151 0,00 -13,50 -16,00 -15,80 -18,00 -18,70 -22,15 9,40 11,30 3,40 Int
162 0,00 -8,40 -8,15 -9,60 -10,40 -11,20 -10,60 -2,80 0,60 -4,15 Int
156 0,00 -1,90 -2,50 -2,40 2,40 1,70 4,90 4,50 3,40 4,80 Kont
158 0,00 -0,60 3,60 4,20 2,20 -3,60 -4,20 -3,70 -3,70 -3,40 Kont
155 0,00 0,70 -0,80 -1,20 -4,60 -5,50 -6,60 -3,50 -4,50 -7,10 Kont
152 0,00 -4,15 -4,70 -6,75 -3,80 -1,95 -2,50 0,25 -1,85 -0,20 Kont
163 0,00 -2,80 -2,75 0,35 0,55 -1,45 -11,85 -13,05 -18,05 -16,35 Kont
153 0,00 -3,40 -7,90 -11,60 -5,20 -3,50 -1,75 -3,00 -0,55 -2,50 Kont
154 0,00 1,35 2,80 5,85 1,90 -7,90 -21,90 -22,95 -26,60 -27,25 Kont
15 0,00 -5,15 -1,35 -5,05 -5,45 -6,65 -4,70 -5,90 -4,35 -6,55 Kont
151 0,00 -1,85 -5,30 -4,20 -5,70 -12,30 -11,20 -11,05 -12,70 -13,60 Kont
162 0,00 -4,70 -0,40 -7,55 -10,30 -11,80 -12,60 -13,40 -11,10 -13,10 Kont
![image 387x500](upload://1yQIhxVomRWp6SIFIosBZlSfPTz.png)

thank you for your help

I don't see any problem with the graph when I use the data you posted. Do you agree? If you run the code below, does the graph look correct?

library(tidyverse)
#> Warning: package 'tibble' was built under R version 4.1.2
data_bds <- structure(list(Proband = structure(c(7L, 8L, 6L, 3L, 10L, 4L,
                                                 5L, 1L, 2L, 9L, NA, 7L, 8L, 6L, 3L, 10L, 4L, 5L, 1L, 2L, 9L,
                                                 7L, 8L, 6L, 3L, 10L, 4L, 5L, 1L, 2L, 9L, NA, 7L, 8L, 6L, 3L,
                                                 10L, 4L, 5L, 1L, 2L, 9L, 7L, 8L, 6L, 3L, 10L, 4L, 5L, 1L), .Label = c("15",
                                                                                                                       "151", "152", "153", "154", "155", "156", "158", "162", "163"
                                                 ), class = "factor"), 
                           Interventionsseite = structure(c(1L, 1L,
                                                            1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, NA, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
                                                            2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, NA, 2L, 2L,
                                                            2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L
                           ), .Label = c("Int", "Kont"), class = "factor"), 
                           t = structure(c(1L,
                                           1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
                                           1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
                                           2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
                                           3L), .Label = c("0", "1", "2", "3", "4", "5", "6", "7", "8",
                                                           "9"), class = "factor"), 
                           v = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                                 NA, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5.39999999999999, 12.1, -9.8,
                                 -5, 6.5, -13.9, -23.7, 0.349999999999994, -13.5, -8.39999999999999,
                                 NA, -1.9, -0.600000000000009, 0.700000000000003, -4.15000000000001,
                                 -2.8, -3.39999999999999, 1.35000000000001, -5.15000000000001,
                                 -1.85, -4.7, 5.7, 9.8, -22.7, -6.6, -2.8, -11.5, -24.3, 4.8)), 
                      row.names = c(NA,-50L), class = c("tbl_df", "tbl", "data.frame"))

pd = position_dodge(width = 0.5)
ggplot(data=data_bds,mapping = aes(x=t,y=v,fill=Interventionsseite))+
  geom_hline(yintercept = 0,size=0.6,color="gray",linetype=2)+
  stat_boxplot(geom = 'errorbar', position=pd, width=0.25)+
  geom_boxplot(outlier.colour = "white",width=0.4, position=pd)+
  scale_x_discrete(name="Zeitfenster",label=c("t0'",
                                              "2'","5'", "15'","30'","60'","90'","p2'",
                                              "p10'","p20'"))+
  scale_y_continuous(name = "<U+0394>-TOI in mlO2/min/100g",
                     breaks = seq(-40,30,by=10),limits = c(-40,30))+
  scale_fill_manual(values=c("gray35","gray96"),
                    name=NULL,labels=c("Interventionsseite","Kontrollseite"))+
  theme(legend.position = "bottom")+
  theme(panel.background = element_blank(),legend.key = element_blank())+
  theme( axis.line=element_line(size=1/10,
                                color='black'))+
  theme(axis.text.x=element_text(size=11))+
  theme(axis.text.y = element_text(size=11))
#> Warning: Removed 2 rows containing non-finite values (stat_boxplot).
#> Warning: Removed 2 rows containing non-finite values (stat_boxplot).

Created on 2022-02-08 by the reprex package (v2.0.1)

If we just look at the first two time points than it really seams ok, but if i try my whole data set the error bars shift a bit uncentered again
Rplot21
.

That is very strange! I guess we need a large enough data set to see the problem. Depending on how big the data set is, can you post (using dput) either the whole set or a sub set that still shows that problem? The dplyr package has the function slice_sample() where you can randomly sample either n rows, or a proportion prop of the data set.

here is the data set using dput (the original data set has 210 rows and 4 variables)
structure(list(Proband = structure(c(7L, 8L, 6L, 3L, 10L, 4L,
5L, 1L, 2L, 9L, NA, 7L, 8L, 6L, 3L, 10L, 4L, 5L, 1L, 2L, 9L,
7L, 8L, 6L, 3L, 10L, 4L, 5L, 1L, 2L, 9L, NA, 7L, 8L, 6L, 3L,
10L, 4L, 5L, 1L, 2L, 9L, 7L, 8L, 6L, 3L, 10L, 4L, 5L, 1L, 2L,
9L, NA, 7L, 8L, 6L, 3L, 10L, 4L, 5L, 1L, 2L, 9L, 7L, 8L, 6L,
3L, 10L, 4L, 5L, 1L, 2L, 9L, NA, 7L, 8L, 6L, 3L, 10L, 4L, 5L,
1L, 2L, 9L, 7L, 8L, 6L, 3L, 10L, 4L, 5L, 1L, 2L, 9L, NA, 7L,
8L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 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L
), .Label = c("0", "1", "2", "3", "4", "5", "6", "7", "8",
"9"), class = "factor"), v = c(0, 0, 0, 0, 0, 0, 0, 0, 0,
0, NA, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5.39999999999999, 12.1,
-9.8, -5, 6.5, -13.9, -23.7, 0.349999999999994, -13.5, -8.39999999999999,
NA, -1.9, -0.600000000000009, 0.700000000000003, -4.15000000000001,
-2.8, -3.39999999999999, 1.35000000000001, -5.15000000000001,
-1.85, -4.7, 5.7, 9.8, -22.7, -6.6, -2.8, -11.5, -24.3, 4.8,
-16, -8.14999999999999, NA, -2.5, 3.59999999999999, -0.799999999999997,
-4.7, -2.75, -7.89999999999999, 2.80000000000001, -1.35000000000001,
-5.3, -0.400000000000006, 5.5, 5.5, -23.9, -8.3, -14.35,
-10, -23.3, 4.89999999999999, -15.8, -9.59999999999999, NA,
-2.4, 4.2, -1.2, -6.75, 0.349999999999994, -11.6, 5.85000000000001,
-5.05000000000001, -4.2, -7.55, 5.5, 9.8, -25.6, -7.45, -11.4,
-6.30000000000001, -26.2, 1.8, -18, -10.4, NA, 2.4, 2.2,
-4.59999999999999, -3.80000000000001, 0.550000000000011,
-5.19999999999999, 1.90000000000001, -5.45000000000002, -5.7,
-10.3, 7.09999999999999, 14.8, -23.7, -3.6, -12.5, -6.50000000000001,
-28.8, -2.65000000000001, -18.7, -11.2, NA, 1.7, -3.60000000000001,
-5.5, -1.95, -1.44999999999999, -3.49999999999999, -7.90000000000001,
-6.65000000000001, -12.3, -11.8, 0.5, 2, -24.2, -2.5, -25.6,
-5.20000000000002, -29.8, -0.150000000000006, -22.15, -10.6,
NA, 4.9, -4.2, -6.59999999999999, -2.5, -11.85, -1.74999999999999,
-21.9, -4.7, -11.2, -12.6, -5.2, 2.1, 19.2, 6.09999999999999,
-0.599999999999994, 8.39999999999999, -4.3, 6.89999999999999,
9.39999999999999, -2.8, NA, 4.5, -3.7, -3.5, 0.25, -13.05,
-2.99999999999999, -22.95, -5.90000000000001, -11.05, -13.4,
-14.8, 5.8, 16.8, 3.95, -0.399999999999991, 10.65, 3.90000000000001,
3.2, 11.3, 0.600000000000009, NA, 3.4, -3.7, -4.5, -1.85000000000001,
-18.05, -0.549999999999983, -26.6, -4.35000000000001, -12.7,
-11.1, -15.3, 5.7, 7.3, 3.05, -5.2, 7.69999999999999, -9.44999999999999,
1.89999999999999, 3.39999999999999, -4.14999999999999, NA,
4.8, -3.40000000000001, -7.09999999999999, -0.200000000000003,
-16.35, -2.49999999999999, -27.25, -6.55000000000001, -13.6,
-13.1)), row.names = c(NA, -210L), class = c("tbl_df", "tbl",
"data.frame"))

I still do not see the problem. I would try shutting down Rstudio and restarting it, because I always try that when things are weird. Also please post the output of running sessionInfo(). I will post mine below the reprex.

library(tidyverse)
#> Warning: package 'tibble' was built under R version 4.1.2
data_bds <- structure(list(Proband = structure(c(7L, 8L, 6L, 3L, 10L, 4L,
                                                 5L, 1L, 2L, 9L, NA, 7L, 8L, 6L, 3L, 10L, 4L, 5L, 1L, 2L, 9L,
                                                 7L, 8L, 6L, 3L, 10L, 4L, 5L, 1L, 2L, 9L, NA, 7L, 8L, 6L, 3L,
                                                 10L, 4L, 5L, 1L, 2L, 9L, 7L, 8L, 6L, 3L, 10L, 4L, 5L, 1L, 2L,
                                                 9L, NA, 7L, 8L, 6L, 3L, 10L, 4L, 5L, 1L, 2L, 9L, 7L, 8L, 6L,
                                                 3L, 10L, 4L, 5L, 1L, 2L, 9L, NA, 7L, 8L, 6L, 3L, 10L, 4L, 5L,
                                                 1L, 2L, 9L, 7L, 8L, 6L, 3L, 10L, 4L, 5L, 1L, 2L, 9L, NA, 7L,
                                                 8L, 6L, 3L, 10L, 4L, 5L, 1L, 2L, 9L, 7L, 8L, 6L, 3L, 10L, 4L,
                                                 5L, 1L, 2L, 9L, NA, 7L, 8L, 6L, 3L, 10L, 4L, 5L, 1L, 2L, 9L,
                                                 7L, 8L, 6L, 3L, 10L, 4L, 5L, 1L, 2L, 9L, NA, 7L, 8L, 6L, 3L,
                                                 10L, 4L, 5L, 1L, 2L, 9L, 7L, 8L, 6L, 3L, 10L, 4L, 5L, 1L, 2L,
                                                 9L, NA, 7L, 8L, 6L, 3L, 10L, 4L, 5L, 1L, 2L, 9L, 7L, 8L, 6L,
                                                 3L, 10L, 4L, 5L, 1L, 2L, 9L, NA, 7L, 8L, 6L, 3L, 10L, 4L, 5L,
                                                 1L, 2L, 9L, 7L, 8L, 6L, 3L, 10L, 4L, 5L, 1L, 2L, 9L, NA, 7L,
                                                 8L, 6L, 3L, 10L, 4L, 5L, 1L, 2L, 9L), .Label = c("15", "151",
                                                                                                  "152", "153", "154", "155", "156", "158", "162", "163"), class = "factor"),
                           Interventionsseite = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
                                                            1L, 1L, 1L, 1L, NA, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
                                                            1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, NA, 2L, 2L, 2L, 2L,
                                                            2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
                                                            1L, NA, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L,
                                                            1L, 1L, 1L, 1L, 1L, 1L, 1L, NA, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
                                                            2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, NA, 2L,
                                                            2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L,
                                                            1L, 1L, 1L, 1L, NA, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
                                                            1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, NA, 2L, 2L, 2L, 2L,
                                                            2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
                                                            1L, NA, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L,
                                                            1L, 1L, 1L, 1L, 1L, 1L, 1L, NA, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
                                                            2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, NA, 2L,
                                                            2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Int", "Kont"
                                                            ), class = "factor"), 
                           t = structure(c(1L, 1L, 1L, 1L, 1L,
                                           1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
                                           1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
                                           2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
                                           3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L,
                                           4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
                                           4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
                                           5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L,
                                           6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
                                           6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
                                           7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
                                           8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L,
                                           9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
                                           9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
                                           10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L
                           ), .Label = c("0", "1", "2", "3", "4", "5", "6", "7", "8",
                                         "9"), class = "factor"), 
                           v = c(0, 0, 0, 0, 0, 0, 0, 0, 0,
                                 0, NA, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5.39999999999999, 12.1,
                                 -9.8, -5, 6.5, -13.9, -23.7, 0.349999999999994, -13.5, -8.39999999999999,
                                 NA, -1.9, -0.600000000000009, 0.700000000000003, -4.15000000000001,
                                 -2.8, -3.39999999999999, 1.35000000000001, -5.15000000000001,
                                 -1.85, -4.7, 5.7, 9.8, -22.7, -6.6, -2.8, -11.5, -24.3, 4.8,
                                 -16, -8.14999999999999, NA, -2.5, 3.59999999999999, -0.799999999999997,
                                 -4.7, -2.75, -7.89999999999999, 2.80000000000001, -1.35000000000001,
                                 -5.3, -0.400000000000006, 5.5, 5.5, -23.9, -8.3, -14.35,
                                 -10, -23.3, 4.89999999999999, -15.8, -9.59999999999999, NA,
                                 -2.4, 4.2, -1.2, -6.75, 0.349999999999994, -11.6, 5.85000000000001,
                                 -5.05000000000001, -4.2, -7.55, 5.5, 9.8, -25.6, -7.45, -11.4,
                                 -6.30000000000001, -26.2, 1.8, -18, -10.4, NA, 2.4, 2.2,
                                 -4.59999999999999, -3.80000000000001, 0.550000000000011,
                                 -5.19999999999999, 1.90000000000001, -5.45000000000002, -5.7,
                                 -10.3, 7.09999999999999, 14.8, -23.7, -3.6, -12.5, -6.50000000000001,
                                 -28.8, -2.65000000000001, -18.7, -11.2, NA, 1.7, -3.60000000000001,
                                 -5.5, -1.95, -1.44999999999999, -3.49999999999999, -7.90000000000001,
                                 -6.65000000000001, -12.3, -11.8, 0.5, 2, -24.2, -2.5, -25.6,
                                 -5.20000000000002, -29.8, -0.150000000000006, -22.15, -10.6,
                                 NA, 4.9, -4.2, -6.59999999999999, -2.5, -11.85, -1.74999999999999,
                                 -21.9, -4.7, -11.2, -12.6, -5.2, 2.1, 19.2, 6.09999999999999,
                                 -0.599999999999994, 8.39999999999999, -4.3, 6.89999999999999,
                                 9.39999999999999, -2.8, NA, 4.5, -3.7, -3.5, 0.25, -13.05,
                                 -2.99999999999999, -22.95, -5.90000000000001, -11.05, -13.4,
                                 -14.8, 5.8, 16.8, 3.95, -0.399999999999991, 10.65, 3.90000000000001,
                                 3.2, 11.3, 0.600000000000009, NA, 3.4, -3.7, -4.5, -1.85000000000001,
                                 -18.05, -0.549999999999983, -26.6, -4.35000000000001, -12.7,
                                 -11.1, -15.3, 5.7, 7.3, 3.05, -5.2, 7.69999999999999, -9.44999999999999,
                                 1.89999999999999, 3.39999999999999, -4.14999999999999, NA,
                                 4.8, -3.40000000000001, -7.09999999999999, -0.200000000000003,
                                 -16.35, -2.49999999999999, -27.25, -6.55000000000001, -13.6,
                                 -13.1)), 
                      row.names = c(NA, -210L), class = c("tbl_df", "tbl","data.frame"))

pd = position_dodge(width = 0.5)
ggplot(data=data_bds,mapping = aes(x=t,y=v,fill=Interventionsseite))+
  geom_hline(yintercept = 0,size=0.6,color="gray",linetype=2)+
  stat_boxplot(geom = 'errorbar', position=pd, width=0.25)+
  geom_boxplot(outlier.colour = "white",width=0.4, position=pd)+
  scale_x_discrete(name="Zeitfenster",label=c("t0'",
                                              "2'","5'", "15'","30'","60'","90'","p2'",
                                              "p10'","p20'"))+
  scale_y_continuous(name = "<U+0394>-TOI in mlO2/min/100g",
                     breaks = seq(-40,30,by=10),limits = c(-40,30))+
  scale_fill_manual(values=c("gray35","gray96"),
                    name=NULL,labels=c("Interventionsseite","Kontrollseite"))+
  theme(legend.position = "bottom")+
  theme(panel.background = element_blank(),legend.key = element_blank())+
  theme( axis.line=element_line(size=1/10,
                                color='black'))+
  theme(axis.text.x=element_text(size=11))+
  theme(axis.text.y = element_text(size=11))
#> Warning: Removed 10 rows containing non-finite values (stat_boxplot).
#> Warning: Removed 10 rows containing non-finite values (stat_boxplot).

Created on 2022-02-08 by the reprex package (v2.0.1)

sessionInfo()
R version 4.1.1 (2021-08-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19044)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252 
[2] LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] forcats_0.5.1   stringr_1.4.0   dplyr_1.0.7     purrr_0.3.4    
[5] readr_2.0.2     tidyr_1.1.4     tibble_3.1.6    ggplot2_3.3.5  
[9] tidyverse_1.3.1

loaded via a namespace (and not attached):
 [1] tidyselect_1.1.1 xfun_0.26        haven_2.4.3      colorspace_2.0-2
 [5] vctrs_0.3.8      generics_0.1.0   htmltools_0.5.2  yaml_2.2.1      
 [9] utf8_1.2.2       rlang_0.4.11     pillar_1.6.3     glue_1.4.2      
[13] withr_2.4.2      DBI_1.1.1        dbplyr_2.1.1     modelr_0.1.8    
[17] readxl_1.3.1     lifecycle_1.0.1  munsell_0.5.0    gtable_0.3.0    
[21] cellranger_1.1.0 rvest_1.0.1      evaluate_0.14    labeling_0.4.2  
[25] knitr_1.36       callr_3.7.0      fastmap_1.1.0    tzdb_0.1.2      
[29] ps_1.6.0         fansi_0.5.0      highr_0.9        broom_0.7.9     
[33] Rcpp_1.0.7       clipr_0.7.1      scales_1.1.1     backports_1.2.1 
[37] jsonlite_1.7.2   farver_2.1.0     fs_1.5.0         hms_1.1.1       
[41] digest_0.6.28    stringi_1.7.4    processx_3.5.2   grid_4.1.1      
[45] cli_3.0.1        tools_4.1.1      magrittr_2.0.1   crayon_1.4.1    
[49] pkgconfig_2.0.3  ellipsis_0.3.2   xml2_1.3.2       reprex_2.0.1    
[53] lubridate_1.7.10 assertthat_0.2.1 rmarkdown_2.11   httr_1.4.2      
[57] rstudioapi_0.13  R6_2.5.1         compiler_4.1.1  

thanks for your fast reply FJCC
If you look closely at certain timepoints like p20' the horizontal part of the errorbar is shifted.

and of course my session information:

R version 4.1.2 (2021-11-01)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19042)

Matrix products: default

locale:
[1] LC_COLLATE=German_Germany.1252 LC_CTYPE=German_Germany.1252
[3] LC_MONETARY=German_Germany.1252 LC_NUMERIC=C
[5] LC_TIME=German_Germany.1252
system code page: 65001

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] readxl_1.3.1 forcats_0.5.1 stringr_1.4.0 dplyr_1.0.7
[5] purrr_0.3.4 readr_2.1.2 tidyr_1.2.0 tibble_3.1.6
[9] ggplot2_3.3.5 tidyverse_1.3.1 rio_0.5.29 pacman_0.5.1

loaded via a namespace (and not attached):
[1] Rcpp_1.0.8 lattice_0.20-45 lubridate_1.8.0
[4] assertthat_0.2.1 digest_0.6.29 utf8_1.2.2
[7] R6_2.5.1 cellranger_1.1.0 backports_1.4.1
[10] reprex_2.0.1 httr_1.4.2 pillar_1.7.0
[13] rlang_1.0.0 curl_4.3.2 rstudioapi_0.13
[16] data.table_1.14.2 labeling_0.4.2 foreign_0.8-81
[19] munsell_0.5.0 broom_0.7.12 compiler_4.1.2
[22] modelr_0.1.8 pkgconfig_2.0.3 mnormt_2.0.2
[25] tmvnsim_1.0-2 tidyselect_1.1.1 fansi_1.0.2
[28] crayon_1.4.2 tzdb_0.2.0 dbplyr_2.1.1
[31] withr_2.4.3 grid_4.1.2 nlme_3.1-153
[34] jsonlite_1.7.3 gtable_0.3.0 lifecycle_1.0.1
[37] DBI_1.1.2 magrittr_2.0.2 scales_1.1.1
[40] zip_2.2.0 cli_3.1.1 stringi_1.7.6
[43] farver_2.1.0 fs_1.5.2 xml2_1.3.3
[46] ellipsis_0.3.2 generics_0.1.2 vctrs_0.3.8
[49] openxlsx_4.2.5 tools_4.1.2 glue_1.6.1
[52] hms_1.1.1 parallel_4.1.2 colorspace_2.0-2
[55] rvest_1.0.2 haven_2.4.3

You are right, p20' is a little off. I experimented and found that increasing the width in stat_boxplot to 0.35 seemed to help the centering. Other than that, I can't suggest anything else. I hope someone else will have a better suggestion.

thank you for your approach fjcc
i also tried differeent width values but i felt that this should not be the usual case
If you can think of another approach or a tip for structuring everything differently, I would of course be very happy to hear from you again.

I think it's likely that this is rounding error. Changing the width of the bars will likely help, as will increasing the resolution of the saved image.

3 Likes

Hey Axeman,
It really worked. Crazy I´ve been trying the whole day different approaches
Thanks a lot!!!!!!!!!

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.