How to wrap long headers in trellis in R?

I need to add a long title to the graph created using a function likertfrom a package HHthat uses a grid, but it (the grid) does not have this facility. Is there any way to do this?

My code is :

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

My details :

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

Title Elements :

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

UPDATE

Graphic headers are not added directly, but dynamically, from a data frame and data frame are loaded from a CSV file. If, as suggested in the comments, I add \nto the long header in the CSV file, this does not work.

+4
source share
1 answer

, @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()
+4

Source: https://habr.com/ru/post/1568996/


All Articles