Trust interval ranges in ggplot2 when using stat_quantile?

I would like to add a median spline and corresponding confidence interval ranges to the ggplot2 scatter ggplot2 . I use the 'quantreg'-package , in particular the rqss function (additive regression smoothing with addition).

In ggplot2 I can add spline medians, but not confidence interval ranges:

 fig = ggplot(dd, aes(y = MeanEst, x = N, colour = factor(polarization))) fig + stat_quantile(quantiles=0.5, formula = y ~ qss(x), method = "rqss") + geom_point() 

ggplot2 median spline

quantreg package comes with its own charting function; plot.rqss . Where can I add confidence ranges ( bands=TRUE ):

 plot(1, type="n", xlab="", ylab="", xlim=c(2, 12), ylim=c(-3, 0)) # empty plot plotfigs = function(df) { rqss_model = rqss(df$MeanEst ~ qss(df$N)) plot(rqss_model, bands=TRUE, add=TRUE, rug=FALSE, jit=FALSE) return(NULL) } figures = lapply(split(dd, as.factor(dd$polarization)), plotfigs) 

enter image description here

However, the graph function that comes with the quantreg package is not very flexible / well suited to my needs. Is it possible to get confidence bands on the ggplot2 chart? Perhaps by imitating the method used in the quantreg package, or simply copying them from the graph?

Data: pastebin .

+5
source share
1 answer

You almost have it. When you call

  plot(rqss_model, bands=TRUE, add=TRUE, rug=FALSE, jit=FALSE) 

The function helps a lot to return the constructed data. All we do is capture a data frame. First, a little tweaking of your function, return the data in a reasonable way.

 plotfigs = function(df) { rqss_model = rqss(df$MeanEst ~ qss(df$N)) band = plot(rqss_model, bands=TRUE, add=TRUE, rug=FALSE, jit=FALSE) data.frame(x=band[[1]]$x, low=band[[1]]$blo, high=band[[1]]$bhi, pol=unique(df$polarization)) } 

Then call the function and condense

 figures = lapply(split(dd, as.factor(dd$polarization)), plotfigs) bands = Reduce("rbind", figures) 

Then use geom_ribbon to build

 ## We inherit y and color, so have to set them to NULL fig + geom_ribbon(data=bands, aes(x=x, ymin=low, ymax=high, y=NULL, color=NULL, group=factor(pol)), alpha=0.3) 
+3
source

Source: https://habr.com/ru/post/1242113/


All Articles