3

I’m attempting to create a 3D surface plot in R – I’ve read a number of other questions, but can’t find the solution for this :(

I have X and Y coordinates within space (each X,Y pair refers to one point) for example:

x   y
10  15
11  11
8   11
15  14
15  8
13  11
50  29
29  30
90  40
55  39

I would to plot these points as a surface plot, so that where there are more points in close vicinity, the density of the plot is higher (e.g. its obvious there are many points close to (10,10) and only one point at (90,40).

The ideal solution looks like this:

enter image description here

I’ve attempted to use this - https://cran.r-project.org/web/packages/HSAUR/vignettes/Ch_density_estimation.pdf (this is where I found the image above). But unfortunately I can’t get the examples to work – the library with data is unavailable!

The idea is to have this visualisation to be compared to the surface image, so I need x and y coordinates (0,0) to start in one place (as on the image, both logs start in one corner). I also need the axis to be unequal, with x axis going from 0 to 100 and y axis going from 0 to 50, so that original coordinates aren't distorted and the visualisation displays the data proportionately to the surface.

I really hope someone can help me!

EDIT: So far I've tried to follow the instructions in the file above, which meant using Gaussian function to estimate the peaks.

test <- read.csv("test1.csv", header = TRUE)
gauss <- function(x) 1/sqrt(2*pi) * exp(-(x^2)/2)
n <- 100
h <- 10
xgrid <- seq(from = 0, to = 1280, by = 16)
bumps <- sapply(test$x, function(a) gauss((xgrid - a)/h)/(n * h))
persp(bumps, theta = 60, axes = TRUE, box = TRUE)
Nat
  • 45
  • 5
  • It's always better to include the code you have tried. See also [these directions on how to give a reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610). – Jaap Jan 31 '16 at 19:45
  • @Jaap, thanks for your advice! Have just updated the post :) – Nat Jan 31 '16 at 19:58
  • Still not reproducible. There are three different HSAUR packages on CRAN and you have not included complete code since you have no library calls and we have no way to see what file: test1.csv contains. – IRTFM Feb 01 '16 at 01:46

1 Answers1

2

See kde2d from the commonly used MASS library. Assuming your data is in data, then the following will produce a perspective plot of the estimated density function. See help files for the associated functions to learn how to tweak.

library(MASS)
with(data,persp(kde2d(x,y)))
A. Webb
  • 26,227
  • 1
  • 63
  • 95
  • It's incredible how I've managed to miss this library! This is exactly what I needed! Thank you very much! Do you by any chance know if I create 2 different graphs and I want to visually compare them, do I just change the scale of Z axis when I plot it with persp(), or do I need to make some changes to kde2d? [I feel a bit confused as there are no numbers on my axis at the moment...] – Nat Jan 31 '16 at 20:38