Epifunky - 2 months ago 10

R Question

I've got a question relative to this one about how to customize notches in ggplot boxplot. The question's author replied to himself and gave a hint with **ggplot_build()** but I'm unfortunately not able to use it.

So my question is: once you set manually your notch limits with **ggplot_build()**, how one reuses it to customize its own box plot?

Here is my data

`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 resulting warning message

`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?

Thanks a lot

Answer

Source (Stackoverflow)