代码之家  ›  专栏  ›  技术社区  ›  SeGa

如何在图例中包含自定义点符号

  •  2
  • SeGa  · 技术社区  · 6 年前

    考虑下面的数据/绘图,因为我只为 pch .

    plot(1:20, col='blue', pch=20); 
    points(2:22, col='red', pch='+', cex=2)
    points(4:24, col='green', pch=15)
    
    legend('bottomright',c('Blue','Red', 'Green'),   
           col=c("blue", "red", "green"), horiz=T, cex=0.8,
           pch=c(20,20,15), bg='white')
    

    但是当我包含一个字符值时,它仍然会绘制一个图例,但是使用 错误的点类型 .

    legend('bottom',c('Blue','Red', 'Green'),   
           col=c("blue", "red", "green"), horiz=F, cex=0.8,
           pch=c(20,'+',20), bg='white')
    

    当只使用字符值时,图例再次正确:

    legend('bottomleft',c('Blue','Red', 'Green'),   
           col=c("blue", "red", "green"), horiz=F, cex=0.8,
           pch=c('!','+','*'), bg='white')
    

    中间的传说就是问题所在。 如何使用正确的点类型绘制图例?

    我知道我可以用 pch=3 对于“+”符号,但如果我试图绘制一个“!”代替符号? plot

    2 回复  |  直到 6 年前
        1
  •  1
  •   Dave2e    6 年前

    ?points 可以将ascii码传递给要打印的符号。对于+的ascii码是43。

    plot(1:20, col='blue', pch=20); 
    points(2:22, col='red', pch='+', cex=2)
    points(4:24, col='green', pch=15)
    
    legend('bottomright',c('Blue','Red', 'Green'),   
           col=c("blue", "red", "green"), horiz=T, cex=0.8,
           pch=c(20,43,15), bg='white')
    

    以下是PCH的可接受值:

    整数:无符号。
    0:18:S兼容矢量符号。
    19:25:进一步的R矢量符号。
    26:31:未使用(和忽略)。
    32:127:ascii字符。
    仅在单字节区域设置中使用128:255本机字符,对于 符号字体。(128:159仅在Windows上使用。)

        2
  •  3
  •   Chris    6 年前

    您可以传入一个数字向量或要用作字符的符号。由于向量只能是一种数据类型,因此不能像尝试的那样混合使用这两种类型-在您的情况下,它将向量中的所有值强制为 character .

    以下是您应该指定的内容:

    legend('bottomright',c('Blue','Red', 'Green'),   
       col=c("blue", "red", "green"), horiz=T, cex=0.8,
       pch=list(20,3,15), bg='white')
    

    编辑:既然你想要 ! 特别是符号,你需要寻找它的pch值。在这种情况下,它是pch=33