Alex Burdusel
Alex Burdusel

Reputation: 3165

Performance issues plotting multiple geoms on ggplot2

I was playing around with a piece of code and wanted to re-do a plot using ggplot2, but encountered a huge performance issue. Maybe I am doing something wrong, but looked for similar issues on the internet and found no related topic.

Using the plot function in the graphics package and adding lines in a loop works pretty fast.

Any suggestions on how can I increase performance using ggplot2 or this is the way it works?

library(bootstrap); data(stamp);
nobs <- dim(stamp)[1]
dens <- density(stamp$Thickness)

# this is using the graphics plot
plot(density(stamp$Thickness),col="black",lwd=3)
for(i in 1:100){
newThick <- rnorm(nobs,mean=stamp$Thickness,sd=dens$bw)
lines(density(newThick),col="grey",lwd=3)
}

# now try something similar using ggplot2
require(ggplot2)
g = ggplot(data.frame(Thickness = stamp[["Thickness"]]), aes(x = Thickness)) + geom_density()
for(i in 1:100){
newThick <- rnorm(nobs,mean=stamp$Thickness,sd=dens$bw)
g = g + geom_density(data = data.frame(new = newThick), aes(x = new), colour = 'gray')
}
g

It is way too slow. Afterwards a tried with 1000 loops and the virtual machine where i am running this ran out of RAM pretty fast. Again, the plot function from graphics had no problem with it.

Upvotes: 1

Views: 861

Answers (1)

Roland
Roland

Reputation: 132706

This should be faster:

DF <- data.frame(Thickness = stamp[["Thickness"]],group=0)
DF <- rbind(DF,data.frame(Thickness=rnorm(nobs*100,mean=stamp$Thickness,sd=dens$bw),group=rep(1:100,each=nobs)))

g <- ggplot(DF, aes(x = Thickness,group=group)) + 
       geom_density(colour="grey") + 
       geom_density(data=DF[DF$group==0,])
print(g)

Upvotes: 2

Related Questions