I am trying to create a plot of timeseries data. I am manually specifying the colours and use a group aesthetic to group datapoints to lines.
The following code reproduces my problem:
If i use the code for plot p, i get the correct colours, but the grouping does not work. The lines are created mixing datapoints from both groups. Did i miss something?
If i split the data to plot the lines for both groups separately (like for plot q), the lines are correct, but the colours are wrong. Since the factors are the same, i would expect to still get black for 'WT' and red for 'KO'.
Any explanation and solution?
Thanks,
Herbert
------------------
df.d <- data.frame(pgroup=c(rep('WT', 100), rep('KO', 100)), step=rep(c(rep(1, 50), rep(2, 50)), 2),
timep=rep(c(1:50), 4), amplitude=c(sin(1:50), 2*sin(1:50), 3*sin(1:50), 4*sin(1:50)),
se=rep(c(0.18, 0.2, 0.22, 0.25), 50))
df.d$fact_step <- factor(df.d$step, levels=unique(df.d$step), labels=unique(df.d$step), ordered=TRUE)
df.d$fact_colour <- factor(df.d$pgroup, levels=groups, labels=groups, ordered=TRUE)
p <- ggplot(df.d) +
geom_line(aes(timep, amplitude, colour=fact_colour, group=fact_step)) +
scale_colour_manual(name='', values=c('black', 'red'), guide='legend',
breaks=c('WT', 'KO'), labels=c('WT', 'KO')) +
scale_fill_manual(name='', values=c('black', 'red'), guide='none')
print(p)
q <- ggplot() +
geom_line(data=subset(df.d, pgroup=='WT'), aes(timep, amplitude, colour=fact_colour, group=fact_step)) +
geom_line(data=subset(df.d, pgroup=='KO'), aes(timep, amplitude, colour=fact_colour, group=fact_step)) +
scale_colour_manual(name='', values=c('black', 'red'), guide='legend',
breaks=c('WT', 'KO'), labels=c('WT', 'KO')) +
scale_fill_manual(name='', values=c('black', 'red'), guide='none')
print(q)
df.d <- data.frame(pgroup=c(rep('WT', 100), rep('KO', 100)),
step=rep(c(rep(1, 50), rep(2, 50)), 2),
timep=rep(c(1:50), 4),
amplitude=c(sin(1:50), 2*sin(1:50), 3*sin(1:50), 4*sin(1:50)),
se=rep(c(0.18, 0.2, 0.22, 0.25), 50))
df.d$fact_step <- factor(df.d$step, levels=unique(df.d$step), ordered=TRUE)
df.d$fact_colour <- factor(df.d$pgroup, levels=c('WT', 'KO'), ordered=TRUE)
p <- ggplot(df.d) +
geom_line(aes(timep, amplitude, colour=fact_colour, group=fact_step)) +
scale_colour_manual(name='', values=c('black', 'red'), guide='legend',
breaks=c('WT', 'KO'), labels=c('Control', 'Gene'))
print(p)
q <- ggplot() +
geom_line(data=subset(df.d, pgroup=='WT'), aes(timep, amplitude, colour=fact_colour, group=fact_step)) +
geom_line(data=subset(df.d, pgroup=='KO'), aes(timep, amplitude, colour=fact_colour, group=fact_step)) +
scale_colour_manual(name='', values=c('black', 'red'), guide='legend',
breaks=c('WT', 'KO'), labels=c('Control', 'Gene'))
print(q)
-----------
df.d$fact_step <- paste(df.d$pgroup, df.d$step, sep="")
ggplot(df.d) +
geom_line(aes(timep, amplitude, colour=fact_colour, group=fact_step)) +
scale_colour_manual(name='', values=c('black', 'red'), guide='legend',
breaks=c('WT', 'KO'), labels=c('Control', 'Gene'))
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2
p <- ggplot(subset(df.d, step==1)) +
geom_line(aes(timep, amplitude, colour=fact_colour)) +
scale_colour_manual(name='', values=c('black', 'red'), guide='legend',
breaks=c('WT', 'KO'), labels=c('Control', 'Gene'))
print(p)
------
I would expect this separation is still active if i add the second separation "group=fact_step".
2. I still have no idea why the colours in my previous example with two separate geom_line statements gets exchanged. It would be very helpful to have an idea what i am missing here.
Thanks,
Herbert
To unsubscribe: email ggplot2+unsubscribe@googlegroups.com
More options: http://groups.google.com/group/ggplot2
---------
load('df.RData')
# i want the Control data to be plotted in black and Gene data in red
colours=c('black', 'red')
labels=c('Control', 'Gene')
# now some control plots
p <- ggplot(subset(df.d, pgroup==1), aes(timep, amplitude)) + geom_point()
print(p)
# -> 100 ms plot corresponding to Control data
q <- ggplot(subset(df.d, pgroup==2), aes(timep, amplitude)) + geom_point()
print(q)
# -> 80 ms plot corresponding to Gene data
s <- ggplot(df.d) +
geom_ribbon(aes(timep, ymin = amplitude-se, ymax = amplitude+se,
fill=fact_colour, group=fact_colint), alpha = 0.5) +
geom_line(aes(timep, amplitude, colour=fact_colour, group=fact_colint)) +
scale_colour_manual(name='', values=colours, guide='legend',
labels=labels) +
scale_fill_manual(name='', values=colours, guide='none')
print(s)
# -> the Control data (100 ms) is shown in red, but the legend indicates black
# -> the Gene data (80 ms) is shown in black, but the legend indicates red
------------