I'm trying to build charts control. I want to explicit that green line (abline1) is central line and red line (abline2 and abline3) are the limits control.
Can you help me?
library(ggplot2)
amostra <- rnorm(30, 10)
qplot(y = amostra, x = seq_along(amostra), ylim = c(5,15), main = "Gráfico de Controle", xlab = 'Período', ylab = 'Valores') + geom_line() +
geom_abline(aes(slope=0, intercept=10), colour = 'green', size=1) +
geom_abline(aes(slope=0, intercept=7), colour='red', size = 1) +
geom_abline(aes(slope=0, intercept=13), colour='red', size = 1) +
theme(plot.title = element_text(hjust = 0.5))
If you want a legend, then you need an aesthetic mapping. Move color in the aes()
and add a scale_color_identity()
. You can do this with
qplot(y = amostra, x = seq_along(amostra), ylim = c(5,15), main = "Gráfico de Controle", xlab = 'Período', ylab = 'Valores') + geom_line() +
geom_abline(aes(slope=0, intercept=10, colour = 'green'), size=1) +
geom_abline(aes(slope=0, intercept=7, colour='red'), size = 1) +
geom_abline(aes(slope=0, intercept=13, colour='red'), size = 1) +
theme(plot.title = element_text(hjust = 0.5)) +
scale_color_identity(labels=c("central","limits"), guide="legend")
This is a rather bad hack, but to force the lines to be in the legend, you could try to draw them using the geom_errorbarh()
function. This will plot errorbars. If you make them as wide as the plot while setting the plot width explicitly with scale_x_continuous()
, you can have horizontal bars that are shown in figure legend.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.