在 R > lattice() > densityplot() 中,如何在键中包含观察数?使用 auto.key()?



我目前正在使用以下内容绘制ROAS密度,按生成ROAS的年份(campaign.eyear)分组

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

我想指出每年的观测次数。

有没有一种好的(简单的)方法可以做到这一点?

谢谢!

您不提供数据,所以使用内置的数据集。使用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))
)

最新更新