Search code examples
rplotlattice

How to add a prettier dynamic key (legend) to lattice xyplot


I would like to add a dynamic ( automatically changing) key to my xyplot. I want to show the different parameters of my simulations like:

enter image description here However, I want the values to be in the same row, next to their labels. Like alpha = 0.1

This is the code that I have used so far:



params <- list()
params <- within(params, {
  
  
  alpha <- 0.1
  beta <- 5
  gamma <- 1
  kappa <- 0.5
  delta <- 0.02
  sigma <- 0.1
})


plot(
  xyplot(N.1+N.2+N.3+N.4+N.5+N.6+N.7 ~ time | sprintf("Simulation %02d",nsim), 
         data=result.rep, type=c('l','g'), as.table=T,
         ylab='Infected', xlab='Time',
         scales=list(y=list(alternating=F)),
         key=list( x = .8, y = .4, 
                  text=list(c( "alpha = ", params$alpha, "beta = ", params$beta, "gamma = ", params$gamma, "kappa = ", params$kappa, "delta = ", params$delta, "sigma = ", params$sigma
                               ))
         )
  )
)

I have also tried to use 2 columns in the key, but was similarly unsuccessful. Should be a relatively easy thing to do, but I could not crack it so far.

Thank you for considering this problem.

edit: Here's a code that you can run fully too. Although it shouldnt matter too much in the problem.

time <- seq(1,10)
N.1 <- c(0,0,0,0,0,0,0,0,0,0)
N.2 <- c(0,0,0,0,0,0,0,0,0,0)
N.3 <- c(0,0,0,0,0,0,0,0,0,1)
N.4 <- c(0,1,5,6,10,1,2,2,6,7)
N.5 <- c(0,0,0,0,0,0,0,0,0,0)
N.6 <- c(0,0,1,0,0,0,0,0,0,0)
N.7 <- c(0,1,1,2,2,4,6,6,6,3)
nsim <- c(1,1,1,1,1,1,1,1,1,2,3,4,5)
result_snip <- data.frame(time,N.1,N.2,N.3,N.4,N.5,N.6, N.7)



plot(
  xyplot(N.1+N.2+N.3+N.4+N.5+N.6+N.7 ~ time | sprintf("Simulation %02d",nsim), 
         data=result_snip, type=c('l','g'), as.table=T,
         ylab='Infected', xlab='Time',
         scales=list(y=list(alternating=F)),
         key=list( x = .8, y = .4, 
                   text=list(c( "alpha = ", params$alpha, "beta = ", params$beta, "gamma = ", params$gamma, "kappa = ", params$kappa, "delta = ", params$delta, "sigma = ", params$sigma
                   ))
         )
  )
)

Solution

  • Use paste("alpha =", params$alpha) by G. Grothendieck