Skip to content

Conversation

@SchmidtPaul
Copy link
Contributor

Summary

This PR adds support for the aspect parameter in ggdesplot() to control cell aspect ratios for true-scale field maps.

Note that this PR is also kind of an issue - see "Observation on Lattice Behavior" below

The aspect ratio feature is documented for the lattice version with the note "To get a map of a field with a true aspect ratio (lattice version only), include 'aspect=ylen/xlen'". This PR removes the "lattice version only" limitation by implementing aspect ratio support for the ggplot2 version.

Changes

R/ggdesplot.R:

  • Added aspect parameter to function signature (line 81)
  • Implemented aspect ratio logic using coord_fixed(ratio=aspect, expand=FALSE) when aspect is specified (lines 530-536)
  • When aspect is NULL, uses coord_cartesian(expand=FALSE) as before (default behavior unchanged)

R/desplot.R:

  • Updated the call to ggdesplot() to pass ... arguments through (line 330)
  • This allows aspect (and other future parameters) to flow from desplot(gg=TRUE) to ggdesplot()

Implementation Details

The implementation uses coord_fixed(ratio=aspect) where aspect represents the height/width ratio. Both lattice and ggplot2 use this same convention:

  • aspect=1 → square cells
  • aspect=2 → cells 2x taller than wide
  • aspect=0.5 → cells 2x wider than tall

Observation on Lattice Behavior

During development and testing, I observed something unexpected: when using ggdesplot() with aspect=1, the cells appear square (as expected). However, when using the lattice version desplot() with aspect=1, the cells do not appear square.

I wanted to bring this to your attention because:

  1. I may be misunderstanding how the aspect parameter is intended to work in the lattice version
  2. There may be a difference in how lattice and ggplot2 interpret aspect ratios that I'm not aware of
  3. It's possible this represents an area where the implementations naturally differ

The ggplot2 implementation uses coord_fixed(ratio=aspect) which directly controls the ratio of y-axis units to x-axis units, creating cell shapes that correspond to the aspect value as I understand it.

Reprex

library(desplot)
library(agridat)

desplot(
  yates.oats,
  yield ~ col * row,
  col = gen,
  num = nitro,
  cex = 1,
  out1 = block,
  aspect = 1,
  gg = FALSE,
  main = "aspect=1, gg=FALSE"
)

desplot(
  yates.oats,
  yield ~ col * row,
  col = gen,
  num = nitro,
  cex = 1,
  out1 = block,
  aspect = 1,
  gg = TRUE,
  main = "aspect=1, gg=TRUE"
)

desplot(
  yates.oats,
  yield ~ col * row,
  col = gen,
  num = nitro,
  cex = 1,
  out1 = block,
  aspect = 0.5,
  gg = FALSE,
  main = "aspect=0.5, gg=FALSE"
)

desplot(
  yates.oats,
  yield ~ col * row,
  col = gen,
  num = nitro,
  cex = 1,
  out1 = block,
  aspect = 0.5,
  gg = TRUE,
  main = "aspect=0.5, gg=TRUE"
)

cat(
  "Testing with desplot version:",
  as.character(packageVersion("desplot")),
  "\n"
)
#> Testing with desplot version: 1.11

Created on 2025-11-14 with reprex v2.1.1

Benefits

  • Enables true-scale field mapping with ggdesplot()
  • Brings ggplot2 version closer to feature parity with lattice version
  • Cells correctly scale according to aspect ratio value
  • Default behavior unchanged when aspect is not specified

claude and others added 11 commits November 13, 2025 13:46
Adds support for the 'aspect' parameter in ggdesplot() to create
true-scale field maps, bringing feature parity with the lattice
version.

Changes:
- Added 'aspect' parameter to ggdesplot() function signature
- When aspect is specified, uses coord_fixed(ratio=aspect, expand=FALSE)
- When aspect is NULL, uses coord_cartesian(expand=FALSE) as before
- Updated desplot() to pass ... arguments to ggdesplot(), allowing
  aspect and other parameters to flow through

Usage example:
  ggdesplot(yates.oats, yield ~ col*row, aspect=511/176)

This allows users to create plots with accurate spatial proportions
matching the actual field dimensions.
Adds support for the 'aspect' parameter in ggdesplot() to create
true-scale field maps, bringing feature parity with the lattice
version.

Changes:
- Added 'aspect' parameter to ggdesplot() function signature
- Converts aspect ratio from lattice convention (width/height) to
  ggplot2 convention (height/width) by inverting: coord_fixed(1/aspect)
- When aspect is NULL, uses coord_cartesian(expand=FALSE) as before
- Updated desplot() to pass ... arguments to ggdesplot(), allowing
  aspect and other parameters to flow through

Usage example:
  ggdesplot(yates.oats, yield ~ col*row, aspect=511/176)

This allows users to create plots with accurate spatial proportions
matching the actual field dimensions and the lattice version output.
Adds support for the 'aspect' parameter in ggdesplot() to create
true-scale field maps, bringing feature parity with the lattice
version.

Changes:
- Added 'aspect' parameter to ggdesplot() function signature
- Uses coord_fixed(ratio=aspect, expand=FALSE) when aspect is specified
- Both lattice and ggplot2 use the same convention: aspect = height/width
- When aspect is NULL, uses coord_cartesian(expand=FALSE) as before
- Updated desplot() to pass ... arguments to ggdesplot(), allowing
  aspect and other parameters to flow through

Usage example:
  ggdesplot(yates.oats, yield ~ col*row, aspect=511/176)

This allows users to create plots with accurate spatial proportions
matching the actual field dimensions.
@SchmidtPaul SchmidtPaul changed the title Add aspect ratio support in ggdesplot Add aspect ratio support in ggdesplot (needs to be checked) Nov 14, 2025
@SchmidtPaul SchmidtPaul changed the title Add aspect ratio support in ggdesplot (needs to be checked) Add aspect ratio support in ggdesplot (not completely sure) Nov 14, 2025
@kwstat kwstat merged commit 018c623 into kwstat:main Dec 5, 2025
1 check passed
@kwstat
Copy link
Owner

kwstat commented Dec 5, 2025

I'm adding a better example to desplot.R file:

data(yates.oats)
# Show experiment layout in true aspect
# Field width = 4 plots * 44 links = 176 links
# Field length = 18 plots * 28.4 links = 511 links
# With lattice, the aspect ratio is y/x for the entire field
desplot(yates.oats, 
        yield ~ col+row, 
        out1=block, out2=gen, aspect=511/176)
# With ggplot, the aspect ratio is y/x for each cell
ggdesplot(yates.oats, 
        yield ~ col+row, 
        out1=block, out2=gen, aspect=28.4/44)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants