Home > Uncategorized > Turning your data into a 3d chart

Turning your data into a 3d chart

Some charts are to help you analyse data. Some charts are to wow people. 3d charts are often the latter, but occasionally the former. In this post, we’ll look at how to turn your data into a 3d chart.

Let’s use the data from this previous post. Use the code which turns the .csv spreadsheet into 3 variables, x, y, and z.

3d charts generally need other packages. We’ll kick off with scatterplot3d, which perhaps makes things too easy:

library(scatterplot3d)
scatterplot3d(x,y,z, highlight.3d = T, angle = 75, scale.y = .5)

The difficulty with 3d plots is that by definition, you’re looking at a 3d plot on a 2d surface. Wouldn’t you like to be able to rotate that plot around a bit? We’ll use the package rgl. Then type:

library(rgl)
plot3d(x,y,z)

This pulls up an interactive window which you can rotate. Very helpful? Perhaps, but there are too many plots. Perhaps you only want to look at the middle 33% of the plots (i.e. look at a subset of the plot)?

startplot <- 33
endplot <- 66
a <- round(startplot/100*length(x))
b <- round(endplot/100*length(x))
plot3d(x[a:b],y[a:b],z[a:b], col = heat.colors(1000))

This looks much better. We’ve said we’d start at 33% of the way through the x,y,z co-ordinates, and end at 66% with the startplot and endplot variables. This is helpful – remember this is one year of data, and we’ve just displayed the middle of the year. The heatmap also helps to distinguish between plots, but in this case it doesn’t add any extra data – more of that in posts to come.

Categories: Uncategorized Tags: , ,
  1. Karl
    July 24, 2010 at 11:44 am

    Very nice. And very useful. How would one save the great image generated in the “RGL Device”?

  2. Karl
    July 24, 2010 at 12:32 pm

    OK, i see rgl.postscript() outputs the image of RGL device in several formats. Although the preferred (for publication) pdf output draws the data points over the frame. Hmmm.

  1. July 30, 2010 at 11:48 am

Leave a comment