错误:在数据中找不到 id 变量:尝试在 R 中绘图时的项目

问题描述

我是 R 的新手,我正在尝试制作一个发散堆积条形图,如 herehere 所示。

我有以下从工作代码修改的 R 代码。我修改后的代码给了我一个错误。我得到的错误Error: id variables not found in data: Item 。我不明白为什么我会收到这个错误

library("devtools")
library("likert")

scale_height = knitr::opts_chunk$get('fig.height')*0.5
scale_width = knitr::opts_chunk$get('fig.width')*1.25
knitr::opts_chunk$set(fig.height = scale_height,fig.width = scale_width)

theme_update(legend.text = element_text(size = rel(0.7)))

# u = understandability
title_u = "Understandability"

headers_u_n_a = c("Type","Strongly disagree","disagree","Neutral","Agree","Strongly Agree")

y_label = "Video Transformation"

understandability_csv_text = "Type  Strongly disagree   disagree    Neutral Agree   Strongly Agree
WT  0.00    0.00    0.00    27.27   72.73
WoT 0.00    18.18   18.18   18.18   45.45
TF  9.09    9.09    36.36   27.27   18.18"

u_data = read.csv(text=understandability_csv_text,header=TRUE,sep="\t")
u_data$Type = as.factor(u_data$Type)
names(u_data) = headers_u_n_a
u_data_summary = likert(summary = u_data)
plot(u_data_summary,plot.percent.neutral=TRUE,plot.percent.low=FALSE,plot.percent.high=FALSE) + ylab(y_label) + ggtitle(title_u)

我正在修改以下 MWE:

library("devtools")
library("likert")

scale_height = knitr::opts_chunk$get('fig.height')*0.5
scale_width = knitr::opts_chunk$get('fig.width')*1.25
knitr::opts_chunk$set(fig.height = scale_height,fig.width = scale_width)

theme_update(legend.text = element_text(size = rel(3)))
theme_update(axis.title = element_text(size = rel(4)))
theme_update(plot.title = element_text(size = rel(4)))
theme_update(axis.text = element_text(size = rel(4)))

title_q1 = "I'm satisfied with the way the results are ranked"

headers_q1 = c("Item","Neither Agree nor disagree","Strongly Agree")

xlab_first = "Position"

CSV_Text = "K,Strongly disagree,disagree,Neither Agree nor disagree,Agree,Strongly Agree
10,2.752293578,15.59633028,18.34862385,48.62385321,14.67889908
5,1.739130435,5.217391304,6.086956522,48.69565217,38.26086957
1,1.639344262,13.93442623,84.42622951
20,11.76470588,33.33333333,22.54901961,27.45098039,4.901960784"

first_q1 = read.csv(text=CSV_Text,sep=",")
first_q1$K= as.factor(first_q1$K)
names(first_q1) = headers_q1
s_first_q1 = likert(summary = first_q1)
plot(s_first_q1,plot.percent.neutral=FALSE,plot.percent.high=FALSE) + xlab(xlab_first) + ggtitle(title_q1)

解决方法

我能够修复它并通过更改使其正常工作

headers_u_n_a = c("Type","Strongly Disagree","Disagree","Neutral","Agree","Strongly Agree")

headers_u_n_a = c("Item","Strongly Agree")

但是,我仍然不确定为什么需要这样做。