Monday, July 25, 2011

Creating svg graphics for web publishing



Thanks to the nice post from Revolution Analytics I was finally able to get an svg device working on my Windows OS version of R. It took some additional tips from a fellow user of blogger to figure out out how to embed the result on bloggers.com (due to the inability to upload svg files I had to post the file on Wikimedia Commons and then create a link).

Luckily, I didn't need to rebuild my R version with cairo support - i just installed the R package Cairo and then used its function CairoSVG()



the code for the figure...


require(Cairo)
set.seed=1111
orbits=10
n.per.orbit=100
angle=rep(seq(0,2*pi, by=2*pi/(n.per.orbit-1)),orbits)
wander<-runif(orbits*n.per.orbit, -1,1)
pos<-scale(cumsum(wander))+5
 
fit <- smooth.spline(pos)
spl.pred <- predict(fit)$y
x<-sin(angle)*spl.pred
y<-cos(angle)*spl.pred
 
COL <- rainbow(length(y))
 
CairoSVG(file = "circle_art.svg", width = 6, height = 6, bg = "black")
par(mar=c(0,0,0,0), bg="black")
plot(x, y, t="n")
segments(x[1:(length(x)-1)], y[1:(length(y)-1)], 
x1=x[2:length(x)], y1=y[2:length(y)], col=COL, lwd=3, lend=0)
dev.off()
Created by Pretty R at inside-R.org

No comments:

Post a Comment