[R] Graph many points without hiding some

Nick Sabbe nick.sabbe at ugent.be
Thu Mar 31 10:06:39 CEST 2011


Hi.
You could also turn it into a 3D plot with some variation on the function
below:
plot4d<-function(x,y,z, u, main="", xlab="", ylab="", zlab="", ulab="")
{
	require(rgl)#may need to install this package first

	#standard trick to get some intensity colors
	uLim<-range(u)
	uLen<-uLim[2] - uLim[1] + 1
	colorlut<-terrain.colors(uLen)
	col<-colorlut[u - uLim[1] + 1]

	open3d()#Open new device
	points3d(x=x, y=y, z=z,  col=col)
	aspect3d(x=1, y=1, z=1) #ensure bounding box is in cube-form
(scaling variables)
	#note: if you want to flip an axis, use -1 in the statement above

	axes3d() #Show axes
	title3d(main = main, sub=paste("Green is low", ulab, ", red is
high")
		xlab = xlab, ylab = ylab, zlab = zlab)
}

HTH,


Nick Sabbe
--
ping: nick.sabbe at ugent.be
link: http://biomath.ugent.be
wink: A1.056, Coupure Links 653, 9000 Gent
ring: 09/264.59.36

-- Do Not Disapprove




-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Peter Langfelder
Sent: donderdag 31 maart 2011 9:26
To: Samuel Dennis
Cc: R-help at r-project.org
Subject: Re: [R] Graph many points without hiding some

On Wed, Mar 30, 2011 at 10:04 PM, Samuel Dennis <sjdennis3 at gmail.com> wrote:
> I have a very large dataset with three variables that I need to graph
using
> a scatterplot. However I find that the first variable gets masked by the
> other two, so the graph looks entirely different depending on the order of
> variables. Does anyone have any suggestions how to manage this?
>
> This code is an illustration of what I am dealing with:
>
> x <- 10000
> plot(rnorm(x,mean=20),rnorm(x),col=1,xlim=c(16,24))
> points(rnorm(x,mean=21),rnorm(x),col=2)
> points(rnorm(x,mean=19),rnorm(x),col=3)
>
> gives an entirely different looking graph to:
>
> x <- 10000
> plot(rnorm(x,mean=19),rnorm(x),col=3,xlim=c(16,24))
> points(rnorm(x,mean=20),rnorm(x),col=1)
> points(rnorm(x,mean=21),rnorm(x),col=2)
>
> despite being identical in all respects except for the order in which the
> variables are plotted.
>
> I have tried using pch=".", however the colours are very difficult to
> discern. I have experimented with a number of other symbols with no real
> solution.
>
> The only way that appears to work is to iterate the plot with a for loop,
> and progressively add a few numbers from each variable, as below. However
> although I can do this simply with random numbers as I have done here,
this
> is an extremely cumbersome method to use with real datasets.
>
> plot(1,1,xlim=c(16,24),ylim=c(-4,4),col="white")
> x <- 100
> for (i in 1:100) {
> points(rnorm(x,mean=19),rnorm(x),col=3)
> points(rnorm(x,mean=20),rnorm(x),col=1)
> points(rnorm(x,mean=21),rnorm(x),col=2)
> }
>
> Is there some function in R that could solve this through automatically
> iterating my data as above, using transparent symbols, or something else?
Is
> there some other way of solving this issue that I haven't thought of?

Assume you are plotting variables y1, y2, y3 of the same length
against a common x, and you would like to assign colors say c(1,2,3).
You can automate the randomization of order as follows:

n = length(y1);
y = c(y1, y2, y3);
xx = rep(x, 3);
colors = rep(c(1,2,3), c(n, n, n));

order = sample(c(1:(3*n)));

plot(xx[order], y[order], col= colors[order])

I basically turn the y's into a single vector y with the corresponding
values of x stored in xx and the plotting colors, then randomize the
order using the sample function.

HTH,

Peter

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list