How can I get the y-axis of a lattice xyplot to display fractions as percentages?
lattice, r
Solution
you can provide a function to the `yscale.components` argument of xyplot (see `?xyplot` and `?xscale.components.default`). use `sprintf()` to add the percentage symbol and you can perform the 100x multiplication in-line.
xyplot(yield~period,data=my.df,
yscale.components=function(...){
yc <- yscale.components.default(...)
yc$left$labels$labels <-
sprintf("%s%%",yc$left$labels$at*100) ## convert to strings as pct
return(yc)
})
Problem
I'd like to be able to display the y-axis of my lattice xyplot in percentage units (e.g. 0.45 = 45%). Here's an example with some fake industrial process yield data: ``` library(lattice) set.seed(1234) my.df <- data.frame(period=c(1:20), n=floor(runif(n=20,min=40,max=80)), d=rpois(n=20,lambda=5)) my.df$yield <- (my.df$n-my.df$d)/my.df$n xyplot(yield~period,data=my.df) ``` I'd like the y-axis labels above to instead be 80%, 85%, 90%, 95% My `yield` variable is a fraction expressed as a decimal in the range (0 <= yield <= 1). I don't want to have to pre-process the data in the data.frame, (e.g. multply by 100), I'd like this to be taken care of by the plotting action.