3

How can one plot a sphere in R by providing a center point and a radius?

For example, something like this:

sphere_3d(center=c(1,1,1), r=2))

The plot would appear on a three dimensional coordinate system.

Community
  • 1
  • 1
Jeromy Anglim
  • 33,939
  • 30
  • 115
  • 173

3 Answers3

7

Try spheres3d in the rgl package for an interactive plot:

library(rgl)
spheres3d(x = 1, y = 1, z = 1, radius = 1)

There are many other ways, so what's going to be useful needs more input.

mdsumner
  • 29,099
  • 6
  • 83
  • 91
  • +1 thanks. Any idea of how to add a labelled coordinate system. I tried `axes3d()` which seems like a start, but it does not say what is x, y, and z. It would also be nice to be able to control the limits of the axes. – Jeromy Anglim Aug 23 '12 at 06:03
  • @JeromyAnglim: An old answer (and one to a derivative of rgl in package:car) and you've probably moved on, but it might help someone else: http://stackoverflow.com/questions/8204972/carscatter3d-in-r-labeling-axis-better – IRTFM Apr 05 '14 at 18:34
3

Adapted from @mdsummer's answer, this also adds axes.

library(rgl)
open3d()                                   # create new plot
spheres3d(x = 1, y = 1, z = 1, radius = 1) # produce sphere
axes3d()                                   # add x, y, and z axes
Community
  • 1
  • 1
Jeromy Anglim
  • 33,939
  • 30
  • 115
  • 173
1

Take a look at the rgl package for making 3D plots.

Alex Reynolds
  • 95,983
  • 54
  • 240
  • 345