views:

56

answers:

3

I would like to make a scatter plot using R base graphics, though I can use ggplot if it is necesary. I have a data frame containing the x and y coordinates, and also two other covariates, call them a and b. I would like the size of each point to vary with a (high values of a -> larger points) and the brightness/solidity of each point to vary with b (so that those points with low enough b are practically invisiable). Does anyone have any advice on how to do this? The documentation for R graphics seems comprehensive, but so general and high-level that I scarcely know where to begin. Thanks for your help

A: 

This comes up often in R-help such as here. Basically, cex will be the argument of interest for changing the size of the points. For changing color of the points I would suggest something like setting col=heat.colors() or making your own color list and define as a factor before plotting.

Stedy
A: 

The cex parameter is a quick and dirty way of changing the size of the plotting characters, but you do need to work quite a bit if you want to have an informative scale etc. symbols() is one way to do this more accurately with base graphics.

Using some dummy data, here is one plot using cex and one using symbols():

## dummy data first
set.seed(123)
dat <- data.frame(X = rnorm(10), Y = rnorm(10,2,2), a = runif(10), b = runif(10))

## plot using cex
plot(Y ~ X, data = dat, cex = 10 * a)

## plot using symbols()
with(dat, symbols(X, Y, circles = a))

You can colour the points using the col parameter in the cex example or using the fg argument in the symbols case. The trick will be get an appropriate scale. In our dummy data, b is on the interval (0,1], so our scale might be to break this into 5 categories and use one of five plotting colours.

## Eg using heat.colors(5)
cut.pts <- seq(0, 1, by = 0.1)
cuts <- with(dat, cut(b, cut.pts))
with(dat, symbols(X, Y, circles = a, bg = heat.colors(5)[cuts]))

As you see, it is important to choose appropriate colours though as here, one colour is the same as the background.

Doing this with ggplot2 is likely going to be much simpler as all the heavy lifting has already been done for you by Hadley Wickham.

## Eg using ggplot
require(ggplot2)
p <- ggplot(dat, aes(X, Y, colour = b, size = a))
p + geom_point()

HTH

Gavin Simpson
+2  A: 

The sample line below shows how to use size and alpha simultaneously in a plot

plot(1:6, sample(6), cex = sample(6)/2, col = rgb(0,0,0, sample(6)/6), pch = 19)
John
+1 for conciseness
Stedy