jozxyqk
jozxyqk

Reputation: 17266

R: Manipulating csv scatter data to plot a 3D surface

I'm sure this has been asked before. If you can find a good link I'll happily delete the question but I'm not having any luck with google.

I want to plot some 3D data and searching always leads me back to R (great/simple alternatives would be welcome).

I have a csv file that looks like this...

x    y    value
0    0    4.35
0    1    4.23
0    2    4.24
1    0    4.34
1    1    4.23
1    2    4.22
2    0    4.34
2    1    4.22
2    2    4.22

All points in the grid are there and unique, so I don't need any interpolation.

I can read it like so...

data <- read.table("mydata.csv", header=T, sep=",")

I've found a number of 3d plotting functions: persp, wireframe, scatterplot3d, rgl/persp3d, rgl/surface. However haven't had any luck understanding the format they need.

I can output like so...

svg("chart.svg", width=4, height=4)
#plot function call(args??)
dev.off()

What's the shortest way to rearrange and draw my data as a surface/heightmap and output to SVG (not embedded raster)?

Upvotes: 2

Views: 1380

Answers (1)

Roman Luštrik
Roman Luštrik

Reputation: 70643

How's this?

my.data <- read.table(text= "x    y    value
0    0    4.35
0    1    4.23
0    2    4.24
1    0    4.34
1    1    4.23
1    2    4.22
2    0    4.34
2    1    4.22
2    2    4.22", header = TRUE)


library(lattice)
wireframe(value ~ x + y, data = my.data, screen = list(z = 70, x = -50))

enter image description here

Upvotes: 3

Related Questions