2016-05-17 13 views
0

我目前使用以下繪製出投資報酬率密度,在今年分組在其中產生的投資報酬率(campaign.year)在R> lattice()> densityplot()中,我如何在關鍵字中包含觀察次數?用auto.key()?

densityplot(~roas,data=ndb.data.analysis,groups=campaign.year,plot.points=FALSE,auto.key = list(columns=4),main="Distribution of ROAS by Year",from=0,xlab="Return on Ad Spend ($)",ylab="Percent of Observations") 

我想表明在每個觀測的數量一年的關鍵。

有沒有一個很好的(簡單)的方法來做到這一點?

謝謝!

回答

0

您不提供數據,因此請使用內置數據集。通過參數key,您可以自定義您的圖例。

library(lattice) 
data(mtcars) 

# number observations for each cylinder count 
nobs = aggregate(mtcars$mpg,list(mtcars$cyl),length) 
nobs # see results 
# construct labels to include nobs 
labels=paste0(levels(as.factor(mtcars$cyl))," (",nobs$x," obs.)") 
linecolor = trellis.par.get("superpose.symbol")$col[1:length(labels)] 
densityplot(~mpg,mtcars,groups=cyl,lwd=2, 
key=list(space="right",adj=0,title="No. cylinders", 
      lines=list(pch=1,col=linecolor,lwd=2), 
      text=list(labels)) 
) 
相關問題