r - Geom_line not plotting colors correctly for characters but fine for numerics -
i'm confused. have following data:
require("ggplot2") treatment=c(rep("no", 10), rep("yes", 30), c(rep("no", 10)), rep("yes", 10), rep("no", 30), c(rep("yes", 10))) dat=data.frame(time=rep(1:50, 2), group=rep(c("group 1", "group 2"), each=50), treatment=treatment)
why not work:
ggplot(dat, aes(x=time, y=group, color=treatment))+ geom_line()
but works (group 2's colors change correctly)?
ggplot(dat, aes(x=time, y=group, color=as.numeric(as.factor(treatment))))+ geom_line()
you need add group=group
clause aes
function want, othewise doesn't handle factors correctly:
require("ggplot2") treatment=c(rep("no", 10), rep("yes", 30), c(rep("no", 10)), rep("yes", 10), rep("no", 30), c(rep("yes", 10))) dat=data.frame(time=rep(1:50, 2), group=rep(c("group 1", "group 2") each=50), treatment=treatment) ggplot(dat, aes(x=time, y=group, color=treatment,group=group))+ geom_line()+ labs(title="group")
Comments
Post a Comment