Skip to content

Instantly share code, notes, and snippets.

@kohske
Created October 7, 2014 04:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kohske/cddbd2ac75d96a59cf11 to your computer and use it in GitHub Desktop.
Save kohske/cddbd2ac75d96a59cf11 to your computer and use it in GitHub Desktop.
pie3d -- interactive 3d pie chart by R and rgl
library(rgl)
pie3d <- function(x, depth = 0.2, webgl = TRUE) {
x = cumsum(x)/sum(x)
ps = embed(c(0, 2*pi*x), 2)
for (i in 1:nrow(ps)) {
p = seq(ps[i, 2], ps[i, 1], by = 2*pi/360)
rgl.triangles(rep(c(t(cbind(embed(sin(p), 2), 0))), 2),
rep(c(t(cbind(embed(cos(p), 2), 0))), 2),
rep(c(-depth, depth), each = (length(p)-1)*3),
col = i)
rgl.quads(sin(c(t(cbind(embed(p, 2), embed(p, 2))[, c(1, 2, 4, 3)]))),
cos(c(t(cbind(embed(p, 2), embed(p, 2))[, c(1, 2, 4, 3)]))),
rep(c(-depth, depth), each = 2, length = (length(p)-1)*4),
col = i)
}
if(webgl) {
browseURL(writeWebGL("."))
}
}
pie3d(1:5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment