绘制具有填充点符号和图例的函数

时间:2013-04-04 19:12:12

标签: r plot legend

我想用不同颜色绘制两个函数,并使用相应的图例指定样式,在普通R

我有几个问题:

  1. 我正在使用pch=21pch=22。我的理解是它们是“填充”符号。它们确实在图例中按预期填充,但它们在图表上看起来是空心的。怎么了?

  2. 我可以在点之间获得更多空间而无需手动指定网格吗?也许通过选择要打印的点数?

  3. 随意添加您想要的任何建议。我对R.很新。特别是,有没有更好的方法来绘制两个函数?例如通过定义函数的向量?并且不会有一种方法可以自动生成图例而无需指定颜色和形状,在普通R?

  4. 这是我的代码:

    par(new=TRUE)
    p1 <- plot(function(x){ x^(2)/2 }
           , 0, 100
           , xlab = "x"
           , ylab = "y"
           , ylim = c(0,5000)
           , las = 1
           , type = "p"
           , cex = 0.8
           , pch = 21
           , col = "red"
    )
    par(new=TRUE)
    p2 <- plot(function(x){ (1-x^(2))/2 }
           , 0, 100
           , xlab = ""
           , ylab = ""
           , axes = FALSE
           , type = "p"
           , cex = 0.8
           , pch = 22
           , col = "blue"
    )
    par(new=TRUE)
    l <- legend( "topleft"
             , inset = c(0,0.4) 
             , cex = 1.5
             , bty = "n"
             , legend = c("A", "B")
             , text.col = c("red", "blue")
             , pt.bg = c("red","blue")
             , pch = c(21,22)
    )
    

    经过各种探索后,我选择使用par(new=TRUE)“技巧”来叠加这两个函数(而不是使用matplot或绘图和点或布局的组合)。这是一个糟糕的举动吗? (编辑:是的,非常糟糕,见下文)如果你不让我阅读手册,请+1; - )

    enter image description here

    修改:解决方案摘要

    感谢joran和Didzis Elferts,我解决了我的一些问题。为了记录,我想在这里总结一下:

    1. 要在图表上填充符号,您需要同时指定col(颜色)和bg(背景)。即使pch=21pch=22也是如此,它们不会自动按指定的颜色填充。要在图例中获取填充符号,您需要同时指定col和pt.bg.在这里,单独的bg还不够好。

    2. par(new=TRUE)axes=FALSE一起使用是一个非常糟糕的主意,正如我最初所做的那样,因为重叠的地块不一定使用相同的坐标系。第二个图的预期函数是(100^2-x^2)/2,但是我无意中写了(1-x^2)/2并且没有意识到它,因为我设置了轴= FALSE。

    3. 总而言之,这是我的首选解决方案:

      curve( x^2/2
        , from = 0
        , to = 100
        , n = 30
        , type = "p"
        , pch = 21 # alternatively pch=15 is a solid symbol
        , col = "red" # colors the outline of hollow symbol pch=21
        , bg = "red" # fills hollow symbol pch=21 with color
        , xlab = "x"
        , ylab = "y"
      )
      curve( (100^2-x^2)/2
        , from = 0
        , to = 100
        , n = 30
        , type = "p"
        , pch = 22  # alternative pch=16
        , col = "blue"
        , bg = "blue"
        , add = TRUE
      )
      legend( "topleft"
        , inset = c(0,0.4), 
        , cex = 1.5, 
        , bty = "n", 
        , legend = c("A", "B"), 
        , text.col = c("red", "blue"),
        , col = c("red", "blue"), 
        , pt.bg = c("red","blue")
        , pch = c(21,22)
      )
      

      这产生了一个像joran所示的情节。非常感谢你们两位的帮助。

2 个答案:

答案 0 :(得分:8)

我想也许你使用curve祝你好运:

curve(x^(2) / 2,from = 0,to = 100,col = 'red',type = 'p',pch = 16,n = 20)
curve((1-x^(2))/2 + 5000,from = 0,to = 100,col = 'blue',type = 'p',pch = 15,add = TRUE,n = 20)
legend("topleft", 
        inset = c(0,0.4), 
        cex = 1.5, 
        bty = "n", 
        legend = c("A", "B"), 
        text.col = c("red", "blue"),
        col = c("red", "blue"), 
        pch = c(16,15))

enter image description here

请注意,我必须略微调整您的功能,以获得与您的图像匹配的输出。

为了避免单独指定颜色和填充(通常是R中的操作方式),我使用了一些较旧的“遗留”符号。使用curve通常可以更简单地绘制函数或表达式。它还为您提供了一种更方便的方法来指定要评估的点网格。它还有一个add参数,允许您跳过您参与的笨拙par黑客攻击。

答案 1 :(得分:7)

您应在bg="red"中添加参数bg="blue"plot(),以获取具有特定颜色的填充符号。