In this post I show how to plot a 3D object and put it into an HTML-supported platform (as an example, see my previous post
here). Being horrible with IT related matters, it took me quite a while and tediously long hours of website browsing (and groaning!) to figure this out, so I figure I'll put the code here and perhaps save some time for anyone who interests in this.
OK, first, the plot of a 3D globe itself is done simple enough, as the example code is provided in the R package "
rgl":
library(rgl)
###plot the globe
open3d()
options(rgl.useNULL=FALSE)
par3d(windowRect = c(100, 100, 900, 900))
# Show the Earth with a cutout by using clipplanes in subscenes
lat <- matrix(seq(90, -90, len = 50)*pi/180, 50, 50, byrow = TRUE)
long <- matrix(seq(-180, 180, len = 50)*pi/180, 50, 50)
r <- 6378.1 # radius of Earth in km
x <- r*cos(lat)*cos(long)
y <- r*cos(lat)*sin(long)
z <- r*sin(lat)
#
obj <- surface3d(x, y, z, col = "white",
texture = system.file("textures/worldsmall.png", package = "rgl"),
specular = "black", axes = FALSE, box = FALSE, xlab = "", ylab = "", zlab = "",
normal_x = x, normal_y = y, normal_z = z)
cols <- c(rep("chocolate4", 4), rep("burlywood1", 4), "darkgoldenrod1")
rs <- c(6350, 5639, 4928.5, 4207, 3486,
(3486 + 2351)/2, 2351, (2351 + 1216)/2, 1216)
for (i in seq_along(rs))
obj <- c(obj, spheres3d(0, 0, col = cols[i], radius = rs[i]))
###put it in a local host HTML page
browseURL(paste("file://",writeWebGL(dir=file.path(tempdir(), "webGL"), width=500), sep=""))
In case you are new to R, find out more about it
here. It short it is a powerful statistical programming language of which vast capabities extend well beyond the field of statistics and into fields such as designing and engineering, to name but a few. The power of R is humbly illustrated here with this 3D plot. No need to say this, but I've fallen in love with R since my days doing Master thesis, over my favourite Eviews and Stata packages.
Right, after plotting, the hassle is on how to publish it online. The code above allows you to open the plot of a 3D globe in a browser. If you have access to an HTML-supported platform (like a Blogger account), all you need is an HTML representation of the plot. To get this, you can play around with another R package called "
knitr" which has very extensive interaction with "rgl".
However, I found another way which is arguably faster (in a most "hackish" manner): suppose you are using Firefox, go to "Open menu" (the button at the top right corner, as of my current version 45). Then open the "Developer" tab and go to "Page source". This opens the immediate page's source code, in HTML form. This is what you need. I'm sure other browsers allow you to do the same thing, it might require some research and tinkering though.
Finally, simply copy all the codes and put them into the platform you are using. In my case, Blogger posts have an HTML section where I can add these 3D pictures anywhere in the posts.
That's it.