[R] Add Anova statistics in each figure

David Winsemius dwinsemius at comcast.net
Mon Jul 31 00:57:25 CEST 2017


> On Jul 30, 2017, at 11:28 AM, Kristi Glover <kristi.glover at hotmail.com> wrote:
> 
> Hi R Users,
> 
> I created interaction plots in ggplot2 and  was trying to add output of two way ANOVA  models, especially only interaction  ( example treatment*control F(XX, XX) = xxx, p = xxx) into figures, but i was not able to add. Would you mind to help on how I can add information into each figure? I have attached the example data and the code that I used for this.
> 
> 
> dat<-structure(list(Sites = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L,
> 
> 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
> 
> 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
> 
> 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
> 
> 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2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
>    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L,
> 
>    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
> 
>    2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 
>    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 
>    2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 
>    1L, 1L), .Label = c("Control", "Treatment"), class = "factor"),
> 
>    Env1 = c(-23.99, 0, -23.4, -24.77, 4.92, 4.94, 4.96, 4.93,
> 
>    4.97, 4.99, 5.06, -12.12, 5.2, 0, 0, 5.07, 5.09, 0, 5.07,
> 
>    0, 5, 0, -27.63, 4.44, 0, 0, 0, 4.33, 4.33, 0, 5.07, 5.07,
> 
>    5.03, 5.01, 0, 0, 4.97, 0, 4.98, 5.07, 0, 5.08, 0, 0, 4.91,
> 
>    -35.98, 0, 0, 0, 5.2, 0, 4.89, 5.07, 0, 4.96, 4.86, 4.96,
> 
>    0, 4.95, 5.07, 0, 0, -34.07, 5.06, 4.98, 4.94, 0, 4.95, 5.07,
> 
>    0, 0, 0, -38.32, -31.91, -29.75, 0, 0, 4.69, 4.48, 0, 0,
> 
>    4.57, 4.78, 0, 0, 4.74, 5.02, 4.87, 0, 0, 0, 4.65, -52.2,
> 
>    0, -32.11, 5.05, 0, 5.03, 5.13, 0, 0, 0, -33.75, 4.84, 4.74,
> 
>    0, 4.82, -32.98, 5.04, 0, 0, 4.95, 5.18, 5.18, 0, 0, 4.57,
> 
>    4.84, 4.74, 0, 4.78, 0, 4.78, 4.36, 4.45, 3.68, 0, 4.16,
> 
>    4.61, 0, 4.63, 0, 0, -28.65, 4.96, 4.42, 0, 0, 0, 0, 0, 4.89,
> 
>    4.96, 4.93, 0, 0, 4.96, 0, 0, 0, 0, 4.42, 0, 0, 0, 4.82,
> 
>    0, 0, 0, -34.71, 0, 4.59, 0, 4.86, 0, 0, 4.75, 0, 0, 4.65,
> 
>    4.8, 0, 0, 4.87, 4.86, 0, 0, 0, 4.83, 0, 4.85, 0, 4.48, 4.38,
> 
>    0, 0, 4.53, 0, 4.79, 0, 0, 0, 4.7, 0, 4.84, 0, 0, 0, 4.35,
> 
>    4.72, 4.72, 4.65, 4.65, 4.39, 4.73, 4.62, 0, 4.76, 0, 5,
> 
>    0, 4.86, 0, -13.38, 0, 4.73, 0, 0, 0, 4.58, 4.76, 0, 0, 0,
> 
>    0, 4.68, 4.74, 0, 0, 0, 4.83, 0, 0, 0, 0, 0, 0), Env2 = c(-5.89,
> 
>    0, -4.13, -8.88, 2.79, -0.31, -1.21, -3.72, -0.52, -6.52,
> 
>    -10.28, -0.27, -21.27, 0, 0, -5.62, -11.49, 0, -4.85, 0,
> 
>    -0.8, 0, -5.4, 0.63, 0, 0, 0, -10.92, -9.41, 0, -9.62, -9.15,
> 
>    -4.32, -5.63, 0, 0, -6.07, 0, -3.34, -11.85, 0, -10.36, 0,
> 
>    0, -5.29, 3, 0, 0, 0, -16.94, 0, 1.47, -8.51, 0, -3.07, -3.51,
> 
>    -4.79, 0, -2.11, -20.19, 0, 0, -9.91, -16.85, -1.4, -3.96,
> 
>    0, -3.83, -7.14, 0, 0, 0, -1.58, -2.42, -1.56, 0, 0, 4.45,
> 
>    23.76, 0, 0, 15.1, 5.86, 0, 0, 8.26, -9.56, 6.43, 0, 0, 0,
> 
>    -1.93, 7.2, 0, 0.39, -6.75, 0, -4.4, -9.39, 0, 0, 0, 2.41,
> 
>    3.23, 2.71, 0, -1, -3.54, -7.19, 0, 0, -5.68, -13.39, -15.14,
> 
>    0, 0, 0.8, 7.74, 5.58, 0, -7.4, 0, 2.63, 10.5, 8.66, 4.49,
> 
>    0, 4.87, 16.63, 0, 9.14, 0, 0, 24.56, -8.22, 5.91, 0, 0,
> 
>    0, 0, 0, -8.03, -7.54, -1.83, 0, 0, 0.67, 0, 0, 0, 0, 7.6,
> 
>    0, 0, 0, -4.54, 0, 0, 0, -5.3, 0, 3.09, 0, 1.35, 0, 0, 12.39,
> 
>    0, 0, 3.66, 5.77, 0, 0, 6, 3.98, 0, 0, 0, -2, 0, 0.99, 0,
> 
>    10.58, 7.31, 0, 0, 5.32, 0, 9.73, 0, 0, 0, 7.23, 0, 8.53,
> 
>    0, 0, 0, 22.69, 0.22, 8.5, 15.74, 13.91, 17.44, 14.93, 13.69,
> 
>    0, -6.28, 0, -5.07, 0, -2.03, 0, 3.01, 0, 11.02, 0, 0, 0,
> 
>    16.53, 7.34, 0, 0, 0, 0, -8.31, 3.31, 0, 0, 0, -2.33, 0,
> 
>    0, 0, 0, 0, 0)), .Names = c("Sites", "Period", "Group", "Env1",
> 
> "Env2"), class = "data.frame", row.names = c(NA, -237L))
> 
> 
> long<-melt(dat, id=c('Sites', 'Period', 'Group'))
> 
> head(long)
> 
> df<-ddply(long, c("Period", "Group","variable"), summarise,
> 
> N    = length(value),
> 
> mean = mean(value, na.rm=TRUE),
> 
> sd   = sd(value, na.rm=TRUE),
> 
> se   = sd / sqrt(N))
> 
> df$Period1 <- factor(df$Period, levels=c("Before", "After"))
> 
> gp <- ggplot(df, aes(x=Period1, y=mean, colour=Group, group=Group))
> 
> gp<-gp + geom_line(aes(linetype=Group), size=.6) +
> 
>     geom_point(aes(shape=Group), size=3) +
> 
>     geom_errorbar(aes(ymax=mean+se, ymin=mean-se), width=.1)
> 
> gp+facet_grid(.~variable)
> 
> 
> ###two way anova interactions analysis
> 
> # since I do have
> 
> long$newFactor<-paste(long$Sites,long$variable)
> 
> models <- dlply(long, "newFactor", function(df)
> 
>  lm(value ~ Group*Period, data = long))
> 
> summary(models)

All of those items in the `models`-object are the same.

What part of these models do you propose to add to the plot?

models[[1]]

Call:
lm(formula = value ~ Group * Period, data = long)

Coefficients:
                (Intercept)               GroupTreatment  
                     0.2130                      -1.2245  
               PeriodBefore  GroupTreatment:PeriodBefore  
                    -0.4174                       2.4311  



> 
> But I could not  add the information of the interaction (F and its P values) in each above figures. Would you mind to hep me. It will be highly appreciated.

summary(models[[1]])

Call:
lm(formula = value ~ Group * Period, data = long)

Residuals:
    Min      1Q  Median      3Q     Max 
-51.996  -1.002   0.204   4.429  24.347 

Coefficients:
                            Estimate Std. Error t value Pr(>|t|)
(Intercept)                   0.2130     0.6581   0.324    0.746
GroupTreatment               -1.2245     1.1627  -1.053    0.293
PeriodBefore                 -0.4174     0.8654  -0.482    0.630
GroupTreatment:PeriodBefore   2.4311     1.5701   1.548    0.122

Residual standard error: 7.787 on 470 degrees of freedom
Multiple R-squared:  0.005522,	Adjusted R-squared:  -0.0008256 
F-statistic: 0.8699 on 3 and 470 DF,  p-value: 0.4565

> Thanks
> 
> 
> 	[[alternative HTML version deleted]]

It didn't make a difference in this posting but you should learn to post in plain text. HTML formatted posting are often mangled.
> 
--

David Winsemius
Alameda, CA, USA

'Any technology distinguishable from magic is insufficiently advanced.'   -Gehm's Corollary to Clarke's Third Law



More information about the R-help mailing list