1

I am trying to combine multiple plots but I don't want to write a ton of code, so I wrote a function

MYGRAPH.Leaf<-function(i){
  ggplot(Leaf.hemi, aes(x=type, y=i, group=pot))+
    geom_line(color="grey")+
    geom_point(aes(color=host))+
    #geom_text_repel()+
    facet_wrap(.~hemiparasite)+
    ylab(names(leaf.response.hemi[i]))+
    newtheme
}

graphs<-lapply(leaf.response.hemi, MYGRAPH.Leaf)
ggarrange(graphs$K, graphs$Na, graphs$P, graphs$Ca, graphs$S, graphs$Mg, common.legend=TRUE

Here is some of the code of Leaf.hemi

head(Leaf.hemi)
   X.1  X        K     Na         P        Mg        Ca         S
6    6  6 4.298202 0.0018 0.9763679 0.4000289 1.2009743 0.7394368
8    8  8 3.490799 0.0030 0.9442089 0.4924891 1.3988981 0.9508996
9    9  9 2.215777 0.0037 0.8185966 0.6482371 1.8348983 1.2879451
11  11 11 4.647563 0.0012 0.7674694 0.3947354 0.9526773 0.7056782
13  13 13 3.457349 0.0054 0.6979817 0.5680050 1.3992462 1.2307497
14  14 14 2.603297 0.0008 0.6273514 0.5238602 1.4479887 1.2073244
   hemiparasite pot.code type host pot mesh.size leaf.species
6          CAFO      41B    B ACMI  41        35         CAFO
8          CAFO      42B    B ACMI  42        35         CAFO
9          CAFO      42C    C ACMI  42        35         CAFO
11         CAFO      43B    B ACMI  43        35         CAFO
13         CAFO      44B    B ACMI  44        35         CAFO
14         CAFO      44C    C ACMI  44        35         CAFO

with leaf.response.hemi just being the 6 response variables

        K     Na         P        Mg         S        Ca
6  4.298202 0.0018 0.9763679 0.4000289 0.7394368 1.2009743
8  3.490799 0.0030 0.9442089 0.4924891 0.9508996 1.3988981
9  2.215777 0.0037 0.8185966 0.6482371 1.2879451 1.8348983
11 4.647563 0.0012 0.7674694 0.3947354 0.7056782 0.9526773
13 3.457349 0.0054 0.6979817 0.5680050 1.2307497 1.3992462
14 2.603297 0.0008 0.6273514 0.5238602 1.2073244 1.4479887

Basically, it all works great, except when I create the graphs, despite the actual points being correct ,the y-label is off

Any help would be appreciated, it is so much better to do it this way (or any other way someone might suggest)

enter image description here

Skotani1
  • 149
  • 6
  • 1
    Can you say exactly what you mean when you say "the y label is off"? What is the expected output? Maybe you wanted `ylab(names(leaf.response.hemi)[i])`? Make sure to share your data in a [reproducible format](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) so we can copy/paste it into R for testing. – MrFlick Jul 09 '20 at 21:46
  • What do you mean by "off"? – Edward Jul 09 '20 at 21:48
  • So, as you can see every graph is labeled "K" (except one that is "Mg") It should iterate over the names of the response matrix. i.e. K Na P Mg S Ca It is correctly plotting the values associated with each of the response variables, but not correctly labeling it I hope this helps, thanks – Skotani1 Jul 09 '20 at 22:08
  • Does changing `ylab(names(leaf.response.hemi[i]))` to `ylab(names(leaf.response.hemi)[i])` fix it? – Allan Cameron Jul 09 '20 at 23:16
  • Nope :( Still gives the same output – Skotani1 Jul 12 '20 at 18:55

0 Answers0