[R] lattice, add text to xyplot

David Winsemius dwinsemius at comcast.net
Sat Jan 9 02:13:56 CET 2010


On Jan 8, 2010, at 8:10 PM, Jason Morgan wrote:

> On 2010.01.08 19:44:39, Ivan Gregoretti wrote:
>> Hello listers,
>>
>> Does anybody know how to add text to an xyplot without whipping out
>> the existing curve?
>> That's all.
>>
>> For instance,
>>
>> Lets say you generate a graph like this
>>
>> A <- data.frame(x = rnorm(100), y = rnorm(100))
>> xyplot(y ~ x, data = A)
>>
>> How would you add 'Hello world'?
>>
>> I tried 6.02E23 different partial solutions found on the web and
>> failed. I just need one EXAMPLE that WORKS.
>>
>> Unfortunately,
>>
>> library(lattice)
>> ?panel.text
>>
>> shows no examples.
>>
>> As you see, I bring you a formidable challenge.
>
> A <- data.frame(x = rnorm(100), y = rnorm(100))
> xyplot(y ~ x, data = A,
>       panel = function(...) {
>         panel.text(0, 0, "Hello world!")
>         panel.xyplot(...)
>       })

Or, following a Sarkar r-helpful example on how to add to an existing  
plot:

  library(lattice)
  A <- data.frame(x = rnorm(100), y = rnorm(100))
  xyplot(y ~ x, data = A)
  trellis.focus("toplevel") ## has coordinate system [0,1] x [0,1]
  panel.text(0.5, 0.2, "Hello, world", cex = 1.2, font = 2)
  trellis.unfocus()

-- 
David
>
> A whole lot of examples lattice are available here:
>
> http://lmdvr.r-forge.r-project.org/figures/figures.html
>
> If you plan on using lattice often, I highly recommend Deepayan  
> Sarkar's
> book.
>
> Hope that helps,
> ~Jason
>
>>
>> Thank you,
>>
>> Ivan
>>

David Winsemius, MD
Heritage Laboratories
West Hartford, CT



More information about the R-help mailing list