Home > Enterprise >  shading a geom_slabinterval given xmin and xmax
shading a geom_slabinterval given xmin and xmax

Time:01-10

I'd like to switch a visualization of mean and CI from geom_pointrange to geom_interval from the ggdist package. I can draw a black slab, but I want shading as in the example in the documentation.

Code:

library(ggplot2)
library(ggdist)
data = data.frame(x=c(0.4,0.8),xmin=c(0.2,0.6),xmax=c(0.7,1.2),y=c("beta1","beta2"))
ggplot(data,aes(x=x,y=y))   geom_pointrange(aes(xmin=xmin,xmax=xmax))
ggplot(data,aes(x=x,y=y))   geom_interval(aes(xmin=xmin,xmax=xmax))

geom_pointrange example geom_interval example without shading

Documentation example (geom_interval example with shading

I've tried messing around with fill_interval but I suspect I need to do something equivalent to a ggplot stats transformations, since unlike in the examples the data to plot isn't an empirical distribution. (In case it's relevant, my 95% confidence intervals come from an asymptotic normal distribution, so it shouldn't be a problem to calculate 50%/95%/99% intervals for plotting.)

CodePudding user response:

Try it:

data %>% 
  group_by(y) %>% 
  median_qi(.width = c(0.50, 0.95, 0.99)) %>% 
  ggplot(aes(y = y, x = x, xmin = xmin, xmax = xmax))  
  geom_interval()  
  scale_color_brewer()

CodePudding user response:

Thanks to @Wilson Souza's answer, this seems to do the trick.

data = rbind(
         data.frame(y="beta1",x=.4,x.lower=c(.35,.3,0),
             x.upper=c(.45,.5,.8),.width=c(.5,.95,.99)),
         data.frame(y="beta2",x=.7,x.lower=c(.6,.2,.1),
             x.upper=c(.8,1.0,1.1),.width=c(.5,.95,.99))
       )

ggplot(data,aes(y = y, x = x))  
  geom_interval(aes(xmin=x.lower,xmax=x.upper))   scale_color_brewer()
  •  Tags:  
  • Related