I am making a scatterplot matrix with three variables (x,y,z) with the points coloured different based on type (w). I want to add a legend to this scatterplot matrix to explain what the colours of plotted points stand for. However, the legend command does not seem to work (i.e. I see no legend in the plot), and trying to change margins of the scatterplot matrix seems to not be working either (no change seems to happen in the plot). My code is as below:
x <- rnorm(20, 1, 0.1)
y <- rnorm(20, 5, 1)
z <- rnorm(20, 10, 2)
w <- c(1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2)
dat <- data.frame(x, y, z, w)
pallet = c( "red", "blue")
plot(dat[c('x','y','z')], col=pallet[dat$w])
legend("topright", legend=c("one", "two"), col=pallet)
Is there a way to add a legend to a scatterplot matrix that I am missing here?