我需要为使用HH
包中的likert
函数创建的图形添加一个长标题,该图形使用格子,但它(格子)没有此功能。有没有办法做到这一点?
我的代码:
library(HH)
ppi <- 150
jpeg("ssb_%02d.jpg", width=7*ppi, height=4*ppi, res=ppi)
for(i in 1:2){
plot_obj <- likert(Grup ~ . | Grup, data = ssb, as.percent = TRUE, positive.order = TRUE,
main=list(label = items[i,], cex=1.2), 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()
我的数据:
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")
标题项:
items <- structure(list(V1 = structure(1:2, .Label = c("1. În cele mai multe privinţe, viaţa mea corespunde idealului meu.",
"2. Până în prezent am primit cele mai importante lucruri pe care le doresc în viață."
), class = "factor")), .Names = "V1", class = "data.frame", row.names = c(NA,
-2L))
更新
图形标题不是直接从数据框动态添加的,而是从.csv加载的,数据框是从文件加载的。如果按照评论中的建议,我在.csv文件中的长标题中添加n
,这是行不通的。
多亏了@josh-obrien,我解决了我的问题。现在,当图形标题超过 70 个字符时,它将换行为 65 个字符宽的版本。
library(HH)
ppi <- 150
jpeg("ssb_%02d.jpg", width=7*ppi, height=4*ppi, res=ppi)
for(i in 1:2){
if(stri_length(items[i,])>70){
graphic.title <- paste(strwrap(items[i,], width = 65), collapse="n")
} else {
graphic.title <- items[i,]
}
plot_obj <- likert(Grup ~ . | Grup, data = ssb, as.percent = TRUE, positive.order = TRUE,
main=list(label = graphic.title, cex=1.2), 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()