problem with legend and scale_linetype_manual

2,374 views
Skip to first unread message

moleps

unread,
Jun 29, 2010, 3:54:11 PM6/29/10
to ggplot2
Dear all,

I´m struggling with assigning values to the linetype scale and have the legend rendered with different color and line for each of the keys. Using the (rather lengthy) command below I get


Error in col2rgb(colour, TRUE) : invalid color name 'GAM'


Any ideas what I´m doing wrong here?

..

Best,

//M


ggplot(b2,aes(as.numeric(bac),value))+facet_grid(GC~as2,scale="free_y")+geom_jitter(size=0.7,alpha=0.3,width=2)+geom_smooth(method="gam",formula=y~s(x),aes(colour="GAM",linetype="a"),size=0.5,se=F)+geom_smooth(method="lm",formula=y~x,aes(colour="OLS",linetype="p"),size=1)+scale_colour_manual("Method",c("OLS"="red","GAM"="blue"))+scale_linetype_manual("",c("a"=2,"p"=1))->z2


Hadley Wickham

unread,
Jun 29, 2010, 4:16:54 PM6/29/10
to moleps, ggplot2
Hi M,

Could you please provide a reproducible example? (maybe with some
spaces and newlines too ;)

Hadley

> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> Please provide a reproducible example: http://gist.github.com/270442
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>

--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

misha

unread,
Jun 29, 2010, 5:12:12 PM6/29/10
to ggplot2
I´m sorry.. Was hoping that someone could spot the mistake from the
supplied convoluted code...

n<-1000
b4<-
data.frame(gc=factor(sample(1:5,n,replace=T)),as2=factor(sample(1:5,n,replace=T)),gs=sample(3:15,n,replace=T),bac=abs(rnorm(n)))

ggplot(b4,aes(bac,gs))+facet_grid(gc~as2,scale="free_y")->b5

b5<-b5+geom_jitter(size=0.7,alpha=0.3,width=2)
b5<-
b5+geom_smooth(method="gam",formula=y~s(x ),aes(colour="GAM",linetype="p"),size=0.5,se=F)
b5<-
b5+geom_smooth(method="lm",formula=y~x,aes(colour="OLS",linetype="a"),size=1)
b5<-b5+scale_colour_manual("Method ",c("OLS"="red","GAM"="blue"))
+scale_linetype_manual("",c("a"=2,"p"=1))

b5

Error in col2rgb(colour, TRUE) : invalid color name 'GAM'

Obviously something is wrong with my understanding of the linetype-
scale as everything works fine as long as I leave it out....


Best,

//M



On Jun 29, 10:16 pm, Hadley Wickham <had...@rice.edu> wrote:
> Hi M,
>
> Could you please provide a reproducible example?  (maybe with some
> spaces and newlines too ;)
>
> Hadley
>
>
>
>
>
> On Tue, Jun 29, 2010 at 2:54 PM, moleps <mole...@gmail.com> wrote:
> > Dear all,
>
> > I´m struggling with assigning values to the linetype scale and have the legend rendered with different color and line for each of the keys. Using the (rather lengthy) command below I get
>
> > Error in col2rgb(colour, TRUE) : invalid color name 'GAM'
>
> > Any ideas what I´m doing wrong here?
>
> > ..
>
> > Best,
>
> > //M
>
> > ggplot(b2,aes(as.numeric(bac),value))+facet_grid(GC~as2,scale="free_y")+geo m_jitter(size=0.7,alpha=0.3,width=2)+geom_smooth(method="gam",formula=y~s(x ),aes(colour="GAM",linetype="a"),size=0.5,se=F)+geom_smooth(method="lm",for mula=y~x,aes(colour="OLS",linetype="p"),size=1)+scale_colour_manual("Method ",c("OLS"="red","GAM"="blue"))+scale_linetype_manual("",c("a"=2,"p"=1))->z2

Hadley Wickham

unread,
Jun 29, 2010, 5:26:47 PM6/29/10
to misha, ggplot2
It seems to be a problem with the legend, because this works:

ggplot(df, aes(gs, bac)) +
geom_point() +
geom_smooth(method = "gam", formula = y ~ s(x),
aes(colour = "GAM", linetype = "GAM"), se = F) +
geom_smooth(method = "lm", formula = y ~ x,
aes(colour = "OLS", linetype = "OLS"), se = F)

but this does not

ggplot(df, aes(gs, bac)) +
geom_point() +
geom_smooth(method = "gam", formula = y ~ s(x),
aes(colour = "GAM", linetype = "a"), se = F) +
geom_smooth(method = "lm", formula = y ~ x,
aes(colour = "OLS", linetype = "b"), se = F)


Hadley

moleps

unread,
Jun 29, 2010, 5:38:01 PM6/29/10
to Hadley Wickham, ggplot2
I agree that I get different linetypes using

> ggplot(df, aes(gs, bac)) +
> geom_point() +
> geom_smooth(method = "gam", formula = y ~ s(x),
> aes(colour = "GAM", linetype = "GAM"), se = F) +
> geom_smooth(method = "lm", formula = y ~ x,
> aes(colour = "OLS", linetype = "OLS"), se = F)


However I´d like to specify the linetype with the scale_linetype_manual command (mainly just to understand how scales can be put to use in the future)

scale_colour_manual("Method ",c("OLS"="red","GAM"="blue"))+scale_linetype_manual("",c("OLS"=2,"GAM"=1))

gives

Error in col2rgb(colour, TRUE) : invalid color name 'GAM'

Best

//M

moleps

unread,
Jun 30, 2010, 11:52:45 AM6/30/10
to Hadley Wickham, ggplot2
Upon further analyzing this I seem to be able to set both manual linetypes and color by themselves, but not the two of them together.


n<-1000
b4<-data.frame(gc=factor(sample(1:5,n,replace=T)),as2=factor(sample(1:5,n,replace=T)),gs=sample(3:15,n,replace=T),bac=abs(rnorm(n)))

ggplot(b4,aes(bac,gs))+facet_grid(gc~as2,scale="free_y")->b5

##focus on linetype works well
b5<-b5+geom_jitter(size=0.7,alpha=0.3,width=2)
b5<-b5+geom_smooth(method="gam",formula=y~s(x ),aes(linetype="GAM"),se=F,color="red")
b5<-b5+geom_smooth(method="lm",formula=y~x,aes(linetype="OLS"),color="blue")
b5+scale_linetype_manual("",c("OLS"=2,"GAM"=1))


##focus on color works well
b5<-b5+geom_jitter(size=0.7,alpha=0.3,width=2)
b5<-b5+geom_smooth(method="gam",formula=y~s(x ),aes(colour="GAM"),se=F,linetype=1)
b5<-b5+geom_smooth(method="lm",formula=y~x,aes(colour="OLS"),linetype=2)
b5+scale_colour_manual("",c("OLS"="blue","GAM"="red"))


##but two different manual scales dont work
b5+geom_jitter(size=0.7,alpha=0.3,width=2)
b5<-b5+geom_smooth(method="gam",formula=y~s(x ),aes(colour="GAM",linetype="GAM",size="GAM")se=F)
b5<-b5+geom_smooth(method="lm",formula=y~x,aes(colour="OLS",linetype="OLS",size="OLS"))
b5

#OK, but not able to set my preferences

b5+scale_colour_manual("Method ",c("OLS"="red","GAM"="blue"))+scale_linetype_manual("",c("OLS"=2,"GAM"=1))

doesnt work and returns

Error in col2rgb(colour, TRUE) : invalid color name 'GAM'

Does anyone else manage to incorporate more than one manual scale in a plot? I´ve tried with other manual scales as well but with errors...

Best,

//M


On 29. juni 2010, at 23.26, Hadley Wickham wrote:

Hadley Wickham

unread,
Jul 3, 2010, 9:08:03 AM7/3/10
to moleps, ggplot2
Could you try creating an absolutely minimal example for me? It will
help with tracking down the bug.

HAdley

Reply all
Reply to author
Forward
0 new messages