Set linetype manually for each line

5,598 views
Skip to first unread message

thomas

unread,
Nov 15, 2010, 7:38:42 PM11/15/10
to ggplot2
Hi there,

is there a way to manually set which linetype ggplot2 should use for a
specific line?

I have 4 lines and set the linetype simply by saying:
ggplot(data,aes(x=ID,y=value,group=Section)) +
geom_line(aes(linetype=Section,colour = Section))

The problem is that ggplot kind of selects the wrong linetypes for the
lines. Attached is the end result.
http://shareimage.org/images/w58rjvo5052jb5p3pie4.jpg

I would rather have Total having the linetype of Abstract for
example.

Anybody knows how that could be done?

Thank you,
Thomas

Joshua Wiley

unread,
Nov 15, 2010, 8:07:44 PM11/15/10
to thomas, ggplot2
Hi Thomas,

I see two options. You can either manually specify the linetype for
each level of your factor, OR you can reorder your factor to match
your desired line types (obviously somewhat less flexible, but can be
easier code wise). Example code follows.

Cheers,

Josh


library(ggplot2)
## data
dat <- data.frame(x=1:10, y=1:10,
group = factor(rep(letters[1:5], each = 2)))

## default lines
ggplot(data = dat, aes(x = x, y = y, linetype = group)) +
geom_line()

## manually set liens
ggplot(data = dat, aes(x = x, y = y, linetype = group)) +
geom_line() +
scale_linetype_manual(name = "Linetype",
value = c("a" = 2, "b" = 3, "c" = 4, "d" = 1, "e" = 5))

## reorder factor so non-alphabetical
dat$group <- factor(dat$group, levels = c("d", "a", "b", "c", "e"))

## new default lines
ggplot(data = dat, aes(x = x, y = y, linetype = group)) + geom_line()

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

--
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/

thomas

unread,
Nov 16, 2010, 7:53:20 AM11/16/10
to ggplot2
Hi Joshua,

thank you very much.
You are using factor and are reordering the levels in your last
method.
If i don't provide a factor, would ggplot2 internally create a factor
and use its levels to create a mapping between the linetype and the
unique values? And by handing ggplot the factor directly you make it
skip this step?

Thomas

Joshua Wiley

unread,
Nov 16, 2010, 11:44:09 AM11/16/10
to thomas, ggplot2
Hi Thomas,

I just sort of assumed you were using a factor, I guess. AFAIK things
like the scale training and mapping is done using factors for discrete
data. When factor() is called on data, the levels are, by default,
alphabetical, but if you have already created factor class data and
specified a different ordering for levels than those will be kept.
The linetype and legend order are, again AFAIK, determined by the
levels of the factor.

Take that with some caution though, I have not actually watched all
the different steps between the call and the final graph.

HTH,

Josh

James McCreight

unread,
Nov 16, 2010, 12:11:36 PM11/16/10
to Joshua Wiley, thomas, ggplot2
it appears that the scale_linetype() is applying factor() to its grouping variable, which defaults to alphabetical order. but i would play it save and make it a factor on your own. generally, when "discretizing" variables, the burden falls on the user. but as you see, this affords the user some amount of flexibility.  (same goes with using cut() to get factors for discrete scales.) 

the first example in http://had.co.nz/ggplot2/scale_linetype.html with some extras

ec_scaled <- data.frame( date = economics$date, rescaler(economics[, -(1:2)], "range") ) 
ecm <- melt(ec_scaled, id = "date") 

qplot(date, value, data=ecm, geom="line", group=variable) 

ecm$facord<-factor(ecm$variable,levels=c('uempmed','pop','unemploy','psavert'))
qplot(date, value, data=ecm, geom="line", linetype=facord)

ecm$char<-as.character(ecm$facord)
qplot(date, value, data=ecm, geom="line", linetype=char)

-
******************************************************************************
James McCreight                                  mccreigh at colorado.edu
NASA Postdoctoral Fellow
cell: (831) 261-5149

thomas

unread,
Nov 16, 2010, 8:14:23 PM11/16/10
to ggplot2
Hi Joshua,

first time I read about factor() I didn't really understand what it is
good for.
Thanks for the explanation, that clears things up quite a bit.

Thanks,
Thomas

thomas

unread,
Nov 16, 2010, 8:19:03 PM11/16/10
to ggplot2
Hi James,

thank you, I will play around with factor() from now on.

Best,
Thomas

On Nov 16, 6:11 pm, James McCreight <mccre...@colorado.edu> wrote:
> it appears that the scale_linetype() is applying factor() to its grouping
> variable, which defaults to alphabetical order. but i would play it save and
> make it a factor on your own. generally, when "discretizing" variables, the
> burden falls on the user. but as you see, this affords the user some amount
> of flexibility.  (same goes with using cut() to get factors for discrete
> scales.)
>
> the first example inhttp://had.co.nz/ggplot2/scale_linetype.htmlwith some
> extras
>
> ec_scaled <- data.frame( date = economics$date, rescaler(economics[,
> -(1:2)], "range") )
> ecm <- melt(ec_scaled, id = "date")
>
> qplot(date, value, data=ecm, geom="line", group=variable)
>
> ecm$facord<-factor(ecm$variable,levels=c('uempmed','pop','unemploy','psaver t'))
> qplot(date, value, data=ecm, geom="line", linetype=facord)
>
> ecm$char<-as.character(ecm$facord)
> qplot(date, value, data=ecm, geom="line", linetype=char)
>
> On Tue, Nov 16, 2010 at 9:44 AM, Joshua Wiley <jwiley.ps...@gmail.com>wrote:
>
>
>
>
>
>
>
>
>
> > Hi Thomas,
>
> > I just sort of assumed you were using a factor, I guess.  AFAIK things
> > like the scale training and mapping is done using factors for discrete
> > data.  When factor() is called on data, the levels are, by default,
> > alphabetical, but if you have already created factor class data and
> > specified a different ordering for levels than those will be kept.
> > The linetype and legend order are, again AFAIK, determined by the
> > levels of the factor.
>
> > Take that with some caution though, I have not actually watched all
> > the different steps between the call and the final graph.
>
> > HTH,
>
> > Josh
>
> > On Tue, Nov 16, 2010 at 4:53 AM, thomas <thk.k...@gmail.com> wrote:
> > > Hi Joshua,
>
> > > thank you very much.
> > > You are using factor and are reordering the levels in your last
> > > method.
> > > If i don't provide a factor, would ggplot2 internally create a factor
> > > and use its levels to create a mapping between the linetype and the
> > > unique values? And by handing ggplot the factor directly you make it
> > > skip this step?
>
> > > Thomas
>
> > > --
> > > 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<ggplot2%2Bunsu...@googlegroups.com >
> > > More options:http://groups.google.com/group/ggplot2
>
> > --
> > Joshua Wiley
> > Ph.D. Student, Health Psychology
> > University of California, Los Angeles
> >http://www.joshuawiley.com/
>
> > --
> > 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<ggplot2%2Bunsu...@googlegroups.com >
> > More options:http://groups.google.com/group/ggplot2
>
> --
> -
> *************************************************************************** ***
> James McCreight                                  mccreigh at
> colorado.<mccre...@colorado.org>
> edu
Reply all
Reply to author
Forward
0 new messages