gpt4 book ai didi

r - 如何在R中的点阵图形中包装长标题?

转载 作者:行者123 更新时间:2023-12-01 00:56:18 25 4
gpt4 key购买 nike

我需要为使用 likert 创建的图形添加一个长标题来自 HH 的函数包,它使用格子,但它(格子)没有这个设施。有没有办法做到这一点?

我的代码 :

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 文件加载的。如果按照评论中的建议,我添加了 \n对于 .csv 文件中的长标题,这不起作用。

最佳答案

感谢@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()

关于r - 如何在R中的点阵图形中包装长标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27650924/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com