【发布时间】:2021-09-22 23:05:13
【问题描述】:
我不确定这个循环的子集有什么问题,但我不断收到语法错误:
for (i in 1:length(wc_comp$Section)) {
print(ggplot(data = (subset(wc_comp, Section == [i]))) +
geom_jitter(aes(x = Headline, y = Word.Count, color=cols), size=5, width = 0.05) +
stat_summary(aes(x = Headline, y = Word.Count, group = Article[i]),
fun = median, fun.min = median, fun.max = median,
geom = "crossbar", color = "black", width = 0.7, lwd = 0.2) +
ylim(min(wc_comp$Word.Count), max(wc_comp$Word.Count)) +
xlab("Story") +
ylab ("Word Count") +
ggtitle([i]) +
theme(plot.title=element_text(hjust=0.5, face = "bold", size = 10),
text = element_text(family = "System Font"),axis.text.x = element_text(size=6)) +
scale_x_discrete(labels = function(x) str_wrap(x, width = 10)))
}
【问题讨论】:
-
请包括错误本身,有太多无法猜测。
-
有几件事跳出来,虽然:(1)
[i]不是有效的R代码,使用i来引用i的值; (2)i在这种情况下总是一个整数,尝试将Section与i进行比较是一个逻辑错误。如果你得到一个匹配,我有 99% 的把握这是一个糟糕的匹配。也许你的意思是Section == Section[i]? (3) 我已经完成了理论化,没有样本数据我不想提出假设的修正。