Area under curve
January 12, 2007 at 7:16 am | In GNU-R | Leave a CommentThere are various ways in which you can get R to calculate area under a curve. Some of these are explained in various posts on this thread. Ravi Varadhan wrote up the following function that uses trapezoidal rule for calculating area between a 45 degrees line and a curve. I used it to get gini coefficients from a lorenz curve.
trap.rule-function(x,f) {sum(diff(x)*(f[-1]+f[-length(f)]))/2}
trap.rule(x,f=y-x)
(Area is negative if y(x) is below the 45 degrees line)
Technorati: R lorenz curve gini
Postscript graphics from R
January 12, 2007 at 7:02 am | In GNU-R, Graphics | Leave a CommentGraphics made in R are best saved as postscript files if the files are to be used for printing. This can be done by wrapping your commands (for example, plot) in a postscript device, or by printing the plot to postscript after the plot has been created.
Method 1.
postscript(“filename”,width=x,height=y)
plot commands
dev.off()
Method 2.
Plot commands #these would, by default, display the plot on the screen
dev.copy2eps(file=<filename>,width=x,height=y) #also see ?dev.print
Note that the postscript files created by R do not have a preview attached with them. So that, when you insert them in an MS-word/Openoffice file, you just see a bounding box and not the image itself. Of course, it prints the image all right.
Your friends who are not used to postscript files may be uncomfortable with this. If you want to insert a preview image so that they can see the image on the screen as well, use epstool on the linux command line to add a preview to the postscript file.
Technorati: R postscript eps
Blog at WordPress.com. | Theme: Pool by Borja Fernandez.
Entries and comments feeds.