2

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.

enter image description here

does anyone know what is wrong? thanks for your help !

3
  • Maybe I'm missing something, but you are iterating across months, which is a vector of strings, and then you are using that as the y axis value. So it will use the string as the y axis rather than any numeric value. Commented Sep 19, 2021 at 14:10
  • yeah i agree but i don't know how to fix this problem ^^' Commented Sep 19, 2021 at 14:31
  • 1
    I suspect this is a lazy evaluation problem. It usually is when ggplot is used in a for loop. Switch to a member of the apply family (which all force evaluation( and you should be OK. You can get more information on lazy evaluation here. Commented Sep 19, 2021 at 16:30

1 Answer 1

2

Since you're using variable i, which turns out to be a string, use aes_string in place of aes.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for your comment, i already tried but i get this error : Error in aes_string(x = bloc, y = i, colour = bloc, fill = bloc) : object 'bloc' not found
Note that all your arguments passed to aes_string need to be characters (that refer to column names in the data.frame).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.