【问题标题】:Problems creating diverging stacked bar plots in R using Likert package使用 Likert 包在 R 中创建发散堆积条形图的问题
【发布时间】:2016-03-22 23:07:10
【问题描述】:

使用 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'

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

我承认因素最好是“非常重要”、“重要”、“非常有效”、“有效”等。但是,由于两个量表不同,我不知道其他方式在不保持 1-5 方案的情况下比较两个量表。

为什么我得到了

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

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

提前致谢。

【问题讨论】:

    标签: r stacked-chart


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 正是需要的。非常感谢。
    【解决方案2】:

    使用您的原始 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
    

    【讨论】:

      猜你喜欢
      • 2014-06-02
      • 1970-01-01
      • 2017-10-13
      • 1970-01-01
      • 2016-06-29
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      • 1970-01-01
      相关资源
      最近更新 更多