2015-10-13 14 views
5

Ho bisogno di tracciare una curva con errore standard come area ombreggiata. Ad esempio, ho una matrice come questo, come bidoni di tempo:È necessario tracciare una curva con errore standard in R

Age  CO2     Standard_error 
0 1     1.42E-13 
0.5 0.998268422989761 0.00169763164186241 
1 0.995743963199747 0.00479900584235718 
1.5 0.995062233834876 0.0103274581695151 
2 1.00398569520812 0.0195262589284694 
2.5 1.03116826950464 0.0329875314671063 
3 1.07422916427453 0.049116358866183 
3.5 1.11992125335082 0.0646007093291105 
4 1.15670166266193 0.0770010287134558 
4.5 1.18120894601468 0.0860204557092314 
5 1.1972210240662 0.0930892044882256 
5.5 1.21094781023761 0.0999899575457834 
6 1.22407556599768 0.10698386874689 
6.5 1.23264038072763 0.112706241640139 
7 1.23471241147135 0.116401516372119 
7.5 1.23341569261173 0.118772825620555 
8 1.23279196992244 0.120901622556905 
8.5 1.2346500417623 0.123408621016096 
9 1.23831115917507 0.126316578608025 
9.5 1.24201463025631 0.129312831831815 

E vorrei tracciare la curva con questo errore standard stimato. La maggior parte delle funzioni che ho visto (in particolare all'interno di ggplot2) stimano l'errore standard e ho già stimato questi dati. Qualsiasi aiuto è apprezzato!

risposta

10

È possibile utilizzare ggplot2 in combinazione con geom_ribbon:

library(ggplot2) 
ggplot(dat, aes(x = Age, y = CO2)) + 
     geom_line() + 
     geom_ribbon(aes(ymin = CO2 - Standard_error, 
         ymax = CO2 + Standard_error), alpha = 0.2) 

enter image description here

+0

Ottimo lavoro @jeremycg! Grazie!! – user3660245

7

?matplot spesso è utile per questo tipo di compito in base di R tracciato:

matplot(
    dat$Age, 
    dat$CO2 + outer(dat$Standard_error, c(0,1,-1)), 
    type="l", lty=c(1,2,2), col=c(1,2,2), 
    xlab="Age", ylab="CO2" 
) 

enter image description here

Se l'ombreggiatura è fondamentale, mi piacerebbe girare a polygon:

ses <- dat$CO2 + outer(dat$Standard_error, c(1,-1)) 
with(dat, 
    plot(
    Age, CO2, type="l", ylim=range(ses), 
    panel.first=polygon(c(Age,rev(Age)), c(ses[,1],rev(ses[,2])),border=NA, col="#ebebeb") 
) 
) 

enter image description here

Problemi correlati