How do I adjust the cutouts in the ggplot window plot after setting them manually with ggplot_build ()?

I have a question regarding this about how to set up the cutouts in the ggplot boxplot. The author of the question answered himself and gave a hint to ggplot_build () , but I am unfortunately unable to use it.

So my question is, after you've manually set the cutout constraints with ggplot_build () , how do I repeat it once to set up my own plot?

Here are my details

vib = c(3.94,-0.61,0.03,0.46)
pla = c(0.784444444, 1.11,-1.98,-1.39)

df = data.frame(value = c(vib, pla), 
                group = c(rep("vibration", times = 4), rep("placebo", times = 4)))

p <- ggplot(df, aes(x = group, y = value)) + geom_boxplot(notch=TRUE)

gg <- ggplot_build(p)

gg$data[[1]]$notchlower[1]<-sort(vib)[qbinom(c(.25, .975), length(vib), .5)][1]
gg$data[[1]]$notchlower[2]<-sort(pla)[qbinom(c(.25, .975), length(pla), .5)][1]

gg$data[[1]]$notchupper[1]<-sort(vib)[qbinom(c(.25, .975), length(vib), .5)][2]
gg$data[[1]]$notchupper[2]<-sort(pla)[qbinom(c(.25, .975), length(pla), .5)][2]

gg$data[[1]]

ggplot(gg$data[[1]], aes(x = x)) +
  geom_boxplot(aes(ymin = ymin, lower = lower, middle = middle, upper = upper, ymax = ymax), 
           stat = "identity",
           notch = TRUE)

      

And the warning message popped up

Error in ifelse(notch, data$notchlower, NA) : replacement has length zero
In addition: Warning message:
In rep(yes, length.out = length(ans)) :
  'x' is NULL so the result will be NULL

      

What am I doing wrong? Many thanks

+3


source to share


1 answer


Use ggplot_gtable

to undo an operation instead of re-inserting it in ggplot

:

gg$data[[1]]$notchlower[1:2]<- c(-1,0)
gg$data[[1]]$notchupper[1:2]<- c(0.5, 1)

plot(ggplot_gtable(gg))

      



enter image description here

+2


source







All Articles