2015-11-12 12 views
5

Ecco il codice di ciò che ho in questo momento:protegge una regione con stat_function e ggplot

library(ggplot2) 

normal <- function(mu, sigma, x){ 
1/(sigma*sqrt(2*pi))*exp(-((x-mu)/sigma)^2) 
} 

normal_expr <- function(){ 
    expression(N~bgroup('(',paste(x, '; ',mu, ',', sigma),')') == frac(1, sigma~sqrt(2*pi)) ~ 
        exp~bgroup('[',-~bgroup('(',frac(x-mu,sigma),')')^2,']')) 

} 


ggplot(data.frame(x=c(-3,3)), aes(x=x, color=g)) + 
    stat_function(data=data.frame(x=c(-2, 3.5), g=factor(1)), fun=normal, geom='line', 
       args=list(mu=0.5, sigma=2)) + 
    stat_function(data=data.frame(x=c(-2, 3.5), g=factor(2)), fun=normal, geom='line', 
       args=list(mu=1, sigma=2)) + 
    scale_x_continuous(breaks=seq(from=-2, to = 3, by=1)) + 
    ylab(normal_expr()) + 
    coord_cartesian(ylim=c(0, 0.2)) + 
    scale_color_manual('',values=c('blue','red', 'red'), 
    labels=c(expression(N(mu == 0.5, sigma==2)),expression(N(mu == 1, sigma==2)))) + 
    theme(panel.background = element_rect(fill='white'), 
     #panel.background has a gray-like color by default 
     panel.border=element_rect(fill=NA), 
     #panel.border puts in fill by default 
     legend.background = element_blank(), 
     legend.box = 'vertical', 
     legend.position=c(0.85,0.85), 
     legend.text.align=0 
     ) 

Ecco l'output:

enter image description here

Da quanto ho capito dal R Grafica Libro di ricette di Chang, dovrei essere in grado di aggiungere qualcosa come

normal_shade <- function(mu, sigma, x){ 
y <- normal(mu=mu, sigma=sigma, x) 
y[x < 0 | x > 2] <- NA 
return(y) 
} 

+ stat_function(fun=normal_shade, geom = 'area', fill = 'red', alpha = 0.2, args = 
list(mu = 1, sigma = 2)) 

al codice di cui sopra per ottenere ombreggiatura sotto la linea rossa sopra da x = 0 per x = 2.

Ecco cosa succede:

library(ggplot2) 

normal <- function(mu, sigma, x){ 
1/(sigma*sqrt(2*pi))*exp(-((x-mu)/sigma)^2) 
} 

normal_expr <- function(){ 
    expression(N~bgroup('(',paste(x, '; ',mu, ',', sigma),')') == frac(1, sigma~sqrt(2*pi)) 
~ exp~bgroup('[',-~bgroup('(',frac(x-mu,sigma),')')^2,']')) 

} 

normal_shade <- function(mu, sigma, x){ 
y <- normal(mu=mu, sigma=sigma, x) 
y[x < 0 | x > 2] <- NA 
return(y) 
} 


ggplot(data.frame(x=c(-3,3)), aes(x=x, color=g)) + 
    stat_function(data=data.frame(x=c(-2, 3.5), g=factor(1)), fun=normal, geom='line', 
       args=list(mu=0.5, sigma=2)) + 
    stat_function(data=data.frame(x=c(-2, 3.5), g=factor(2)), fun=normal, geom='line', 
       args=list(mu=1, sigma=2)) + 
    stat_function(fun=normal_shade, geom = 'area', fill = 'red', alpha = 0.2, 
args=list(mu=1, sigma=2)) + 
    scale_x_continuous(breaks=seq(from=-2, to = 3, by=1)) + 
    ylab(normal_expr()) + 
    coord_cartesian(ylim=c(0, 0.2)) + 
    scale_color_manual('',values=c('blue','red', 'red'), 
    labels=c(expression(N(mu == 0.5, sigma==2)),expression(N(mu == 1, sigma==2)))) + 
    theme(panel.background = element_rect(fill='white'), 
     #panel.background has a gray-like color by default 
     panel.border=element_rect(fill=NA), 
     #panel.border puts in fill by default 
     legend.background = element_blank(), 
     legend.box = 'vertical', 
     legend.position=c(0.85,0.85), 
     legend.text.align=0 
     ) 

Error in eval(expr, envir, enclos) : object 'g' not found 

Ho fatto un sacco di ricerche, ma non sono stati in grado di aggirare questo problema.

+0

basta rimuovere il 'color = g' poiché non si sta colorando da qualche gruppo. ggplot sta cercando una colonna chiamata "g" nel tuo data frame – rawr

+0

@rawr L'unico problema che ho con questo è che rimuove la legenda. C'è un modo per mantenerlo? – Clarinetist

+0

puoi aggiungere 'g = factor (1)' al tuo data frame – rawr

risposta

5

Hai omesso i dati sulla terza chiamata stat_function(). Quando si mette che già nel, sembra che questo:

ggplot(data.frame(x=c(-3,3)), aes(x=x, color=g)) + 
    stat_function(data=data.frame(x=c(-2, 3.5), g=factor(1)), fun=normal, geom='line', 
       args=list(mu=0.5, sigma=2)) + 
    stat_function(data=data.frame(x=c(-2, 3.5), g=factor(2)), fun=normal, geom='line', 
       args=list(mu=1, sigma=2)) + 
    stat_function(data=data.frame(x=c(-2, 3.5), g=factor(2)), fun=normal_shade, geom = 'area', fill = 'red', alpha = 0.2, 
       args=list(mu=1, sigma=2)) + 
    scale_x_continuous(breaks=seq(from=-2, to = 3, by=1)) + 
    ylab(normal_expr()) + 
    coord_cartesian(ylim=c(0, 0.2)) + 
    scale_color_manual('',values=c('blue','red', 'red'), 
        labels=c(expression(N(mu == 0.5, sigma==2)),expression(N(mu == 1, sigma==2)))) + 
    theme(panel.background = element_rect(fill='white'), 
     #panel.background has a gray-like color by default 
     panel.border=element_rect(fill=NA), 
     #panel.border puts in fill by default 
     legend.background = element_blank(), 
     legend.box = 'vertical', 
     legend.position=c(0.85,0.85), 
     legend.text.align=0 
) 

enter image description here

E se si imposta color=NA nel terzo stat_function() si chiama, si ottiene questo (che può essere un po 'più desiderabile):

enter image description here

+0

Grazie mille, soprattutto per il suggerimento 'color'! – Clarinetist

Problemi correlati