我使用自定义面板功能(在此处找到)在图表上显示数据标签。
require(HH) # also loads: lattice, grid, latticeExtra
# custom panel function
myPanelFunc <- function(...){
panel.likert(...)
vals <- list(...)
DF <- data.frame(x=vals$x, y=vals$y, groups=vals$groups)
### some convoluted calculations here...
grps <- as.character(DF$groups)
for(i in 1:length(origNames)){
grps <- sub(paste0('^',origNames[i]),i,grps)
}
DF <- DF[order(DF$y,grps),]
DF$correctX <- ave(DF$x,DF$y,FUN=function(x){
x[x < 0] <- rev(cumsum(rev(x[x < 0]))) - x[x < 0]/2
x[x > 0] <- cumsum(x[x > 0]) - x[x > 0]/2
return(x)
})
subs <- sub(' Positive$','',DF$groups)
collapse <- subs[-1] == subs[-length(subs)] & DF$y[-1] == DF$y[-length(DF$y)]
DF$abs <- abs(DF$x)
DF$abs[c(collapse,FALSE)] <- DF$abs[c(collapse,FALSE)] + DF$abs[c(FALSE,collapse)]
DF$correctX[c(collapse,FALSE)] <- 0
DF <- DF[c(TRUE,!collapse),]
DF$perc <- ave(DF$abs,DF$y,FUN=function(x){x/sum(x) * 100})
###
panel.text(x=DF$correctX, y=DF$y, label=paste0(round(DF$perc,1),'%'), cex=0.7)
}
data(ProfChal)
origNames = colnames(ProfChal) # required for myPanelFunc
likert(x=Question ~ . , data=ProfChal[ProfChal$Subtable=="Employment sector",]
,main='Is your job professionally challenging?' # title
,as.percent=TRUE
,panel=myPanelFunc
)
但是,在结果图中,我看到一些标签位于另一个之上。为什么这种情况只发生在一些标签上,而不是所有标签上?