使用Likert软件包在R中创建发散堆积条形图的问题
使用R中的Likert软件包,我试图创建不同的堆叠条形图,以比较受访者对调查项目的回答,其中受访者根据两个量表对每个项目进行评分:重要性和有效性(1到5,每个量表上有一个“无法判断”选项)。对于每一个项目,我都在“3”类别上对情节进行定心,在情节最右边的4和5个响应的百分比,以及在最左边的3以下的响应百分比。我试图包括一个例子,但我是一个新手和服务条款不允许我这样做 当有两个以上级别时,我的R代码工作正常。但是,我在少于3个级别时遇到问题 下面是一个简单的例子:使用Likert软件包在R中创建发散堆积条形图的问题,r,stacked-chart,R,Stacked Chart,使用R中的Likert软件包,我试图创建不同的堆叠条形图,以比较受访者对调查项目的回答,其中受访者根据两个量表对每个项目进行评分:重要性和有效性(1到5,每个量表上有一个“无法判断”选项)。对于每一个项目,我都在“3”类别上对情节进行定心,在情节最右边的4和5个响应的百分比,以及在最左边的3以下的响应百分比。我试图包括一个例子,但我是一个新手和服务条款不允许我这样做 当有两个以上级别时,我的R代码工作正常。但是,我在少于3个级别时遇到问题 下面是一个简单的例子: Importance <-
Importance <- c(4,5,5,5,4,4)
Effectiveness <- c(5,4,4,4,5,5)
df <- data.frame(Importance,Effectiveness)
df
levels = c("Cannot Judge", "1", "2", "3", "4", "5")
df$Importance <- recode(df$Importance, from=c(0,1,2,3,4,5), to=c("Cannot Judge", "1", "2", "3", "4", "5"))
df$Importance <- as.factor(df$Importance)
df$Importance <- factor(df$Importance, levels=c("Cannot Judge", "1", "2", "3", "4", "5"), ordered=TRUE)
df$Effectiveness <- recode(df$Effectiveness, from=c(0,1,2,3,4,5), to=c("Cannot Judge", "1", "2", "3", "4", "5"))
df$Effectiveness <- as.factor(df$Effectiveness)
df$Effectiveness <- factor(df$Effectiveness, levels=c("Cannot Judge", "1", "2", "3", "4", "5"), ordered=TRUE)
df2 <- likert(df)
plot(df2)
问题似乎是在我将数值数据重新编码为因子后,在data.frame上调用likert()
命令时。如果我不重新编码因子,只对原始数据应用likert()
,则会生成绘图,但它会自动居中于4和5之间(在此数据集中),这不是我需要的
我知道最好是“非常重要”、“重要”、“非常有效”、“有效”等因素。但是,由于这两个量表不同,我不知道在不维持1-5计划的情况下,如何比较这两个量表
为什么我会被解雇
Error in matrix(value, n, p) :
'data' must be of a vector type, was 'NULL'?
我如何调整我的代码使其在两个级别上工作
提前感谢。由于没有“低结果”值,因此出现此错误。
likert.bar.plot
函数使用ggplot
并为正面和负面响应创建一个层,但是,它不会首先检查这些组中是否有任何观察结果。当它添加一个空层时,您会收到上面的错误消息
我发现消除错误和绘制绘图的最简单方法是手动删除坏层。你可以用它来做这个
df2 <- likert(df)
pp <- plot(df2)
pp$layers <- pp$layers[-2]
df2使用原始data.frame,这里有一种算法方法,当绘制的问题集中完全缺少值时,可以删除左侧或右侧堆叠的条形图图层
Importance <- c(4,5,5,5,4,4)
Effectiveness <- c(5,4,4,4,5,5)
df <- data.frame(Importance,Effectiveness)
df
levels = c("Cannot Judge", "1", "2", "3", "4", "5")
df$Importance <- recode(df$Importance, from=c(0,1,2,3,4,5), to=c("Cannot Judge", "1", "2", "3", "4", "5"))
df$Importance <- as.factor(df$Importance)
df$Importance <- factor(df$Importance, levels=c("Cannot Judge", "1", "2", "3", "4", "5"), ordered=TRUE)
df$Effectiveness <- recode(df$Effectiveness, from=c(0,1,2,3,4,5), to=c("Cannot Judge", "1", "2", "3", "4", "5"))
df$Effectiveness <- as.factor(df$Effectiveness)
df$Effectiveness <- factor(df$Effectiveness, levels=c("Cannot Judge", "1", "2", "3", "4", "5"), ordered=TRUE)
df.likert <- likert(df)
# This is the proposed fix
LHS <- 2 # layer number for SD D or equiv
RHS <- 3 # layer number for A SA or equiv
pp <- plot(df.likert)
if (sum(is.na(pp$layers[[LHS]]$data$Item)) > 0) pp$layers <- pp$layers[-LHS]
if (sum(is.na(pp$layers[[RHS]]$data$Item)) > 0) pp$layers <- pp$layers[-RHS]
pp
重要性正是我们所需要的。非常感谢。
Importance <- c(4,5,5,5,4,4)
Effectiveness <- c(5,4,4,4,5,5)
df <- data.frame(Importance,Effectiveness)
df
levels = c("Cannot Judge", "1", "2", "3", "4", "5")
df$Importance <- recode(df$Importance, from=c(0,1,2,3,4,5), to=c("Cannot Judge", "1", "2", "3", "4", "5"))
df$Importance <- as.factor(df$Importance)
df$Importance <- factor(df$Importance, levels=c("Cannot Judge", "1", "2", "3", "4", "5"), ordered=TRUE)
df$Effectiveness <- recode(df$Effectiveness, from=c(0,1,2,3,4,5), to=c("Cannot Judge", "1", "2", "3", "4", "5"))
df$Effectiveness <- as.factor(df$Effectiveness)
df$Effectiveness <- factor(df$Effectiveness, levels=c("Cannot Judge", "1", "2", "3", "4", "5"), ordered=TRUE)
df.likert <- likert(df)
# This is the proposed fix
LHS <- 2 # layer number for SD D or equiv
RHS <- 3 # layer number for A SA or equiv
pp <- plot(df.likert)
if (sum(is.na(pp$layers[[LHS]]$data$Item)) > 0) pp$layers <- pp$layers[-LHS]
if (sum(is.na(pp$layers[[RHS]]$data$Item)) > 0) pp$layers <- pp$layers[-RHS]
pp