如何将标签作为百分比添加到R中的格子堆叠条形图? [英] How to add labels as percentages to the lattice stacked bar charts in R?

查看:315
本文介绍了如何将标签作为百分比添加到R中的格子堆叠条形图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 HH 包中的 likert 函数创建了一些堆叠的条形图,它使用晶格。现在我想将以百分比表示的标签添加到图表的每个部分,或者更好的是只添加到具有足够宽度的部分。这个怎么做?我提到我的数据以频率表示,而不是百分比。我的数据

  ssb < - 结构(列表(强烈不同意)= c(2L,1L),`中等不同意'= 1:2,
`稍微不同意` (3L,1L),轻微同意= c(1L,
5L),同意同意= 4:5,非常同意= c(9L,6L),
Grup = c(强烈反对,强烈反对,
中等反对,轻微不同意,轻微同意,中等赞同,
非常同意,Grup),row.names = c(1,2),class =data.frame)

我的密码

 图书馆(HH)
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(Su每个组的bjects,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))

print(plot_obj)

dev。关闭()


解决方案

感谢Deepayan Sarkar,解答:

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


I have created some stacked bar charts with the likert function from the HH package, that uses lattice. Now I want to add labels represented as percentages inside to each segment of the charts, or, better, only to that segments that have a sufficient width for that. How to do this? I mention that my data are represented as frequencies, not as percentages.

My data:

ssb <- structure(list(`Strongly Disagree` = c(2L, 1L), `Moderate Disagree` = 1:2, 
    `Slightly Disagree` = c(3L, 1L), `Slightly Agree` = c(1L, 
    5L), `Moderate Agree` = 4:5, `Strongly Agree` = c(9L, 6L), 
    Grup = c("Experimental grup", "Control grup")), .Names = c("Strongly Disagree", 
"Moderate Disagree", "Slightly Disagree", "Slightly Agree", "Moderate Agree", 
"Strongly Agree", "Grup"), row.names = c("1", "2"), class = "data.frame")

My code:

library(HH)
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))

print(plot_obj)

dev.off()

解决方案

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

这篇关于如何将标签作为百分比添加到R中的格子堆叠条形图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆