Reputation: 121
i have an issue with my ggplot inside my loop. i created a vector with the colnames which i want to loop as a y variable but clearly something is wrong because ggplot function seems to take it as a string and not as a colname in my dataframe.
here is my df :
> head(nbf2020)
Plant annee rang bloc genotype feuilles9.06 feuilles21.06 feuilles12.07
1 19 2020 A 1 22928 10 16 23
2 20 2020 A 1 25899 12 18 25
3 21 2020 A 1 27819 8 15 25
4 22 2020 A 1 25699 19 24 25
5 23 2020 A 1 24882 11 19 29
6 24 2020 A 1 23948 21 29 39
feuilles3.08 feuilles19.08 repetition total
1 26 NA 1 5
2 29 NA 1 2
3 31 NA 1 3
4 40 NA 1 2
5 35 NA 1 2
6 45 NA 1 5
i want to create subplot for the col feuilles.XX then i created a vector with the colnames :
> months = colnames(nbf2020[,c(6:10)])
> months
[1] "feuilles9.06" "feuilles21.06" "feuilles12.07" "feuilles3.08"
[5] "feuilles19.08"
>
after i used this vector for loop and create ggplot :
for (i in months) {
y_name = paste("number of leafs counted", i, sep="")
print(i)
temp_plot = ggplot(nbf2020, aes(x=bloc, y=i, colour=bloc,fill=bloc))+
geom_boxplot(outlier.alpha = 0, alpha=0.25)+
geom_jitter(width=0.25)+
stat_summary(fun=mean, colour="black", geom="point",
shape=18, size=3) +
theme_classic()+
theme(legend.position="none")+
geom_text(data = myletters_df, aes(label = letter, y = 60 ), colour="black", size=5) + xlab("bloc number") + ylab(y_name)
ggsave(temp_plot, file=paste0(i, ".pdf", sep=""), width = 14, height = 10, units = "cm")
}
it's kind of working except that my y value is just the name of the column.
does anyone know what is wrong? thanks for your help !
Upvotes: 2
Views: 293
Reputation: 70623
Since you're using variable i
, which turns out to be a string, use aes_string
in place of aes
.
Upvotes: 2