How to define more line types for graphs in R?

后端 未结 2 1800
萌比男神i
萌比男神i 2020-11-30 13:30

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

相关标签:
2条回答
  • 2020-11-30 14:14

    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.

    0 讨论(0)
  • 2020-11-30 14:33

    This works using pch = 1:ncol(DF)

    # sample data
    dat <- matrix(runif(40,1,20),ncol=10) 
    matplot(dat, type = "b", lty = "longdash", pch = 1:10, col = 1:10, lwd = 2)
    
    0 讨论(0)
提交回复
热议问题