Skip to contents

Produces one of four ggplot2 visualisations from a compare() result object, all correctly handling the by-group structure (including multi-factor groups) through automatic faceting.

Usage

plot_compare(
  res,
  type = c("dotplot", "errbar", "letters", "heatmap"),
  reference = NULL,
  interactive = FALSE,
  theme = ggplot2::theme_bw(),
  return_data = FALSE,
  ...
)

Arguments

res

A data frame returned by compare().

type

Character string selecting the plot type. One of "dotplot" (default), "errbar", "letters", or "heatmap".

reference

Character string or NULL. Applies to type = "dotplot" only. When NULL (default), the criterion band is anchored to the top-ranked variety in each group (one-sided band; points outside the band are red). When set to a variety name, the band is centred on that variety ([ref \eqn{-} crit,\; ref + crit]), the reference variety is shown as a diamond, and points are coloured green (significantly better), blue (not significant), or red (significantly worse). If the reference variety is not found in a particular group a warning is issued and that group falls back to top-ranked behaviour. Ignored with a warning for type = "letters" or "heatmap".

interactive

Logical. If TRUE, converts the finished ggplot to an interactive plotly::ggplotly() object with hover tooltips showing the variety name, predicted value, group, and significance status. Requires the plotly package. Default FALSE.

theme

A complete ggplot2 theme object. Default ggplot2::theme_bw().

return_data

Logical. If TRUE returns the tidy data frame used to build the plot rather than the plot itself. Default FALSE.

...

Additional arguments passed to the background geom_point() call (e.g. size, alpha).

Value

A ggplot object (when interactive = FALSE and return_data = FALSE), a pc_interactive object (when interactive = TRUE – auto-converts to plotly on print(); supports + for ggplot2 layer extensions before conversion), or a data.frame (when return_data = TRUE).

Details

The four type options are:

"dotplot"

Varieties sorted by predicted value (highest at top) within each group. A shaded band of width equal to the comparison criterion is drawn below the top-ranked variety – any variety whose point falls within the band is not significantly different from the best. Points outside the band are coloured red. The dashed vertical line marks the top-ranked predicted value.

"errbar"

Varieties sorted by predicted value (highest at top). A horizontal line segment spanning \([\hat\tau - \text{crit}/2,\; \hat\tau + \text{crit}/2]\) is drawn for each variety, with the predicted value marked by a point. Two varieties are significantly different if and only if their bars do not overlap – an exact visual equivalent of the criterion test.

"letters"

Compact letter display (CLD) overlaid on the sorted dot plot. Varieties sharing at least one letter are not significantly different. Letters are assigned by the standard descending sweep algorithm using the comparison criterion as the significance threshold.

"heatmap"

An \(n \times n\) tile matrix with varieties sorted by predicted value on both axes. Tile colour encodes the absolute pairwise difference; a white cross (\(\times\)) marks pairs that are significantly different. Useful for large variety sets where a letter display becomes unreadable.

Group detectionplot_compare() infers which columns are grouping variables and which is the comparison variable directly from res. Factor columns that are constant within every group (i.e. within every block of rows sharing the same criterion value) are treated as by columns; the remaining factor column is the comparison variable. When multiple by columns are present their values are pasted into a composite facet label.

Examples

if (FALSE) { # \dontrun{
res <- compare(model,
               term = "Treatment:Site:Variety",
               by   = "Site",
               type = "HSD")

# Sorted dot plot with criterion band anchored to best variety
plot_compare(res)

# Anchor band to a specific check variety
plot_compare(res, reference = "Scout")

# Interactive plotly version with hover tooltips
plot_compare(res, interactive = TRUE)

# Compact letter display
plot_compare(res, type = "letters")

# Interactive heatmap
plot_compare(res, type = "heatmap", interactive = TRUE)

# Retrieve the tidy data frame
df <- plot_compare(res, return_data = TRUE)

# Extend with a custom title
plot_compare(res, type = "letters") +
  ggplot2::ggtitle("Variety comparison -- HSD (Tukey)")
} # }