gpt4 book ai didi

r - 如何将两个 sjp.likert (来自 sjPlot 包)生成的图合并到一个图中?

转载 作者:行者123 更新时间:2023-12-02 04:33:03 24 4
gpt4 key购买 nike

我正在尝试使用 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成功显示了图。有谁知道如何正确组合这些类型的情节?

最佳答案

sjp.likert 返回 ggplot2 对象,它不是基本图形。

因此,您必须使用 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)

关于r - 如何将两个 sjp.likert (来自 sjPlot 包)生成的图合并到一个图中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22607596/

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