library(ggplot2)
set.seed(12)
pts <- c(rnorm(9,0,1), rnorm(12,1.5,0.8))
df <- data.frame(t=seq_along(pts), y=pts,
weco=c(rep("Normal",9),rep("Regra 4",12)))
ggplot(df, aes(t,y)) +
annotate("rect",xmin=-Inf,xmax=Inf,ymin=2,ymax=3,
fill="#f07050",alpha=.15) +
annotate("rect",xmin=-Inf,xmax=Inf,ymin=-3,ymax=-2,
fill="#f07050",alpha=.15) +
annotate("rect",xmin=-Inf,xmax=Inf,ymin=1,ymax=2,
fill="#f7c87e",alpha=.12) +
annotate("rect",xmin=-Inf,xmax=Inf,ymin=-2,ymax=-1,
fill="#f7c87e",alpha=.12) +
geom_line(color="white",linewidth=.7)+
geom_point(aes(color=weco),size=3)+
scale_color_manual(values=c("Normal"="#7ecff7","Regra 4"="#f7c87e"))+
geom_hline(yintercept=c(3,2,1,0,-1,-2,-3),
color=c("#f07050","#f7c87e","#a8f77e","#a8f77e",
"#a8f77e","#f7c87e","#f07050"),
linetype=c("dashed","dotted","dotted","solid",
"dotted","dotted","dashed"),linewidth=.8)+
annotate("segment",x=10,xend=17,y=-0.5,yend=-0.5,
color="#f7c87e",linewidth=1.2,
arrow=arrow(ends="both",length=unit(.15,"cm")))+
annotate("text",x=13.5,y=-0.85,
label="8 pts acima da LM (Regra 4)",
color="#f7c87e",size=3.5)+
labs(title="Gráfico de Controle com Detecção da Regra 4",
x="Amostra",y="Estatistica",color="")+
theme_minimal(base_size=13)