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



我是使用R的新手,我正在尝试制作一个发散的堆叠条形图,如这里和这里所示。

我从工作代码中修改了以下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","Strongly Disagree",  "Disagree", "Neither Agree nor Disagree",   "Agree",    "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,0,0,13.93442623,84.42622951
20,11.76470588,33.33333333,22.54901961,27.45098039,4.901960784"
first_q1 = read.csv(text=CSV_Text, header=TRUE, 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.low=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 Disagree", "Disagree", "Neutral", "Agree", "Strongly Agree")

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

最新更新