首页 文章

如何为R中的图形定义更多线型?

提问于
浏览
10

为R中的图定义了6种线类型,定义为"dashed","longdash" ... Link

如果我要绘制更多6个系列,我该如何定义更多类型?可以基于软拷贝中的颜色来区分图形线,但是不适合于黑白打印 .

是否有更多选项可用,或者我需要根据参考链接中的线和点组合来定义它们?

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

一些谷歌搜索建议开/关模式也可以用2,4,6或8个字符(非零十六进制字符,1-9和a-f)的字符串指定,预设样式为“虚线” “=”44“,”dotted“=”13“,”dotdash“=”1343“,”longdash“=”73“,”twodash“=”2262“ .

但似乎用它来定义新的线型会受到很多打击和试验,这些线型将在B&W印刷上有所区别 .

编辑:

如果组合了点和线样式,我如何以可以输入的方式定义线类型集

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

非常感谢 .

2 回答

  • 0

    这可以使用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)
    
  • 11

    正如您在链接中提到的,合法值是字符串“空白”,“实心”,“虚线”,“点缀”,“dotdash”,“longdash”和“twodash” . 或者,可以使用数字0到6(0代表“空白”,1代表“实心”,......) .

    Moreover ,也可以使用最多 8 hexadecimal digits 定义线型的字符串(每个数字指定交错线和间隙的长度) .

    这里有一个例子,在 ggplot2 中使用 linetype aes相当于基础R中的 lty . 这样你就可以获得超过6种预定义类型 .

    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

    说明:

    "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:解决方案是从link采用的 .

相关问题