Created
November 22, 2012 14:20
-
-
Save informationsea/4131400 to your computer and use it in GitHub Desktop.
Density plot with grid and summary
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(lattice) | |
library(grid) | |
mydensityplot <- function(data, xlab=deparse(substitute(data)), grid=F, summary=F, ...) { | |
densityplot(data, xlab=xlab, ..., | |
panel=function(...){ | |
if (grid) { | |
panel.grid(h=-1, v=-1) | |
} | |
if (summary) { | |
grid.text(sprintf("Mean: %.2e", mean(data)), x=0.95, y=0.8, just="right") | |
grid.text(sprintf("Median: %.2e", median(data)), x=0.95, y=0.75, just="right") | |
grid.text(sprintf("SD: %.2e", sd(data)), x=0.95, y=0.7, just="right") | |
q <- quantile(data, probs=1:9/10) | |
for (i in 1:9) { | |
print(i) | |
grid.text(sprintf("%d%%: %.2e", i*10, q[i]), x=0.95, y=0.7-(i*0.05), just="right") | |
} | |
} | |
panel.densityplot(...) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment