tags:

views:

317

answers:

3

In R, the plot() function takes a pch argument that controls the appearance of the points in the plot. I'm making scatterplots with tens of thousands of points and prefer a small, but not too small dot. Basically, I find pch='.' to be too small, but pch=19 to be too fat. Is there something in the middle or some way to scale the dots down somehow?

+8  A: 

Try the cex argument:

?par

  • cex
    A numerical value giving the amount by which plotting text and symbols should be magnified relative to the default. Note that some graphics functions such as plot.default have an argument of this name which multiplies this graphical parameter, and some functions such as points accept a vector of values which are recycled. Other uses will take just the first value if a vector of length greater than one is supplied.
rcs
i don't think i would ever use 'cex' to control symbol size unless i had no other option. It only works some of the time; the rule is that when 'cex' is set via 'par', it affects the size of (most) text on the plot, when set inside 'plot', 'cex' affects only symbol size. So if you mis-apply that rule then not only is your symbol size not changed, but you now have multiple parameters affecting text size (cex.axis, cex.lab, cex.main, and cex.sub--all do the same job as 'cex', only piece-wise). Code like that is difficult to maintain and extend.
doug
+5  A: 

pch=20 returns a symbol sized between "." and 19. It's a "filled" symbol (which is probably what you want).

Aside from that, even the base graphics system in R allows a user fine-grained control over symbol size, color, and shape. E.g.,

dfx = data.frame(ev1=1:10, ev2=sample(10:99, 10), ev3=10:1)

symbols(x=dfx$ev1, y=dfx$ev2, circles=dfx$ev3, inches=1/3, ann=F, bg="steelblue2", fg=NULL)

alt text

doug
+4  A: 

As rcs stated, cex will do the job in base graphics package. I reckon that you're not willing to do your graph in ggplot2 but if you do, there's a size aesthetic attribute, that you can easily control (ggplot2 has user-friendly function arguments: instead of typing cex (character expansion), in ggplot2 you can type e.g. size = 2 and you'll get 2mm point).

Here's the example:

### base graphics ###
plot(mpg ~ hp, data = mtcars, pch = 16, cex = .9)

### ggplot2 ###
# with qplot()
qplot(mpg, hp, data = mtcars, size = I(2))
# or with ggplot() + geom_point()
ggplot(mtcars, aes(mpg, hp), size = 2) + geom_point()
# or another solution:
ggplot(mtcars, aes(mpg, hp)) + geom_point(size = 2)
aL3xa
And, if you're really plotting tens of thousands of points, ggplot2 has several alternative ways to make that look nice - alpha adjustments, hex bins, contour plots, etc. Check out pages 72-77 of the ggplot2 book, if there's one in your library or if your library has electronic access to Springer books (I think most of the R books are in there).
Matt Parker
Yup... I especially use `alpha` in scatterplots, to avoid overplotting.
aL3xa