Reputation: 8447
I am trying to combine several plots using the par function. The plots are generated by the sjPlot function sjp.likert().
I use two example plots from the sjPlot package itself and try to combine them:
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)
The result is that R shows the plots succeedingly. Does anyone know how to combine these kinds of plots properly?
Upvotes: 2
Views: 1181
Reputation: 13300
sjp.likert
returns ggplot2-objects, which are not base-graphics.
Therefore, you have to use other functions than par
.
For example try this:
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)
Upvotes: 5