I have some problems by looping "geom_line" functions.
I have this code, that Works ok for me.
variables<-c("irritabilidad","duracionsuenho")
ggplot(dfu) +
geom_line(data = dfu, aes(x = dfu[,c("fecha")], y = dfu[,c(variables[1])],color = "1") ) +
geom_line(data = dfu, aes(x = dfu[,c("fecha")], y = dfu[,c(variables[2])],color = "2") ) +
scale_color_manual(name="Legenda",values=rainbow(length(variables)),labels=variables) +
xlab('Fecha') +
ylab('Variables')
The result :
Now, Im trying to add "geom_line" by looping all the variables:
variables<-input$variables
variables<-c("irritabilidad","duracionsuenho")
c<-0
p<-ggplot(dfu)
for (x in variables){
c<-c+1
p<-p+geom_line(data = dfu, aes(x = dfu[,c("fecha")], y = dfu[,c(x)],color = as.character(c)) )
}
p<-p+scale_color_manual(name="Variables",values=rainbow(length(variables)),labels=variables)
p<-p+xlab('Fecha')
p<-p+ylab('Variables')
p
And this is the result:
I need to loop because i have several variables. Can you tell me what is wrong? Many Thanks