我正在尝试使用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成功地显示了这些图。有谁知道如何正确地组合这些类型的情节?
答案 0 :(得分:4)
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)