如何将标签作为百分比添加到 R 中的格子堆积条形图?
How to add labels as percentages to the lattice stacked bar charts in R?
我使用 HH
包中的 likert
函数创建了一些堆积条形图,该函数使用 lattice
。现在我想在图表的每个部分添加 表示为百分比 的标签,或者更好的是,只添加到具有足够宽度的部分。这个怎么做?我提到我的数据表示为频率,而不是百分比。
我的数据:
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")
我的代码:
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()
感谢 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()
我使用 HH
包中的 likert
函数创建了一些堆积条形图,该函数使用 lattice
。现在我想在图表的每个部分添加 表示为百分比 的标签,或者更好的是,只添加到具有足够宽度的部分。这个怎么做?我提到我的数据表示为频率,而不是百分比。
我的数据:
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")
我的代码:
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()
感谢 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()