Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

plot functions with filled point symbols and legend

Tags:

plot

r

legend

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

I have several questions:

  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?

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

  3. 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?

Here's my code:

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) ) 

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? (Edit: yes, very bad, see below) +1 if you don't ask me to read the manual ;-)

enter image description here

Edit: Summary of Solutions

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. 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.

  2. 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) ) 

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

like image 713
PatrickT Avatar asked Apr 04 '13 19:04

PatrickT


People also ask

What does PCH function do in R?

Plot character or pch is the standard argument to set the character that will be plotted in a number of R functions. Explanatory text can be added to a plot in several different forms, including axis labels, titles, legends, or a text added to the plot itself.

How do I plot different symbols in R?

Using special characters as Pch values In the R Language, we can use “+”, “*“,”-“,”. “,”#, “%”, and “o” for plotting different symbols.

How do you add a legend to a plot?

Set the DisplayName property as a name-value pair when calling the plotting functions. Then, call the legend command to create the legend. Legends automatically update when you add or delete a data series. If you add more data to the axes, use the DisplayName property to specify the labels.

Which function is used to draw points markers in a diagram?

Plot. The plot() function is used to draw points (markers) in a diagram. The function takes parameters for specifying points in the diagram.


1 Answers

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)) 

enter image description here

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

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.

like image 130
joran Avatar answered Oct 14 '22 13:10

joran