This vignette demonstrates plotting functions for visualising group
means, interactions, and ANOVA model diagnostics from the
rwf package. These plots are designed to accompany ANOVA
results — they help communicate what the model found (means plots,
interaction plots) and whether its assumptions hold (diagnostic
plots).
Two datasets are used throughout:
df_blood_pressure: a repeated-measures
dataset recording blood pressure under different treatments, with
grouping variables for patient, sex, and age group.df_crop_yield: a factorial dataset
recording crop yield under different fertiliser and irrigation
conditions.Installation instructions for rwf can be found on the rwf GitHub repository
plot_oneway plots group means with error bars for one or
more dependent variables against one or more independent variables. The
type argument controls what the error bars represent:
"se" — standard error of the mean:
shows the precision of the mean estimate. Narrower bars indicate more
stable estimates."ci" — 95% confidence interval: wider
than SE bars; a rough guide is that non-overlapping CIs suggest a
significant difference, though this is not a formal test."sd" — standard deviation: shows the
spread of raw scores around the mean, not the precision of the mean
itself. Useful for communicating variability in the data rather than
uncertainty in the estimate.The four calls demonstrate the key combinations:
type
(uses the default) — shows how the function handles a fully crossed
design.The df_blood_pressure data is first reshaped from wide
to long format using melt, so that each treatment condition
becomes a level of a single treatment factor rather than
separate columns.
df_blood_pressure_melt<-melt(df_blood_pressure,
variable.name="treatment",
value.name="blood_pressure",
id.vars=c("patient","sex","agegrp"))
plot_oneway(df=df_blood_pressure_melt,dv=5,iv=3:4,type="se")
## $plot_data ## $plot_data$agegrp_blood_pressure ## agegrp N blood_pressure sd se ci ## 1 30-45 80 148.0000 11.21708 1.254107 2.496238 ## 2 46-59 80 152.9375 13.16819 1.472248 2.930436 ## 3 60+ 80 160.7750 11.60202 1.297146 2.581904 ## ## $plot_data$treatment_blood_pressure ## treatment N blood_pressure sd se ci ## 1 bp_before 120 156.4500 11.38985 1.039746 2.058801 ## 2 bp_after 120 151.3583 14.17762 1.294234 2.562712 ## ## ## $plot_data_df ## agegrp treatment blood_pressure N sd se ci ## 1 30-45148.0000 80 11.21708 1.254107 2.496238 ## 2 46-59 152.9375 80 13.16819 1.472248 2.930436 ## 3 60+ 160.7750 80 11.60202 1.297146 2.581904 ## 4 bp_before 156.4500 120 11.38985 1.039746 2.058801 ## 5 bp_after 151.3583 120 14.17762 1.294234 2.562712 ## ## $plots ## $plots$agegrp_blood_pressure
## ## $plots$treatment_blood_pressure
plot_oneway(df=df_blood_pressure,dv=5,iv=2,type="ci")
## $plot_data ## $plot_data$sex_bp_after ## sex N bp_after sd se ci ## 1 Female 60 147.2000 11.74272 1.515979 3.033467 ## 2 Male 60 155.5167 15.24322 1.967891 3.937740 ## ## ## $plot_data_df ## sex bp_after N sd se ci ## 1 Female 147.2000 60 11.74272 1.515979 3.033467 ## 2 Male 155.5167 60 15.24322 1.967891 3.937740 ## ## $plots ## $plots$sex_bp_after
plot_oneway(df=df_blood_pressure,dv=4:5,iv=2,type="sd")
## $plot_data ## $plot_data$sex_bp_before ## sex N bp_before sd se ci ## 1 Female 60 153.6333 10.73560 1.385960 2.773300 ## 2 Male 60 159.2667 11.41344 1.473469 2.948405 ## ## $plot_data$sex_bp_after ## sex N bp_after sd se ci ## 1 Female 60 147.2000 11.74272 1.515979 3.033467 ## 2 Male 60 155.5167 15.24322 1.967891 3.937740 ## ## ## $plot_data_df ## sex bp_before bp_after N sd se ci ## 1 Female 153.6333 NA 60 10.73560 1.385960 2.773300 ## 2 Male 159.2667 NA 60 11.41344 1.473469 2.948405 ## 3 Female NA 147.2000 60 11.74272 1.515979 3.033467 ## 4 Male NA 155.5167 60 15.24322 1.967891 3.937740 ## ## $plots ## $plots$sex_bp_before
## ## $plots$sex_bp_after
plot_oneway(df=df_blood_pressure,dv=4:5,iv=2:3)
## $plot_data ## $plot_data$sex_bp_before ## sex N bp_before sd se ci ## 1 Female 60 153.6333 10.73560 1.385960 2.773300 ## 2 Male 60 159.2667 11.41344 1.473469 2.948405 ## ## $plot_data$agegrp_bp_before ## agegrp N bp_before sd se ci ## 1 30-45 40 151.675 9.258087 1.463832 2.960880 ## 2 46-59 40 155.100 11.459628 1.811926 3.664967 ## 3 60+ 40 162.575 10.727122 1.696107 3.430700 ## ## $plot_data$sex_bp_after ## sex N bp_after sd se ci ## 1 Female 60 147.2000 11.74272 1.515979 3.033467 ## 2 Male 60 155.5167 15.24322 1.967891 3.937740 ## ## $plot_data$agegrp_bp_after ## agegrp N bp_after sd se ci ## 1 30-45 40 144.325 11.89352 1.880530 3.803732 ## 2 46-59 40 150.775 14.50285 2.293102 4.638237 ## 3 60+ 40 158.975 12.28609 1.942602 3.929283 ## ## ## $plot_data_df ## sex agegrp bp_before bp_after N sd se ci ## 1 Female153.6333 NA 60 10.735600 1.385960 2.773300 ## 2 Male 159.2667 NA 60 11.413442 1.473469 2.948405 ## 3 30-45 151.6750 NA 40 9.258087 1.463832 2.960880 ## 4 46-59 155.1000 NA 40 11.459628 1.811926 3.664967 ## 5 60+ 162.5750 NA 40 10.727122 1.696107 3.430700 ## 6 Female NA 147.2000 60 11.742722 1.515979 3.033467 ## 7 Male NA 155.5167 60 15.243217 1.967891 3.937740 ## 8 30-45 NA 144.3250 40 11.893518 1.880530 3.803732 ## 9 46-59 NA 150.7750 40 14.502851 2.293102 4.638237 ## 10 60+ NA 158.9750 40 12.286093 1.942602 3.929283 ## ## $plots ## $plots$sex_bp_before
## ## $plots$agegrp_bp_before
## ## $plots$sex_bp_after
## ## $plots$agegrp_bp_after
plot_interaction plots interaction profiles — lines
connecting group means across levels of one factor, drawn separately for
each level of a second factor. An interaction is present when the lines
are not parallel: the effect of one factor changes depending on the
level of the other.
The two calls demonstrate different contexts:
sex,
agegrp, treatment): a three-way interaction
plot using the melted long-format data. With base_size = 20
the text is scaled up for readability. This is useful when presenting
findings where the treatment effect differs across sex and age
groups.order_factor = TRUE reorders factor levels by their mean,
which can make patterns in the interaction easier to read when factor
levels have no natural order.plot_interaction(df=df_blood_pressure_melt,
dv=5,iv=2:4,base_size=20,title="",type="se")
## $plot_data ## $plot_data$agegrp_sex_blood_pressure ## agegrp sex N blood_pressure sd se ci ## 1 30-45 Female 40 146.050 9.483995 1.499551 3.033129 ## 2 30-45 Male 40 149.950 12.534977 1.981954 4.008880 ## 3 46-59 Female 40 147.725 10.102012 1.597268 3.230780 ## 4 46-59 Male 40 158.150 13.909137 2.199228 4.448358 ## 5 60+ Female 40 157.475 12.029851 1.902086 3.847333 ## 6 60+ Male 40 164.075 10.276654 1.624882 3.286633 ## ## $plot_data$treatment_sex_blood_pressure ## treatment sex N blood_pressure sd se ci ## 1 bp_before Female 60 153.6333 10.73560 1.385960 2.773300 ## 2 bp_before Male 60 159.2667 11.41344 1.473469 2.948405 ## 3 bp_after Female 60 147.2000 11.74272 1.515979 3.033467 ## 4 bp_after Male 60 155.5167 15.24322 1.967891 3.937740 ## ## $plot_data$sex_agegrp_blood_pressure ## sex agegrp N blood_pressure sd se ci ## 1 Female 30-45 40 146.050 9.483995 1.499551 3.033129 ## 2 Female 46-59 40 147.725 10.102012 1.597268 3.230780 ## 3 Female 60+ 40 157.475 12.029851 1.902086 3.847333 ## 4 Male 30-45 40 149.950 12.534977 1.981954 4.008880 ## 5 Male 46-59 40 158.150 13.909137 2.199228 4.448358 ## 6 Male 60+ 40 164.075 10.276654 1.624882 3.286633 ## ## $plot_data$treatment_agegrp_blood_pressure ## treatment agegrp N blood_pressure sd se ci ## 1 bp_before 30-45 40 151.675 9.258087 1.463832 2.960880 ## 2 bp_before 46-59 40 155.100 11.459628 1.811926 3.664967 ## 3 bp_before 60+ 40 162.575 10.727122 1.696107 3.430700 ## 4 bp_after 30-45 40 144.325 11.893518 1.880530 3.803732 ## 5 bp_after 46-59 40 150.775 14.502851 2.293102 4.638237 ## 6 bp_after 60+ 40 158.975 12.286093 1.942602 3.929283 ## ## $plot_data$sex_treatment_blood_pressure ## sex treatment N blood_pressure sd se ci ## 1 Female bp_before 60 153.6333 10.73560 1.385960 2.773300 ## 2 Female bp_after 60 147.2000 11.74272 1.515979 3.033467 ## 3 Male bp_before 60 159.2667 11.41344 1.473469 2.948405 ## 4 Male bp_after 60 155.5167 15.24322 1.967891 3.937740 ## ## $plot_data$agegrp_treatment_blood_pressure ## agegrp treatment N blood_pressure sd se ci ## 1 30-45 bp_before 40 151.675 9.258087 1.463832 2.960880 ## 2 30-45 bp_after 40 144.325 11.893518 1.880530 3.803732 ## 3 46-59 bp_before 40 155.100 11.459628 1.811926 3.664967 ## 4 46-59 bp_after 40 150.775 14.502851 2.293102 4.638237 ## 5 60+ bp_before 40 162.575 10.727122 1.696107 3.430700 ## 6 60+ bp_after 40 158.975 12.286093 1.942602 3.929283 ## ## ## $plot_data_df ## sex agegrp treatment blood_pressure N sd se ci ## 1 Female 30-45146.0500 40 9.483995 1.499551 3.033129 ## 2 Male 30-45 149.9500 40 12.534977 1.981954 4.008880 ## 3 Female 46-59 147.7250 40 10.102012 1.597268 3.230780 ## 4 Male 46-59 158.1500 40 13.909137 2.199228 4.448358 ## 5 Female 60+ 157.4750 40 12.029851 1.902086 3.847333 ## 6 Male 60+ 164.0750 40 10.276654 1.624882 3.286633 ## 7 Female bp_before 153.6333 60 10.735600 1.385960 2.773300 ## 8 Male bp_before 159.2667 60 11.413442 1.473469 2.948405 ## 9 Female bp_after 147.2000 60 11.742722 1.515979 3.033467 ## 10 Male bp_after 155.5167 60 15.243217 1.967891 3.937740 ## 11 Female 30-45 146.0500 40 9.483995 1.499551 3.033129 ## 12 Female 46-59 147.7250 40 10.102012 1.597268 3.230780 ## 13 Female 60+ 157.4750 40 12.029851 1.902086 3.847333 ## 14 Male 30-45 149.9500 40 12.534977 1.981954 4.008880 ## 15 Male 46-59 158.1500 40 13.909137 2.199228 4.448358 ## 16 Male 60+ 164.0750 40 10.276654 1.624882 3.286633 ## 17 30-45 bp_before 151.6750 40 9.258087 1.463832 2.960880 ## 18 46-59 bp_before 155.1000 40 11.459628 1.811926 3.664967 ## 19 60+ bp_before 162.5750 40 10.727122 1.696107 3.430700 ## 20 30-45 bp_after 144.3250 40 11.893518 1.880530 3.803732 ## 21 46-59 bp_after 150.7750 40 14.502851 2.293102 4.638237 ## 22 60+ bp_after 158.9750 40 12.286093 1.942602 3.929283 ## 23 Female bp_before 153.6333 60 10.735600 1.385960 2.773300 ## 24 Female bp_after 147.2000 60 11.742722 1.515979 3.033467 ## 25 Male bp_before 159.2667 60 11.413442 1.473469 2.948405 ## 26 Male bp_after 155.5167 60 15.243217 1.967891 3.937740 ## 27 30-45 bp_before 151.6750 40 9.258087 1.463832 2.960880 ## 28 30-45 bp_after 144.3250 40 11.893518 1.880530 3.803732 ## 29 46-59 bp_before 155.1000 40 11.459628 1.811926 3.664967 ## 30 46-59 bp_after 150.7750 40 14.502851 2.293102 4.638237 ## 31 60+ bp_before 162.5750 40 10.727122 1.696107 3.430700 ## 32 60+ bp_after 158.9750 40 12.286093 1.942602 3.929283 ## ## $plots ## $plots$agegrp_sex_blood_pressure
## ## $plots$treatment_sex_blood_pressure
## ## $plots$sex_agegrp_blood_pressure
## ## $plots$treatment_agegrp_blood_pressure
## ## $plots$sex_treatment_blood_pressure
## ## $plots$agegrp_treatment_blood_pressure
plot_interaction(df=df_crop_yield,dv=3,iv=1:2,type="se",order_factor=TRUE)
## $plot_data ## $plot_data$Water_Fert_Yield ## Water Fert N Yield sd se ci ## 1 High A 5 31.80 3.146427 1.407125 3.906805 ## 2 High B 5 29.84 3.374611 1.509172 4.190133 ## 3 Low A 5 30.00 3.512834 1.570987 4.361759 ## 4 Low B 5 24.52 3.791042 1.695406 4.707200 ## ## $plot_data$Fert_Water_Yield ## Fert Water N Yield sd se ci ## 1 A High 5 31.80 3.146427 1.407125 3.906805 ## 2 A Low 5 30.00 3.512834 1.570987 4.361759 ## 3 B High 5 29.84 3.374611 1.509172 4.190133 ## 4 B Low 5 24.52 3.791042 1.695406 4.707200 ## ## ## $plot_data_df ## Fert Water Yield N sd se ci ## 1 A High 31.80 5 3.146427 1.407125 3.906805 ## 2 B High 29.84 5 3.374611 1.509172 4.190133 ## 3 A Low 30.00 5 3.512834 1.570987 4.361759 ## 4 B Low 24.52 5 3.791042 1.695406 4.707200 ## 5 A High 31.80 5 3.146427 1.407125 3.906805 ## 6 A Low 30.00 5 3.512834 1.570987 4.361759 ## 7 B High 29.84 5 3.374611 1.509172 4.190133 ## 8 B Low 24.52 5 3.791042 1.695406 4.707200 ## ## $plots ## $plots$Water_Fert_Yield
## ## $plots$Fert_Water_Yield
plot_oneway_diagnostics produces a panel of diagnostic
plots for checking whether the assumptions of ANOVA are met. These
should always be inspected before trusting ANOVA results.
The key plots produced are:
Here the blood pressure data is used in long format with blood pressure as the DV and sex, age group, and treatment as IVs, allowing diagnostics to be assessed across all three grouping factors simultaneously.
plot_oneway_diagnostics(df=df_blood_pressure_melt,dv=5,iv=2:4,base_size=15)
## $sex_blood_pressure
## ## $agegrp_blood_pressure
## ## $treatment_blood_pressure