gpt4 book ai didi

r - 带有填充点符号和图例的绘图函数

转载 作者:行者123 更新时间:2023-12-03 10:17:04 25 4
gpt4 key购买 nike

我想用相应的图例绘制两个不同颜色和点样式的函数,在普通 R .

我有几个问题:

  • 我正在使用 pch=21pch=22 .我的理解是它们是“填充”的符号。它们确实在图例中按预期填充,但它们在图表本身上看起来是空心的。怎么了?
  • 我可以在不手动指定网格的情况下在点之间获得更多空间吗?也许通过选择要打印的点数?
  • 随意添加您想要的任何类型的建议。我对 R 很陌生。特别是,有没有更好的方法来绘制两个函数?例如通过定义函数向量?难道没有一种方法可以在纯 R 中自动生成图例而无需指定颜色和形状?

  • 这是我的代码:
    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,我找到了解决我的几个问题的方法。为了记录,我想在这里总结一下:
  • 要在图形上获得填充符号,您需要同时指定 col(颜色)和 bg(背景)。即使对于 pch=21 也是如此和 pch=22 ,不会自动被指定的颜色填充。要在图例中获得填充符号,您需要同时指定 col 和 pt.bg。在这里,光靠 bg 是不够的。
  • 使用 par(new=TRUE) 是一个非常糟糕的主意与 axes=FALSE ,正如我最初所做的那样,因为重叠的图不一定使用相同的坐标系。第二个图的预期函数是 (100^2-x^2)/2但我无意中写了(1-x^2)/2并没有意识到这一点,因为我设置了轴 = FALSE。

  • 总而言之,这是我首选的解决方案:
    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 所示的图。非常感谢你们两位的帮助。

    最佳答案

    我想也许你会更幸运地使用 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你参与的黑客攻击。

    关于r - 带有填充点符号和图例的绘图函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15819762/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com