How many linetypes are available in ggplot2?

1,077 views
Skip to first unread message

Jennifer Krauel

unread,
Apr 11, 2013, 11:25:56 AM4/11/13
to ggp...@googlegroups.com
I have a line plot with 9 different lines.  I have to make it readable in black and white, so even though it's not going to be very readable I was hoping to use different linetypes for each.  In some places the documentation states there are only 6 linetypes, but I found this reference that made me think there were 12:

If there are truly only 6 linetypes, then in theory I guess I could use combinations of different line types and sizes.  But I'm really not sure how to do that short of having a separate geom_line call for each variable, which seems like a hack.  If I use both scale size manual and scale linetype  manual, how do I know if the size is going to correspond to the points or the lines?  It's a very flexible system but confusing.

Thanks,
Jennifer

Roman Luštrik

unread,
Apr 11, 2013, 11:30:08 AM4/11/13
to Jennifer Krauel, ggp...@googlegroups.com
I don't know what you're trying to present, but have you considered facetting? 12 facets isn't that many if you fold them 6x2 or 4x3, especially if you count for the fact that there will be zero information loss, compared to 12 sort of different line/point type combinations.

Cheers,
Roman




--
--
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
 
---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
In God we trust, all others bring data.

Jennifer Krauel

unread,
Apr 11, 2013, 12:03:55 PM4/11/13
to ggp...@googlegroups.com, Jennifer Krauel
Thanks, Roman.  I actually have three sets of graphs, bat activity sorted by small, medium, and large parks, with 9, 7, and 6 lines in each, respectively.  Or I could just do 22 different facets, but grouping them by size makes sense biologically.  I am trying to show activity over different seasons in each park.

Strangely, however, by playing around with this I can make more than six linetypes appear.  They disappear when I put in the scale_linetype_manual command to set up the legend.  I of course need this command to display a legend!  I'm passing just a series 1:9 as the "values" in that command.  Perhaps these are not the correct numbers to use to access more than six linetypes?

I'm going to put in sample code below.  It includes my melted dataset and the ggplot commands.  If you run just the first three lines of the ggplot command you should see more than 6 linetypes.  But adding in the manual linetype command switches to a repeating set of six linetypes.

library(ggplot2)
# load in the melted data structure with just the "small" parks

melted.sm <- structure(list(code = structure(c(1L, 2L, 4L, 5L, 9L, 10L, 11L,
12L, 19L, 1L, 2L, 4L, 5L, 9L, 10L, 11L, 12L, 19L, 1L, 2L, 4L,
5L, 9L, 10L, 11L, 12L, 19L, 1L, 2L, 4L, 5L, 9L, 10L, 11L, 12L,
19L), .Label = c("BG", "BH", "BV", "CH", "EH", "GB", "GC", "GG",
"GH", "GV", "HH", "KH", "LM", "MD", "ML", "P1", "P2", "PL", "TH",
"TP", "TR", "VU"), class = "factor"), park = structure(c(4L,
3L, 5L, 6L, 7L, 10L, 12L, 13L, 20L, 4L, 3L, 5L, 6L, 7L, 10L,
12L, 13L, 20L, 4L, 3L, 5L, 6L, 7L, 10L, 12L, 13L, 20L, 4L, 3L,
5L, 6L, 7L, 10L, 12L, 13L, 20L), .Label = c("B. Vista", "Bayview",
"Bernal", "Billygoat", "Corona", "Edgehill", "GG Hts", "GG Pk.",
"Glen Cyn", "Grandview", "Grn. Belt", "Hawk", "Kite", "Lk. Merced",
"Lobos Ck.", "McLaren", "Mt. David.", "Mtn. Lake", "Pine Lk.",
"Tank", "TP Res.", "Twin Pks."), class = "factor"), period = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L), .Label = c("May", "September", "December", "March"
), class = "factor"), passes = c(0L, 1L, 0L, 13L, 36L, 4L, 6L,
25L, 3L, 48L, 6L, 29L, 35L, 69L, 36L, 5L, 8L, 5L, 2L, 0L, 2L,
0L, 45L, 0L, 6L, 0L, 0L, 5L, 2L, 0L, 3L, 14L, 0L, 0L, 0L, 1L)), .Names = c("code",
"park", "period", "passes"), row.names = c(NA, 36L), class = "data.frame")

# some variables for the manual linetypes command

sm.linetypes <- c(1:9) 

sm.codes <- c("BG", "BH","CH", "EH", "GH", "GV", "HH", "KH", "TH")

sm.names <- c("Billygoat","Bernal","Corona","Edgehill","GG Hts","Grandview","Hawk","Kite","Tank")

ggplot (data = melted.sm, aes(x = period, y = log(passes+1), group=code)) +
          geom_line(aes(linetype=code)) +
          geom_point() +
         labs(y = "Log Bat Passes", 

                 x = "Season") + 
          scale_linetype_manual ("Small\nParks\nActivity"), 
                                              breaks = sm.codes, 
                                              labels = sm.names, 
                                              values = sm.linetypes)

Ista Zahn

unread,
Apr 11, 2013, 2:16:15 PM4/11/13
to Jennifer Krauel, ggplot2
Hi Jennifer,

You can have a lot of linetypes, but the problem is that you won't be
able to visually distinguish them once you have more than a handful.
For example, here is a plot with 81 linetypes:

library(ggplot2)
lineTypes <- apply(expand.grid(1:3, 1:3, 1:3, 1:3), 1, paste0, collapse="")

tmp <- data.frame(x = rep(1:10, each=length(lineTypes)),
y= rep(1:length(lineTypes), times=10),
l = rep(lineTypes, times=10))


ggplot(tmp,
aes(x=x,
y=y,
linetype=l)) +
geom_line(aes(group=l)) +
scale_linetype_manual(values = tmp$l)

So this is technically possible, but not practically very useful.

For the technical details on how my lineTypes example works see the
"Line Type Specification" of

?par

Best,
Ista

On Thu, Apr 11, 2013 at 12:03 PM, Jennifer Krauel

Brian Diggs

unread,
Apr 11, 2013, 2:47:59 PM4/11/13
to Jennifer Krauel, ggplot2
There are either 6, 13, or (approx) 2^32 different line types, depending
on how you count/access them.

There are 6 (really, 7) named/numbered linetypes as described in the lty
portion of the par help page:

0=blank, 1=solid (default), 2=dashed, 3=dotted, 4=dotdash, 5=longdash,
6=twodash

These can be addressed by a number or the corresponding string.

The default palette of linetypes in the scales package has 13 entries:

"solid", "22", "42", "44", "13", "1343", "73", "2262", "12223242",
"F282", "F4448444", "224282F2", "F1"

This palette shows the other way of specifying a linetype: an even
digited hex string of up to 8 (non-zero) hex digits. "This is done with
a string of an even number (up to eight) of characters, namely
_non-zero_ (hexadecimal) digits which give the lengths in consecutive
positions in the string. For example, the string '"33"' specifies three
units on followed by three off and '"3313"' specifies three units on
followed by three off followed by one on and finally three off." (From
"Line Type Specification" in the par help page). That also gives the
definitions of the named patterns: "The five standard dash-dot line
types ('lty = 2:6') correspond to 'c("44", "13", "1343", "73", "2262")'"

So you can specify as many linetypes as you want with the hex string
notation. However, the question of making variants that are
distinguishable is another matter. I don't know off hand of work similar
to ColorBrewer for line types. However, since the default ggplot palette
has 13, I would assume those to be sufficiently distinguishable and
would use those.

> Thanks,
> Jennifer
>


--
Brian S. Diggs, PhD
Senior Research Associate, Department of Surgery
Oregon Health & Science University
Reply all
Reply to author
Forward
0 new messages