带有填充点符号和图例的绘图函数 [英] plot functions with filled point symbols and legend

查看:74
本文介绍了带有填充点符号和图例的绘图函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to plot two functions in different colors and point styles with a corresponding legend, in plain R.

我有几个问题:

  1. 我正在使用pch=21pch=22.我的理解是它们是填充"符号.它们的确显示了图例中预期的填充状态,但是在图形本身上显示为空心.怎么了?

  1. I am using pch=21 and pch=22. My understanding is that they are "filled" symbols. They do appear filled as expected in the legend, but they appear hollow on the graph itself. What's wrong?

是否可以在不手动指定网格的情况下在点之间获得更多空间?也许通过选择要打印的点数?

Can I get more space between the points without specifying a grid manually? maybe by selecting the number of points to be printed?

随时添加您想要的任何建议.我对R非常陌生.特别是,是否有更好的方法来绘制两个函数?例如通过定义函数向量?并没有一种方法可以自动生成图例而不用指定普通R中的颜色和形状吗?

Feel free to add any kind of advice you'd like. I'm very new to R. In particular, is there a better way to plot two functions? e.g. by defining vectors of functions? and wouldn't there be a way for the legend to be automagically generated without having to specify colors and shapes, in plain 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;-)

After various explorations, I opted to use the par(new=TRUE) "trick" to superimpose the two functions (rather than, say, using matplot or a combination of plot and points or layout). Was that a bad move to begin with? ( yes, very bad, see below) +1 if you don't ask me to read the manual ;-)

解决方案摘要

多亏了joran和Didzis Elferts,我才得以解决我的一些问题.作为记录,我想在这里总结一下:

Thanks to joran and Didzis Elferts, I got a solution to several of my problems. For the record, I'd like to summarize here:

  1. 要在图形上获得实心符号,您需要同时指定col(颜色)和bg(背景).即使对于pch=21pch=22,也是如此,它们不会自动被指定的颜色填充.要在图例中获得填充符号,您需要同时指定col和pt.bg.在这里,仅靠bg还不够好.

  1. To get filled symbols on the graph, you need to specify both col (color) and bg (background). This is true even for pch=21 and pch=22, which do not automatically get filled by the color specified. To get filled symbols in the legend, you need to specify both col and pt.bg. Here, bg alone is not good enough.

par(new=TRUE)axes=FALSE一起使用是一个非常糟糕的主意,因为重叠的图不一定使用相同的坐标系.第二个绘图的预期功能是(100^2-x^2)/2,但是我无意中写了(1-x^2)/2,但由于我将axis = FALSE设置了而没有意识到.

it's a very bad idea to use par(new=TRUE) with axes=FALSE, as I had done initially, because, the overlaid plots do not necessarily use the same coordinate system. The intended function for the second plot was (100^2-x^2)/2 but I inadvertently wrote (1-x^2)/2 and did not realize it because I had set axes=FALSE.

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

All in all, here is my preferred solution:

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所示的情节.非常感谢你们俩的帮助.

This yields a plot like the one shown by joran. Thanks a lot to both of you for your help.

推荐答案

我认为使用curve可能会带来更好的运气:

I think maybe you'd have better luck using 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))

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

Note that I had to tweak your functions slightly, to get output that matched your image.

为避免分别指定颜色和填充(这通常是R中的处理方式),我使用了一些较旧的旧版"符号.使用curve通常可以更轻松地绘制函数或表达式.它还为您提供了一种更方便的方法来指定要评估的点的网格.它还有一个add参数,使您可以跳过自己从事的笨拙的par黑客行为.

To avoid specifying color and fill separately (which in general is how things are done in R) I used some older "legacy" symbols. Using curve is often much simpler for plotting functions or expressions. It also gives you a more convenient way to specify the grid of points to evaluate on. It also has an add argument that allows you to skip the awkward par hacking you engaged in.

这篇关于带有填充点符号和图例的绘图函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆