2

我正在尝试使用 par 函数组合几个图。这些图由 sjPlot 函数 sjp.likert() 生成。

我使用 sjPlot 包本身的两个示例图并尝试将它们组合起来:

likert_2 <- data.frame(as.factor(sample(1:2, 500, replace=TRUE, prob=c(0.3,0.7))),
    as.factor(sample(1:2, 500, replace=TRUE, prob=c(0.6,0.4))),
    as.factor(sample(1:2, 500, replace=TRUE, prob=c(0.25,0.75))),
    as.factor(sample(1:2, 500, replace=TRUE, prob=c(0.9,0.1))),
    as.factor(sample(1:2, 500, replace=TRUE, prob=c(0.35,0.65))))
    levels_2 <- list(c("Disagree", "Agree"))

likert_4 <- data.frame(as.factor(sample(1:4, 500, replace=TRUE, prob=c(0.2,0.3,0.1,0.4))),
    as.factor(sample(1:4, 500, replace=TRUE, prob=c(0.5,0.25,0.15,0.1))),
    as.factor(sample(1:4, 500, replace=TRUE, prob=c(0.25,0.1,0.4,0.25))),
    as.factor(sample(1:4, 500, replace=TRUE, prob=c(0.1,0.4,0.4,0.1))),
    as.factor(sample(1:4, 500, replace=TRUE, prob=c(0.35,0.25,0.15,0.25))))
    levels_4 <- list(c("Strongly disagree", "Disagree", "Agree", "Strongly Agree"))
    items <- list(c("Q1", "Q2", "Q3", "Q4", "Q5"))

par(mfrow=c(2,1))
    sjp.likert(likert_2, legendLabels=levels_2, axisLabels.y=items, orderBy="neg")
    sjp.likert(likert_4, legendLabels=levels_4, axisLabels.y=items)

结果是 R 成功地显示了这些图。有谁知道如何正确组合这些情节?

4

1 回答 1

5

sjp.likert返回 ggplot2-objects,它们不是基本图形。

因此,您必须使用除par.

例如试试这个:

p1 <- sjp.likert(likert_2, legendLabels=levels_2, axisLabels.y=items, orderBy="neg")
p2 <- sjp.likert(likert_4, legendLabels=levels_4, axisLabels.y=items)
require(gridExtra)
require(grid)
require(ggplot2)
grid.arrange(p1$plot, p2$plot, nrow = 2)
于 2014-03-24T13:16:48.667 回答