Density plots in R

file=read.table(“file.dat”, header=FALSE)
x=data.matrix(file)
d <- density(x)
pdf(“density.pdf”, 10, 10)
plot(d, xlim=c(-2,2), ylim=c(0,6), xlab=’RDD’)
dev.off()

I tried to use sm.density.compare() to plot multiple density plots together, but the densities shown did not correspond to those that I obtained when plotting separately.

A work around to this problem is to generate separate density plots using the code above and control xlim and ylim, making them equal in all plots.

xlim: receives a vector with min and max X axis values

ylim: receives a vector with min and max Y axis values

Then it’s possible to overlay them by opening the PDF files with Inkscape and using “paste in place”. It’s dirty, but does the trick when R doesn’t want to behave…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s