如何将晶格xyplot的y轴显示为百分比?

mac*_*mac 4 r lattice

我希望能够以百分比单位显示格子xyplot的y轴(例如0.45 = 45%).

以下是一些假工业过程产量数据的示例:

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)
Run Code Online (Sandbox Code Playgroud)

示例情节

我希望上面的y轴标签代替80%,85%,90%,95%

我的yield变量是在范围内以小数表示的分数(0 <= yield <= 1).我不想在data.frame中预处理数据(例如,乘以100),我希望通过绘图操作来处理这个问题.

mac*_*mac 7

你可以为yscale.componentsxyplot 的参数提供一个函数(参见?xyplot?xscale.components.default).用于sprintf()添加百分比符号,您可以在线执行100倍乘法.

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) 
       })
Run Code Online (Sandbox Code Playgroud)

示例情节