Reputation: 7
I'm not sure what's wrong with the subset of this loop, but I keep getting a Syntax error:
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)))
}
Upvotes: 0
Views: 85
Reputation: 91
You use the syntax [i]
in two places to refer to the looping variable i
, which is causing the syntax error. Removing the [ ]
on lines 2 and 10 resolves the issue:
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)))
}
Upvotes: 1