gpt4 book ai didi

在格子图上重复类别(R 中的 Likert 函数)

转载 作者:行者123 更新时间:2023-12-02 04:53:40 28 4
gpt4 key购买 nike

我是 R 新手用户,正在尝试使用 HH 包中的 Likert 函数创建绘图。我的问题似乎来自重复的类别标签。更容易显示问题:

    library(HH)

responses <- data.frame( Subtable= c(rep('Var1',5),rep('Var2',4),rep('Var3',3)),
Question=c('very low','low','average','high','very high', '<12', '12-14', '15+',
'missing', '<25','25+','missing'), Res1=as.numeric(c(0.05, 0.19, 0.38, 0.24, .07,
0.09, 0.73, 0.17, 0.02, 0.78, 0.20, 0.02)), Res2=as.numeric(c(0.19, 0.04, 0.39,
0.22, 0.06, 0.09, 0.50, 0.16, 0.02, 0.75, 0.46, 0.20)))

likert(Question ~ . | Subtable, responses,
scales=list(y=list(relation="free")), layout=c(1,3),
positive.order=TRUE,
between=list(y=0),
strip=FALSE, strip.left=strip.custom(bg="gray97"),
par.strip.text=list(cex=.6, lines=3),
main="Description of Sample",rightAxis=FALSE,
ylab=NULL, xlab='Percent')

不幸的是,它创建了实际上并不存在的奇怪空间,如下图的底部面板所示:

enter image description here

这似乎来自重复的“缺失”类别。我的实际数据有几次重复(例如,“否”、“其他”),每当包含它们时,我都会得到这些额外的空格。如果我运行相同的代码但删除重复的类别,那么它可以正常运行。在这种情况下,这意味着将上面代码中的“响应”更改为 responses[!回复$Question %in% 'missing',].

有人可以告诉我如何使用所有类别创建图表,而不需要“额外”空间吗?感谢您的帮助和耐心。

-Z

R 3.0.2
HH 3.0-3
lattice 0.20-24
latticeExtra 0.6-26

最佳答案

这是使用ggplot2创建图形的解决方案

library(ggplot2)

responses <-
data.frame(Subtable = c(rep('Var1',5), rep('Var2',4), rep('Var3',3)),
Question = c('very low','low','average','high','very high',
'<12', '12-14', '15+', 'missing', '<25','25+',
'missing'),
Res1 = as.numeric(c(0.05, 0.19, 0.38, 0.24, .07, 0.09, 0.73,
0.17, 0.02, 0.78, 0.20, 0.02)),
Res2 = as.numeric(c(0.19, 0.04, 0.39, 0.22, 0.06, 0.09, 0.50,
0.16, 0.02, 0.75, 0.46, 0.20)),
stringsAsFactors = FALSE)

responses$Subtable <- factor(responses$Subtable, levels = paste0("Var", 1:3))

responses$Question <-
factor(responses$Question,
levels = c("missing", "25+","<25", "<12", "12-14", "15+",
"very low", "low", "average", "high", "very high"))

ggplot(responses) +
theme_bw() +
aes(x = 0, y = Question) +
geom_errorbarh(aes(xmax = 0, xmin = Res1, color = "red")) +
geom_errorbarh(aes(xmin = 0, xmax = -Res2, color = "blue")) +
facet_wrap( ~ Subtable, ncol = 1, scale = "free_y") +
scale_color_manual(name = "",
values = c("red", "blue"),
labels = c("Res1", "Res2")) +
scale_x_continuous(breaks = c(-0.5, 0, 0.5),
labels = c("0.5", "0", "0.5")) +
ylab("") + xlab("Percent") +
theme(legend.position = "bottom")

enter image description here

关于在格子图上重复类别(R 中的 Likert 函数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21877298/

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