[R-sig-eco] 3d density plot

ONKELINX, Thierry Thierry.ONKELINX at inbo.be
Sun Jun 21 16:13:02 CEST 2009


Here is a solution using ggplot2 

Positions <- expand.grid(Run = seq_len(100), ID = seq_len(101))
Positions$X <- runif(nrow(Positions), -.5, .5)
Positions$Y <- runif(nrow(Positions), -.5, .5)
Positions[Positions$ID == 1, c("X", "Y")] <- Positions[Positions$ID == 1, c("X", "Y")] + matrix(c(50, 40), ncol = 2, nrow = 100, byrow = TRUE)
library(plyr)
Positions <- ddply(Positions, "Run", function(x){
	x <- x[order(x$ID), ]
	x$X <- cumsum(x$X)
	x$Y <- cumsum(x$Y)
	x
})
library(ggplot2)
ggplot(Positions, aes(x = X, y = Y)) + geom_density2d()

HTH,

Thierry


----------------------------------------------------------------------------
ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest
Cel biometrie, methodologie en kwaliteitszorg / Section biometrics, methodology and quality assurance
Gaverstraat 4
9500 Geraardsbergen
Belgium
tel. + 32 54/436 185
Thierry.Onkelinx at inbo.be
www.inbo.be

To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of.
~ Sir Ronald Aylmer Fisher

The plural of anecdote is not data.
~ Roger Brinner

The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data.
~ John Tukey

-----Oorspronkelijk bericht-----
Van: r-sig-ecology-bounces at r-project.org [mailto:r-sig-ecology-bounces at r-project.org] Namens Kingsford Jones
Verzonden: zaterdag 20 juni 2009 0:05
Aan: Nabeil Salama
CC: r-sig-ecology at r-project.org
Onderwerp: Re: [R-sig-eco] 3d density plot

Hi Nabeil,

I'm not sure I understand what you'd like to accomplish, but perhaps the following will be helpful:

example(kde2d, package='MASS')


hth,

Kingsford Jones


On Fri, Jun 19, 2009 at 10:32 AM, Nabeil Salama<N.Salama at marlab.ac.uk> wrote:
> Dear all.
>
>
>
> I am new to R so I am probably asking something that will be sneered at.
>
>
>
>
> I am moving 100 "particles" in both x,y dimensions and would like to 
> plot the density of particles at each Cartesian coordinate.
>
>
>
> I can simply use:
>
>
>
> plot(density(positionX)
>
> plot(density(positionY)
>
>
>
> to produce the relevant density of particles at either an X or a Y 
> point, however how would it be possible to overlay the densities so 
> that I obtain a density at a particular coordinate.
>
>
>
> An mock example of what I am currently doing is
>
>
>
> posX    <- matrix (nrow=101,ncol=100,data=0)
>
> posY     <- matrix (nrow=101,ncol=100,data=0)
>
>
>
> posX[1,]<-50
>
> posY[1,]<-40
>
>
>
> for (j in 1:100) {
>
> posX[j+1,]<- posX[j,]+(runif(100)-0.5)
>
> posY[j+1,]<- posY[j,]+(runif(100)-0.5)
>
>
>
> }
>
>
>
> matplot(posX,posY,type="l")
>
> plot(density((posY[,j])))
>
> plot(density((posX[,j])))
>
>
>
> Simply I would like to know the density of particles at coordinates of 
> the matplot.
>
> >From the example given the density of particles at 50,40 will be 
> >higher
> than at the edges. But to quantifiably see this would be an advantage.
>
>
>
>
>
> Any help?
>
>
>
> I hope that is understandable.
>
>
>
> Many thanks
>
>
>        [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-ecology mailing list
> R-sig-ecology at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-ecology
>

_______________________________________________
R-sig-ecology mailing list
R-sig-ecology at r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-ecology

Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer 
en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is
door een geldig ondertekend document. The views expressed in  this message 
and any annex are purely those of the writer and may not be regarded as stating 
an official position of INBO, as long as the message is not confirmed by a duly 
signed document.



More information about the R-sig-ecology mailing list