How to add labels as percentages to the lattice stacked bar charts in R?

人盡茶涼 提交于 2019-12-08 05:03:28

Thanks to Deepayan Sarkar, this question was solved:

library(HH)
library(latticeExtra)
ppi <- 150
jpeg("ssb_%02d.jpg", width=7*ppi, height=4*ppi, res=ppi)

plot_obj <- likert(Grup ~ . | Grup, data = ssb, as.percent = TRUE, positive.order = TRUE,
    main="", xlab=list(label="Percent", cex=1.1),
    ylab="", ylab.right = list("Subjects per group", cex=1.1),
    scales = list(y = list(relation = "free", labels=""), cex=1.1),
    layout = c(1, 2), auto.key=list(space="bottom", columns=3, title="", cex=1.1))

plot_obj <- plot_obj +
    layer({
        id = which(x > 0)
        xx = 0.5 * (cumsum(x[id]) + cumsum(c(0, x[id][-length(id)])))
        panel.text(xx, y[id], labels = paste(x[id], "%", sep = ""))
        id = which(x < 0)
        xx = 0.5 * (cumsum(x[id]) + cumsum(c(0, x[id][-length(id)])))
        panel.text(xx, y[id], labels = paste(-x[id], "%", sep = ""))
    })

print(plot_obj)

dev.off()
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!