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() 

enter image description here

but works (group 2's colors change correctly)?

ggplot(dat, aes(x=time, y=group, color=as.numeric(as.factor(treatment))))+   geom_line() 

enter image description here

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") 

enter image description here


Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -