4

I know I can create a plot with line and dots using the type = "o" argument in the plot command. I would like some more control over this -- I want to be able to draw the "o" as full dots, with black border and fill-in color of my choice, of customized size and of a different color than the line. Same for the line, I want to make it thicker, and of my choice of color. How would I go on about doing that?

What I found until now is just a plain

 plot(y, type= "o")

which is too poor for my needs.

I am not interested in using ggplot, but instead use the internal plot library of R. Any help appreciated.

flodel
  • 87,577
  • 21
  • 185
  • 223
kloop
  • 4,537
  • 13
  • 42
  • 66
  • 2
    Take a careful look at `?points`, including running the examples (`example("points")`) ... – Ben Bolker Mar 31 '12 at 17:26
  • thanks. I will look at ?points, but the examples do not seem to present what I need. they do have a solid circle, but without a border. – kloop Mar 31 '12 at 17:29
  • Look more carefully, at the second plot (light blue background, dark blue edge), third plot (point types 21-25; yellow background, red edge) – Ben Bolker Mar 31 '12 at 18:02

2 Answers2

15

All the information you need should be present in ?plot and ?points, as suggested by @BenBolker. In particular, you want to be using pch=21, and specifying background colour with the bg argument, size with cex, and line width with lwd.

If you want the line to be a different thickness to the point borders, you need to plot the line first, and then overlay the points.

For example:

y <- sample(10)
plot(y, lwd=6, type='l')
points(y, bg='tomato2', pch=21, cex=3, lwd=3) # tomato2 is a personal fave

enter image description here

You could also provide a vector of lwd, cex and col to the points call:

plot(y, lwd=6, type='l')
points(y, bg=rainbow(10), pch=21, cex=seq(1, by=0.2, length.out=10), 
       lwd=seq(2, by=1, length.out=10))

enter image description here

jbaums
  • 27,115
  • 5
  • 79
  • 119
9

You could use layering (I don't work in base too much any more as a social researcher I love the facet_grid of ggplot, so there may be a better way) as in:

x <- sort(rnorm(25))
y <- sort(rnorm(25))
z <- as.factor(sample(LETTERS[1:5], 25, r=TRUE))

plot(x, y, pch = 19, cex = 1.3)
par(new = TRUE)
plot(x, y, pch = 19, cex = 1, col = z)

Which gives you: enter image description here

Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519
  • I suggest adding opar <- par(new = TRUE) before the second call to 'plot', and par(opar) after (to prevent surprises in the future). Or replace the second call to 'plot' with a call to 'points' (with the same arguments). – Matthew Lundberg Mar 31 '12 at 18:48
  • 2
    Layering is unnecessary to achieve points with coloured fill. `bg` and `pch=21` will do the trick. – jbaums Apr 01 '12 at 05:28