2

使用 R 中的 Likert 包,我正在尝试创建发散堆积条形图来比较调查项目的响应,其中受访者根据两个尺度对每个项目进行评分:重要性和有效性(1 到 5,每个都有“无法判断”选项)。对于每个项目,我将图集中在“3”类别上,图的最右侧是 4 和 5 响应的百分比,最左侧是低于 3 的响应百分比。我试图举一个例子,但我是新手,服务条款不允许我这样做。

当有两个以上的级别时,我的 R 代码可以正常工作。但是,当级别少于 3 个时,我会遇到问题。

这是一个最小的例子:

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)

这会导致以下错误:

Error in matrix(value, n, p) : 
  'data' must be of a vector type, was 'NULL'

问题似乎是当我likert()在 data.frame 上调用命令时,在我将数值数据重新编码为因子之后。如果我不重新编码为因子,而仅应用于likert()原始数据,则会生成绘图,但它会自动居中在 4 和 5 之间(在此数据集中),这不是我需要的。

我承认因素最好是“非常重要”、“重要”、“非常有效”、“有效”等。但是,由于两个尺度不同,我不知道另一种方法来比较两个尺度而不保持 1-5 方案。

为什么我得到

Error in matrix(value, n, p) : 
      'data' must be of a vector type, was 'NULL'?

以及如何调整我的代码以使其适用于两个级别?

提前致谢。

4

2 回答 2

3

您收到此错误是因为您没有“低结果”值。该likert.bar.plot函数使用ggplot并为正面和负面响应创建一个层,但是,它不会首先检查这些组中是否有任何观察结果。当它添加一个空的图层时,您会收到上面的错误消息。

我发现消除错误和原始绘图的最简单方法是手动删除坏层。你可以这样做

df2 <- likert(df)
pp <- plot(df2)
pp$layers <- pp$layers[-2]

(至少在这种情况下它是第 2 层;如果您设置了其他可能不同的选项,那么您可能必须尝试其他值)

李克特条形图

于 2014-06-13T01:48:15.660 回答
0

使用您的原始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
于 2016-03-22T15:07:10.157 回答