Hi,
Does this help at all? I've used rbind to concatenate the data.frames and then named values in scale_colour_manual to force them to be the right colour even when missing.
I've ignored the geom_point bits, as the question related to the line data, but these can also be identified within (or added to) a single DF.
Most of this code relates to making example data, the rbind and compound ggplot are all that is required. Except that you need to add a dataset identifier (dset below) to your records.
library(ggplot2)
# Some dummy data to play with...
data_black_lines <- data.frame(emp_code = rep(1:2,each=5),
date = rep(1:5,times=2),
cum = c(cumsum(runif(5)), cumsum(runif(5))),
dset = 'black_lines',
stringsAsFactors = FALSE)
data_green_lines <- data.frame(emp_code = rep(3:4,each=5),
date = rep(1:5,times=2),
cum = 1+c(cumsum(runif(5)), cumsum(runif(5))),
dset = 'green_lines',
stringsAsFactors = FALSE)
data_orange_lines <- data.frame(emp_code = rep(5:6,each=5),
date = rep(1:5,times=2),
cum = 1.5+c(cumsum(runif(5)), cumsum(runif(5))),
dset = 'orange_lines',
stringsAsFactors = FALSE)
data_red_lines <- data.frame(emp_code = rep(7:8,each=5),
date = rep(1:5,times=2),
cum = 2+c(cumsum(runif(5)), cumsum(runif(5))),
dset = 'red_lines',
stringsAsFactors = FALSE)
#
DF<-rbind(data_black_lines, data_green_lines, data_orange_lines, data_red_lines)
ggplot(DF,aes(x=date,y=cum,group=emp_code,colour=dset)) +
geom_line() +
scale_colour_manual(values=c(black_lines = 'black', green_lines = 'green',
orange_lines = 'orange', red_lines = 'red'))
#
# No orange data
DF1<-rbind(data_black_lines, data_green_lines, data_red_lines)
ggplot(DF1,aes(x=date,y=cum,group=emp_code,colour=dset)) +
geom_line() +
scale_colour_manual(values=c(black_lines = 'black', green_lines = 'green',
orange_lines = 'orange', red_lines = 'red'))
#
# Orange data creating function returned NULL
orange_data_null <- NULL
DF2<-rbind(data_black_lines, data_green_lines, orange_data_null, data_red_lines)
ggplot(DF2,aes(x=date,y=cum,group=emp_code,colour=dset)) +
geom_line() +
scale_colour_manual(values=c(black_lines = 'black', green_lines = 'green',
orange_lines = 'orange', red_lines = 'red'))
Regards,
Ron.