Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define more line types for graphs in R (custom linetype)?

Tags:

plot

r

ggplot2

There are 6 line types defined for plots in R defined as "dashed", "longdash" ... Link

How can I define more types if I have more that 6 series to plot ? The graph lines can be distinguished based on colour in the soft copy but is not suitable for black and white printing.

Are there more options available or do I need to define them based on combining lines and points as in the reference link?

plot(x, type = "b", pch = 0, lty = "dotted")

Some google search suggested that patterns of on/off can also be specified with strings of 2, 4, 6, or 8 characters (non-zero hexadecimal characters, 1–9 and a–f) and the pre-set styles are “dashed” = “44", “dotted” = “13”, “dotdash” = “1343”, “longdash” = “73”, “twodash” = “2262”.

But seems it will be a lot of hit and trial to use this to define new line types which will be distinguishable on B&W printing.

Edit :

If points and line styles are combined, how can I define the set of line types in such a way that they can be input as

plot(DF, ..., col = 1:ncol(DF), lty = 1:ncol(DF))
# where DF is the set of data to be plotted. 

Thanks a lot.

like image 860
Anusha Avatar asked Sep 11 '14 13:09

Anusha


People also ask

How do you specify a linetype in R?

line type (lty) can be specified using either text (“blank”, “solid”, “dashed”, “dotted”, “dotdash”, “longdash”, “twodash”) or number (0, 1, 2, 3, 4, 5, 6). Note that lty = “solid” is identical to lty=1.

How many line types are there in R?

The different line types available in R software are : “blank”, “solid”, “dashed”, “dotted”, “dotdash”, “longdash”, “twodash”. Note that, line types can be also specified using numbers : 0, 1, 2, 3, 4, 5, 6.

How do I change the linetype in R ggplot2?

To change the linetype for geom_vline, we can use linetype argument in geom_vline function of ggplot2 package. There are mainly six linetypes that can be used and these values are 0=blank, 1=solid (default), 2=dashed, 3=dotted, 4=dotdash, 5=longdash, 6=twodash.

What does Lty do in R?

You can use those for any type of graphics, like plotting for line charts or developing simple shapes. In R base plot functions, two options are available lty and lwd, lty stands for line types, and lwd for line width. The type of line you can be specified based on a number or a string.


1 Answers

As you have mentioned in your link, legal values are the strings "blank", "solid", "dashed", "dotted", "dotdash", "longdash", and "twodash". Alternatively, the numbers 0 to 6 can be used (0 for "blank", 1 for "solid", ...).

Moreover, one can also use strings that define the linetype with up to 8 hexadecimal digits (each digit specifying the length of interleaved lines and gaps).

Here an example , using linetype aes in ggplot2 equivalent to lty in base R. This way you can get more than the 6 predefined types.

library(ggplot2)
d=data.frame(lt=c("blank", "solid", "dashed", "dotted", 
                  "dotdash", "longdash", "twodash", "1F", 
                  "F1", "4C88C488", "12345678"))
ggplot() +
  scale_x_continuous(name="", limits=c(0,1), breaks=NULL) +
  scale_y_discrete(name="linetype") +
  scale_linetype_identity() +
  geom_segment(data=d, mapping=aes(x=0, xend=1, y=lt, yend=lt, linetype=lt))

enter image description here

Explanation:

"1F": dash length 1, gap length F (15)
"F1": dash length F (15), gap length 1
"4C88C488": dash (4), gap (C=12), dash (8), gap (8), dash (C=12), ...
"12345678": dash (1), gap (2), dash (3), gap (4), ...

PS: the solution is adopted from this link.

like image 57
agstudy Avatar answered Oct 07 '22 17:10

agstudy