2016-02-02 8 views
5

मैं ggplot2 स्कैटर प्लॉट में औसत स्पलीन और संबंधित आत्मविश्वास अंतराल बैंड जोड़ना चाहता हूं। मैं 'quantreg'-package का उपयोग कर रहा हूं, विशेष रूप से rqss फ़ंक्शन (योजक क्वांटाइल रिग्रेशन स्मूथिंग)।stat_quantile का उपयोग करते समय ggplot2 में विश्वास अंतराल बैंड?

ggplot2 में मैं मंझला पट्टी को जोड़ने में सक्षम हूँ, लेकिन नहीं विश्वास अंतराल बैंड:

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 अपनी ही भूखंड समारोह के साथ आता है; plot.rqss। मैं कहां से आत्मविश्वास बैंड (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

हालांकि भूखंड समारोह है कि quantreg -package के साथ आता है बहुत लचीला नहीं/अच्छी तरह से मेरी जरूरतों के लिए अनुकूल है। क्या ggplot2 साजिश में आत्मविश्वास बैंड प्राप्त करना संभव है? शायद quantreg -package में उपयोग की गई विधि की नकल करके, या बस उन्हें साजिश से कॉपी कर रहे हैं?

डेटा: pastebin

उत्तर

3

आपके पास लगभग है। जब आप

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

फ़ंक्शन बहुत उपयोगी रूप से प्लॉट किए गए डेटा को वापस कर देता है। हम सब डेटा फ्रेम पकड़ लेते हैं। सबसे पहले अपने कार्य करने के लिए एक छोटी सी ट्वीक, एक समझदार तरीका

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)) 
} 

अगला कॉल समारोह में डेटा लौटाने और गाढ़ा

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

फिर साजिश

## 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) 
+0

धन्यवाद geom_ribbon का उपयोग करें। Plot.rqss आकृति को दबाने के लिए मैंने इसे 'pdf (फ़ाइल = NULL)' 'और' dev.off() '' में लपेट लिया। http://stackoverflow.com/a/24762900/1053612 – bonna

संबंधित मुद्दे