0

I have a multi-panel plot created with facet_wrap in ggplot, and model outputs from the FlexParamCurve package.

FlexParamCurve provides a model to fit each set of data, i.e. each panel in the plot. I have found code elsewhere for plotting the same curve across all panels, and for plotting model curves for individual lines in each panel. But how can I plot the model curve for each specific plot?

Sample data;

DATA <- data.frame(Cond = rep(1:2, each = 60),
                   Site = rep((seq(1:2)), each = 30),
                   Survey = rep((seq(1:5)), each = 6),
                   Time = rep(1:6))

NUMBERS <- data.frame(Numbers = c(10,13,10,16,31,25,4,11,11,16,21,23,7,12,15,18,20,19,9,15,22,21,24,30,5,10,15,21,21,24,5,7,10,12,20,17,7,11,17,25,27,34,4,9,13,18,23,28,11,15,17,20,21,25,9,18,21,24,21,30,10,11,8,15,20,17,2,6,5,13,14,15,7,9,13,23,28,25,8,13,17,20,24,24,10,10,15,19,23,26,1,2,5,10,17,18,1,3,5,8,15,19,1,8,14,18,26,27,4,9,17,23,25,23,6,12,15,17,20,23))

GRAPH.DATA <- data.frame(cbind(DATA, NUMBERS))

ggplot code;

ggplot(GRAPH.DATA, aes(Time, Numbers, colour=factor(Survey))) +
       geom_line(aes(group = Survey), size = 1.5) +
       facet_grid(Cond ~ Site)

FlexParamCurve code and Model outputs;

MODELS <- pn.mod.compare(GRAPH.DATA$Time, GRAPH.DATA$Numbers, GRAPH.DATA$Site, pn.options ="MOD.OUTPUTS")

Call:
Model: y ~ SSposnegRichards(x, Asym = Asym, K = K, Infl = Infl, M = M,
RAsym = RAsym, modno = 11, pn.options = "MOD.OUTPUTS") | grp 
Data: userdata 

Coefficients:
      Asym         K     Infl          M       RAsym
1-1  24.32301 1.0579649 35.61585  17.961961  -8.1278653
1-2  25.15115 0.4167590 31.70722   6.473804 -14.2503569
2-1  23.69160 0.9179826 36.32567  15.839351 -11.2709848
2-2  24.75980 0.3559937 35.14258   4.499553 -14.4040608

The FlexParamCurve call is in the format (x, y, grouping variable, output_object)

EcologyTom
  • 2,344
  • 2
  • 27
  • 38
  • Not sure if this will help but you could check the broom package. Intro: http://varianceexplained.org/r/broom-intro/ Github: https://github.com/dgrtwo/broom – Jeroen Boeye Sep 05 '16 at 11:36
  • all you really need to do is grab the rownames from your coefficients table and store them as the same names you are already using for your facetting variables. I'm not familiar with `FlexParamCurve` but if you show that code, I could show you how to grab and add the rownames. `dplyr::rownames_to_columns` is a good place to start – Nate Sep 05 '16 at 12:08
  • @JeroenBoeye, thanks for the tip about the broom package. I think that could be very useful, but it will take me a little while to work through it all and see if it suits this situation. – EcologyTom Sep 05 '16 at 12:25
  • @NathanDay I've edited the question to include the FlexParamCurve code. I think I can work out how to add the coefficients as columns to the dataframe (maybe with broom as per Jeroen's comment). But if you could give me pointers on converting that into output in ggplot that would be great – EcologyTom Sep 05 '16 at 12:39
  • The curve data or formula is likely going to be stored in a slot in attributes(model). I'll try and help later today, sorry on the road now – Nate Sep 05 '16 at 13:35
  • Sorry I couldn't figure out a way to extract easily. I've used `drc` before for 4-p linear models maybe that package is worth a look if plotting is essential. – Nate Sep 06 '16 at 12:24
  • Thanks anyway @NathanDay. After a long while I came up with a workaround that let me plot the lines. I will write it up as an answer at some point if I can find the time. – EcologyTom Sep 06 '16 at 12:56

0 Answers0