On 10/25/06, Murray Pung <mcpung at gmail.com>
wrote:> Is there any way of adding text to a density plot? I have had a go using
the
> text() function but I think the error is because this function doesn't
work
> with densityplot().
>
> Alternatively, I understand I can achieve pretty much the same result if I
> plot a density kernel estimate using plot() (which allows text()), but I do
> prefer densityplot().
>
>
> Also, is it possible to specify the dimensions of a graphics device? I
don't
> mean the x and y limits of a plot, but rather can I change the dimensions
of
> the default (square) graphics device?
>
> Many thanks
> Murray
>
>
> try <- (rnorm(100, mean = 5, sd = 3))
> library(lattice)
> trellis.device(col = FALSE, theme =
lattice.getOption("col.whitebg"))
> densityplot(~try)
> normtest <- shapiro.test(try)
> normtest
> pvalue <- round(normtest$p.value,5)
> normtext <- paste(normtest$method,"p-value =",pvalue)
> normtext
> xcoord <- max(try)*0.6
> text(xcoord,0.1,normtext)
You have two options: either
trellis.focus("panel", 1, 1)
panel.text(xcoord,0.1,normtext)
trellis.unfocus()
which is analogous to the plot(density()) paradigm, or, what in this
situation is more appropriate IMO (as it will work for multipanel
plots as well):
densityplot(~try,
panel = function(x, ...) {
panel.densityplot(x, ...)
normtest <- shapiro.test(x)
pvalue <- round(normtest$p.value,5)
normtext <- paste(normtest$method,"p-value =",
pvalue)
xcoord <- max(try) * 0.6
panel.text(xcoord, 0.1, normtext)
})
There are ways to make a safer choice of the y coordinate than 0.1; see
?current.panel.limits
and
library(grid)
?grid.text
-Deepayan