2013-03-27 52 views
3

我希望能够以百分比为单位显示我的晶格xyplot的y轴(例如0.45 = 45%)。如何获得晶格xyplot的y轴以百分比形式显示分数?

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

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) 

example plot

我想上面,而不是80%,85%,90%,95 y轴标签%

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

回答

6

您可以为xyplot的参数yscale.components提供一个函数(请参见?xyplot?xscale.components.default)。使用sprintf()来添加百分比符号,并且可以在线执行100x乘法。

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) 
     }) 

example plot