Frage

Good Day All,

I want to add text floating in my wireframe plot and I am rather confused. I can certainly add the text as a title (e.g. main="Hello World") but I would rather not have my particular text in the title

Here is a sample wireframe:

  library(lattice)
  #set up some simplified data
  x <- seq(-.8, .8, .1)
  y <- seq(-.8, .8, .1)
  myGrid <- data.frame(expand.grid(x,y))
  colnames(myGrid) <- c("x","y")
  myGrid$z <- myGrid$x + myGrid$y

  wireframe(
     myGrid$z ~ myGrid$x * myGrid$y, 
     xlab="X", ylab="Y", zlab="Z",
     scales = list(z.ticks=5, arrows=FALSE, col="black", font=3, tck=1)
  )

If I wanted to add "Hello World" in this plot floating somewhere how would I do that ?

War es hilfreich?

Lösung

Override the panel function and add text with grid.text.

wireframe(
    myGrid$z ~ myGrid$x * myGrid$y, 
    xlab="X", ylab="Y", zlab="Z",
    scales = list(z.ticks=5, arrows=FALSE, col="black", font=3, tck=1),
    panel = function(...)
    {
      panel.wireframe(...)
      grid.text("some text", 0, 0, default.units = "native")
    }
)

Andere Tipps

Alternatively you could add the text after you plotted your wireframe with

grid::grid.text("some text", x=unit(0.7, "npc"), y=unit(0.8, "npc"))

The unit function allows you to specify the location of the text. If you use "npc" as your unit, the total width and height of your graph is 1. So the example above would display your text in the top right corner while x=y=unit(0.5, "npc") would plot it in the center.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top